Relay

API reference

Every export from @relayhttp/relay. Types are shown in TypeScript syntax.

On this page

#relay()

Signature
relay(input: string | URL | Request, options?: RelayOptions): RelayRequest

Sends a request. The method defaults to GET unless you pass options.method. The method helpers below are usually more readable.

ParameterTypeDescription
inputstring | URL | RequestThe URL to request. Relative URLs use baseUrl.
optionsRelayOptionsOptional. See Options.

Returns RelayRequest: a promise for a Response with body helpers.

Example
const res = await relay('https://api.example.com/health', { method: 'HEAD' });

#Method helpers

Signature
relay.get(input, options?)
relay.post(input, options?)
relay.put(input, options?)
relay.patch(input, options?)
relay.delete(input, options?)
relay.head(input, options?)

Shorthands that set options.method for you. They accept the same arguments as relay().

Returns RelayRequest

Example
await relay.patch('todos/7', { json: { done: true } });

#RelayRequest

The value returned by every request. Await it for the raw Response, or call a body helper:

MethodReturnsDescription
.json<T>()Promise<T>Parses JSON. Returns undefined for empty 204 responses.
.text()Promise<string>Reads the body as text.
.blob()Promise<Blob>Reads the body as a Blob, e.g. for downloads.
.arrayBuffer()Promise<ArrayBuffer>Reads raw bytes.
.formData()Promise<FormData>Parses multipart or URL-encoded form bodies.

#relay.create()

Signature
relay.create(defaults: RelayOptions): RelayInstance

Creates a new instance with its own defaults and interceptors. Instances have every method the default export has.

ParameterTypeDescription
defaultsRelayOptionsOptions applied to every request made with the instance.

Returns RelayInstance

Example
export const github = relay.create({
  baseUrl: 'https://api.github.com',
  headers: { accept: 'application/vnd.github+json' },
});

#instance.extend()

Signature
instance.extend(defaults: RelayOptions): RelayInstance

Creates a child instance that inherits the parent's defaults and interceptors, then layers new defaults on top. Headers are merged; other options are replaced.

ParameterTypeDescription
defaultsRelayOptionsOptions to add or override.

Returns RelayInstance

Example
const admin = api.extend({ headers: { 'x-role': 'admin' } });

#Interceptors

Signature
instance.interceptors.request.use(fn: (request: Request) => void | Request | Promise<void | Request>): () => void
instance.interceptors.response.use(fn: (response: Response, request: Request) => Response | Promise<Response>): () => void

Registers a function that runs before each request or after each response. Return a new Request or Response to replace the original.

Returns A function that removes the interceptor.

Example
const stop = api.interceptors.request.use((req) => {
  console.time(req.url);
});

stop(); // remove it again

#RelayError

Thrown (as a rejected promise) for every failure. It extends Error.

PropertyTypeDescription
code'HTTP_ERROR' | 'TIMEOUT' | 'NETWORK_ERROR' | 'ABORTED' | 'PARSE_ERROR'What went wrong.
statusnumber | undefinedHTTP status, when there was a response.
requestRequestThe request that failed.
responseResponse | undefinedThe response, for HTTP_ERROR.
attemptsnumberHow many attempts were made, including retries.

#Options

OptionTypeDescription
baseUrlstringPrepended to relative URLs.
headersHeadersInitHeaders merged into every request.
searchParamsRecord | string | URLSearchParamsQuery string to append.
jsonunknownSerialised as the JSON body. Sets Content-Type.
bodyBodyInitRaw request body, e.g. FormData.
timeoutnumber | falsePer-attempt timeout in ms. Default 10000.
totalTimeoutnumber | falseTimeout across all attempts. Default false.
retryRetryOptions | numberSee Retries. Default limit 2.
dedupebooleanShare one in-flight request between identical GETs. Default true.
fetchtypeof fetchCustom fetch implementation, useful for tests.

Was this page helpful?

Edit this page on GitHub

Last updated

© 2026 Relay contributors. Released under the MIT License.

v3.2.0 · FAQ · GitHub