For AI agents: the complete documentation index is available at https://navios.tech/llms.txt, the full documentation bundle is available at https://navios.tech/llms-full.txt, and this page is available as Markdown at https://navios.tech/docs/builder/essentials/endpoints.md.

Declaring endpoints

API.declareEndpoint(options) is the only way to declare an HTTP endpoint. This page is the map of what options can contain. The larger topics have their own pages; the smaller ones are covered here.

Method and URL

Every declaration needs a method and a url. The method is one of GET, POST, PUT, PATCH, DELETE, HEAD or OPTIONS. The URL is a path relative to the client's base URL, with $name marking URL parameters.

// API and userSchema come from the Quick start
export const deleteUser = API.declareEndpoint({
  method: 'DELETE',
  url: '/users/$userId',
  response: 'void',
})

The options at a glance

OptionWhat it doesCovered in
method, urlIdentify the endpointthis page
urlParamsSchemaValidates and types the $name valuesInputs
querySchemaValidates and types the query string (params)Inputs
requestSchemaValidates and types the body (data)Inputs
body'json' (default) or 'multipart'File uploads
responseSchemaValidates and types a JSON responseResponses
responsesPer-status success schemas, instead of responseSchemaResponses
response'json' (default), 'blob', 'text', 'stream' or 'void'Responses
validateResponsefalse skips the runtime check of the responseResponses
errorSchemaSchemas for error bodies, by statusErrors
headersHeaders sent with every callthis page
timeoutTimeout in millisecondsthis page
clientOptionsExtra keys for your HTTP clientthis page

A responseSchema is required unless the declaration has responses or a non-JSON response kind. Any key that is not in this table is a compile error, so a typo such as requstSchema cannot slip through. So are options that contradict each other, which also throw a NaviosError when the endpoint is declared:

  • responses together with responseSchema
  • responseSchema or responses together with a non-JSON response
  • requestSchema or a multipart body on a GET or HEAD endpoint

Headers and timeout

headers and timeout set defaults for one endpoint. A call can override both: per-call headers are merged over the endpoint's, whatever the case of their names, and a per-call timeout wins.

export const exportUsers = API.declareEndpoint({
  method: 'POST',
  url: '/users/export',
  responseSchema: z.object({ jobId: z.string() }),
  headers: { 'X-Feature': 'export' },
  timeout: 30_000,
})

await exportUsers({ timeout: 60_000, headers: { 'X-Trace': traceId } })

Headers that every endpoint needs, such as authorization, belong on the HTTP client rather than on each declaration.

Client options

clientOptions passes keys through to your HTTP client that Builder itself has no opinion about, such as withCredentials (axios and @navios/http) or credentials (fetch and @navios/http).

export const getSession = API.declareEndpoint({
  method: 'GET',
  url: '/session',
  responseSchema: z.object({ userId: z.string() }),
  clientOptions: { withCredentials: true },
})

It cannot override what Builder controls: method, url, params, data, signal, timeout and responseType are always set from the declaration and the call. headers in clientOptions are sent, merged underneath the endpoint's headers and the headers of the call.

Reading a declaration back

The options object is available unchanged as endpoint.config. Use it to build tooling on top of your declarations, or to reuse a schema.

getUser.config.method // 'GET'
getUser.config.url // '/users/$userId'
getUser.config.responseSchema // userSchema

Type helpers

When you need the types of an endpoint elsewhere — a function parameter, a component prop — derive them from the declaration.

import type {
  EndpointConfig,
  EndpointErrorType,
  EndpointParams,
  EndpointResult,
} from '@navios/builder'

type GetUserArgs = EndpointParams<typeof getUser> // { urlParams: { userId: string | number }, ... }
type User = EndpointResult<typeof getUser> // { id: string; name: string; email: string }
type GetUserError = EndpointErrorType<typeof getUser> // the typed EndpointError union
type GetUserConfig = EndpointConfig<typeof getUser> // the declared options
async function loadUser(args: EndpointParams<typeof getUser>): Promise<User> {
  return getUser(args)
}