Skip to main content
The Fetch API provides an interface for making HTTP requests and handling responses. It is the primary way workers communicate over HTTP.

fetch()

Make an HTTP request.
function fetch(
  input: RequestInfo,
  init?: RequestInit
): Promise<Response>
input
Request | string
required
The resource to fetch. Can be a Request object or a URL string.
init
RequestInit
Optional configuration object:
  • method: HTTP method (GET, POST, PUT, DELETE, etc.)
  • headers: Headers object or record
  • body: Request body (string, ArrayBuffer, FormData, ReadableStream, etc.)
  • redirect: Redirect handling (follow, error, manual)
  • cf: Cloudflare-specific options
  • signal: AbortSignal for cancellation
Promise<Response>
Resolves to a Response object representing the HTTP response.

Request

Represents an HTTP request.

Constructor

new Request(input: RequestInfo, init?: RequestInit)

Properties

method
string
HTTP method (e.g., GET, POST).
url
string
Full URL of the request.
headers
Headers
Headers object containing request headers.
body
ReadableStream | null
Request body as a stream, or null if no body.
bodyUsed
boolean
True if the body has been read.
redirect
string
Redirect mode: follow, error, or manual.
signal
AbortSignal
AbortSignal for cancelling the request.
cf
IncomingRequestCfProperties | undefined
Cloudflare-specific request properties (available on incoming requests).

Methods

clone()
method
Create a copy of the request.
clone(): Request
arrayBuffer()
method
Read the request body as an ArrayBuffer.
arrayBuffer(): Promise<ArrayBuffer>
bytes()
method
Read the request body as a Uint8Array.
bytes(): Promise<Uint8Array>
text()
method
Read the request body as a string.
text(): Promise<string>
json()
method
Parse the request body as JSON.
json<T = unknown>(): Promise<T>
formData()
method
Parse the request body as form data.
formData(): Promise<FormData>
blob()
method
Read the request body as a Blob.
blob(): Promise<Blob>

Response

Represents an HTTP response.

Constructor

new Response(body?: BodyInit | null, init?: ResponseInit)
body
BodyInit | null
Response body: string, ArrayBuffer, Blob, FormData, URLSearchParams, or ReadableStream.
init
ResponseInit
Optional configuration:
  • status: HTTP status code (default: 200)
  • statusText: Status message
  • headers: Headers object or record
  • cf: Cloudflare-specific options
  • webSocket: WebSocket to accept
  • encodeBody: Encoding type (auto, manual)

Properties

status
number
HTTP status code.
statusText
string
HTTP status message.
ok
boolean
True if status is in the range 200-299.
headers
Headers
Headers object containing response headers.
body
ReadableStream | null
Response body as a stream, or null if no body.
bodyUsed
boolean
True if the body has been read.
redirected
boolean
True if the response is the result of a redirect.
url
string
Final URL after redirects.
webSocket
WebSocket | null
WebSocket if this response accepted a WebSocket upgrade.

Methods

clone()
method
Create a copy of the response.
clone(): Response
arrayBuffer()
method
Read the response body as an ArrayBuffer.
arrayBuffer(): Promise<ArrayBuffer>
bytes()
method
Read the response body as a Uint8Array.
bytes(): Promise<Uint8Array>
text()
method
Read the response body as a string.
text(): Promise<string>
json()
method
Parse the response body as JSON.
json<T = unknown>(): Promise<T>
formData()
method
Parse the response body as form data.
formData(): Promise<FormData>
blob()
method
Read the response body as a Blob.
blob(): Promise<Blob>

Static methods

Response.json()
method
Create a Response with JSON body.
static json(
  data: any,
  init?: ResponseInit
): Response
Response.redirect()
method
Create a redirect response.
static redirect(
  url: string | URL,
  status?: number
): Response

Headers

Represents HTTP headers.

Constructor

new Headers(init?: HeadersInit)

Methods

get()
method
Get a header value.
get(name: string): string | null
has()
method
Check if a header exists.
has(name: string): boolean
set()
method
Set a header value, replacing any existing value.
set(name: string, value: string): void
append()
method
Append a value to a header.
append(name: string, value: string): void
delete()
method
Delete a header.
delete(name: string): void
forEach()
method
Iterate over headers.
forEach(
  callback: (value: string, name: string, parent: Headers) => void
): void
entries()
method
Get an iterator of [name, value] pairs.
entries(): IterableIterator<[string, string]>
keys()
method
Get an iterator of header names.
keys(): IterableIterator<string>
values()
method
Get an iterator of header values.
values(): IterableIterator<string>

Example

// Making a GET request
const response = await fetch('https://api.example.com/data');
const data = await response.json();

// Making a POST request
const response = await fetch('https://api.example.com/submit', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ key: 'value' }),
});

// Creating a Response
const response = new Response('Hello World', {
  status: 200,
  headers: {
    'Content-Type': 'text/plain',
  },
});

// Working with Headers
const headers = new Headers();
headers.set('Content-Type', 'application/json');
headers.append('X-Custom-Header', 'value');

// Cloning requests/responses
const request = new Request('https://example.com');
const clonedRequest = request.clone();

Build docs developers (and LLMs) love