Skip to main content
fetch is a global function. It sends one HTTP request and resolves with a Response — including for HTTP error statuses like 404 or 500.

Permissions

fetch reaches the network, so every request needs --allow-net:
  • Matching uses the URL’s literal host, lowercased. There is no DNS resolution and no IP matching: localhost and 127.0.0.1 are different scopes, and granting api.example.com does not permit fetching https://93.184.216.34/.
  • Default ports count. https://example.com/ checks example.com:443; http://example.com/ checks example.com:80.
  • Every redirect hop is checked independently against the new URL.
  • A configured proxy does not change the destination check; see Proxies for the proxy-host caveat.
Failure takes two different shapes:
The initial denial message names the host and port: fetch requires --allow-net for 'api.example.com:443'. The redirect-hop string carries neither code nor the host. Full model: Permissions.

Quick look

Modules vs scripts

ff forces ES module evaluation when a file ends in .mjs. For .js files, ff tries classic-script compilation first and automatically falls back to ES module evaluation when the file contains import, export, or top-level await anywhere in the source. The runnable examples on this page wrap async code in (async () => { ... })() so they also work when pasted into classic-script contexts.

fetch(url, init?)

Only method, headers, body, and signal are read. These init keys are ignored: timeout, redirect, credentials, cache, mode.

Cancellation: AbortController and AbortSignal

Use AbortController or AbortSignal.timeout(ms) to cancel a fetch before or during an in-flight request. Aborting closes the underlying socket immediately and rejects the fetch promise with signal.reason:

Response reference

The object fetch resolves with — and the object your http.serve handler returns. Bodies can be re-read freely (bodyUsed remains false):
Construction + statics (for handlers and tests):

Request reference

Build requests explicitly when you need to inspect or reuse them:
fetch(req) applies the same --allow-net check to req.url as it does to a string URL, including on redirect hops.

Headers reference

Case-insensitive; names are lowercased internally:
CR/LF characters in header values are dropped.

Redirects

Followed automatically up to 5 hops. res.redirected and res.url tell you where the response actually came from. Past 5 hops, fetch throws Too many redirects. There is no manual-redirect mode. Every hop is re-checked against --allow-net. A redirect from an allowed host to a host outside the scope rejects the promise with the string "PermissionDenied: fetch requires --allow-net"; the initial request is not retried and no Response is produced. Note: redirects your own http.serve handler returns via Response.redirect() carry no Location header, so nothing follows them — see Gotchas.

Proxies

fetch honors the standard proxy environment variables. They are read once at process start — set them before launching ff: Proxy URLs may include credentials (http://user:pass@proxy:8080), which are sent as proxy-authorization. http:// requests are tunneled with CONNECT (falling back to an absolute-form request line when the proxy refuses it); https:// targets always tunnel through CONNECT and carry real end-to-end TLS to the target. NO_PROXY entries match the host itself and its subdomains (example.com also matches api.example.com); append :port to scope an entry, and * bypasses every host. Only HTTP proxies are supported (no SOCKS), and the proxy URL itself must be http:// — an https:// proxy URL (TLS to the proxy) is rejected.
Permissions caveat: proxy variables are read by the runtime directly, so no --allow-env grant is needed. --allow-net is checked against the destination URL, not the proxy host. If the environment configures a proxy, an allowed request will connect to that proxy even when its host is absent from every --allow-net scope. Set NO_PROXY or clear the proxy variables when that is not intended.

Full example

Limits (summary)

Full table lives in the Limitations reference: 16 concurrent fetches, 64KB body stage per slot, 16 MB max response body (both Content-Length and decompressed output), 30s I/O timeout, AbortSignal cancellation (init.signal / req.signal), 5 redirect hops, every hop re-checked against --allow-net, cross-origin redirect credential stripping (authorization, cookie, x-api-key), RFC 7230 header validation, HTTP/1.1 only, HTTP proxies via env vars only (no SOCKS).

Gotchas

Permission denial is two different shapes. The initial URL throws a real Error with name: "PermissionDenied" and code: "PERMISSION_DENIED". A redirect-hop denial rejects with the plain string "PermissionDenied: fetch requires --allow-net" — no code, no host. Catch both, and log with String(e) so string rejections print something useful. Cross-origin redirects strip credentials. If a redirect crosses to a different origin (different scheme, host, or port), Fairyfly automatically strips sensitive headers (authorization, cookie, x-api-key) to prevent credential leakage. Response body limit is 16 MB. Both the declared Content-Length and decompressed (gzip/deflate) body data are capped at 16 MiB. Larger responses fail and reject the promise. Method names are exact-case. fetch matches init.method against the standard tokens verbatim — "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS". Anything else, including lowercase ("post"), is sent as GET (a body is still attached). Your http.serve handler likewise sees the method exactly as the client sent it, so normalize before comparing (method.toUpperCase() === "POST"). Only string bodies reach the wire intact. Every other body value is coerced with JS String() semantics: a FormData or Blob arrives as "[object Object]", an ArrayBuffer as "[object ArrayBuffer]", and a Uint8Array([104, 105]) as "104,105". URLSearchParams survives only because its toString() is the query string. There is no multipart encoding and no binary-body path — serialize yourself (JSON.stringify, params.toString(), base64, or a hand-built multipart string with an explicit content-type header). Response.redirect() sends no Location header. It sets the status (default 302) and flags the object as redirected, but clients never follow it. Build the response yourself when clients must follow:
Rejections are plain strings. Network failures reject with "Network error", bad URLs with "Invalid URL", long chains with "Too many redirects" — e.message is undefined, so log String(e). The initial permission denial is the exception: it throws an Error and never reaches the promise.

See also