Skip to main content

Fetch API

MyCroCloud functions include a built-in fetch() function for making HTTP requests to external APIs. It follows the standard Fetch API with some differences noted below.

Basic Usage

function handler(request) {
const res = fetch('https://api.example.com/users/1');
const user = res.json();

return {
statusCode: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify(user)
};
}

Syntax

fetch(url)
fetch(url, options)

Parameters

url (string, required)

The URL to fetch. Must use http:// or https:// scheme.

options (object, optional)

PropertyTypeDefaultDescription
methodstring"GET"HTTP method (GET, POST, PUT, DELETE, PATCH, etc.)
headersobject{}Request headers as key-value pairs
bodystringundefinedRequest body (typically used with POST/PUT)

Return Value

Returns a Response object with the following properties and methods:

Property/MethodTypeDescription
statusnumberHTTP status code (e.g., 200, 404)
statusTextstringHTTP status text (e.g., "OK", "Not Found")
okbooleantrue if status is 200–299
headersobjectResponse headers as key-value pairs (lowercase keys)
text()functionReturns the response body as a string
json()functionParses the response body as JSON and returns the result

Examples

GET request

function handler(request) {
const res = fetch('https://jsonplaceholder.typicode.com/posts/1');

return {
statusCode: res.status,
headers: { 'content-type': 'application/json' },
body: JSON.stringify(res.json())
};
}

POST with JSON body

function handler(request) {
const res = fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + env.API_TOKEN
},
body: JSON.stringify({
name: request.body.name,
email: request.body.email
})
});

if (!res.ok) {
return {
statusCode: 502,
body: 'Upstream API error: ' + res.status + ' ' + res.statusText
};
}

return {
statusCode: 201,
headers: { 'content-type': 'application/json' },
body: res.text()
};
}

Reading response headers

function handler(request) {
const res = fetch('https://api.example.com/data');

return {
statusCode: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
contentType: res.headers['content-type'],
data: res.json()
})
};
}

Multiple requests

function handler(request) {
const users = fetch('https://api.example.com/users').json();
const posts = fetch('https://api.example.com/posts').json();

return {
statusCode: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ users, posts })
};
}

Differences from the Web Fetch API

FeatureWeb Fetch APIMyCroCloud fetch()
Return typePromise<Response>Response (synchronous)
.text() / .json()Returns a PromiseReturns the value directly
Streaming bodySupportedNot supported
Request objectSupported as inputNot supported (use URL string)
Headers classSupportedPlain object instead
AbortControllerSupportedNot supported
mode, credentials, cacheSupportedNot supported

Since MyCroCloud functions run in a synchronous JavaScript engine, fetch() and its response methods (text(), json()) return values directly instead of Promises. There is no need for await or .then().

Limits

LimitValueDescription
Max requests per execution50Total number of fetch() calls allowed per function invocation
Request body size1 MBMaximum size of the outgoing request body
Response body size5 MBMaximum size of a single response body
Total bandwidth10 MBTotal bytes received across all fetch calls
Per-request timeout5 secondsMaximum time for a single fetch request
Max redirects5Maximum number of HTTP redirects followed
Max recursion depth3Maximum depth when functions call other MyCroCloud functions

Exceeding these limits will throw an error that terminates your function execution.

Security Restrictions

For security, the following restrictions are enforced:

  • HTTPS/HTTP only — Only http:// and https:// URLs are allowed. Schemes like file://, ftp://, and data:// are blocked.
  • No private network access — Requests to private/internal IP addresses are blocked, including 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, and cloud metadata endpoints (169.254.169.254).
  • Restricted headers — The Host, Cookie, Set-Cookie, and User-Agent headers cannot be set on outgoing requests.
  • Fixed User-Agent — All requests are sent with User-Agent: MycroCloud-FunctionInvoker/1.0, which cannot be overridden.
  • Recursion depth limit — When a function uses fetch() to call another MyCroCloud function (which may in turn call another), the call chain is limited to 3 levels deep. This prevents infinite loops such as function A calling function B which calls function A back. Exceeding this limit returns HTTP 508 Loop Detected.