Skip to main content

HTTP interceptors

Callback hooks for intercepting and modifying HTTP requests and responses. Pass an SDKHooks instance to GustoProvider via config.hooks.

SDKHooks

Request interceptors for customizing HTTP requests and responses.

Remarks

Pass an instance of this interface to GustoProvider via config.hooks to inspect or modify requests and responses across the four lifecycle stages. Each entry is an array of objects implementing the corresponding hook type from @gusto/embedded-api-v-2026-06-15/hooks/types.

StageWhen it runs
beforeCreateRequestBefore the Request object is constructed (URL / method changes)
beforeRequestAfter the Request is created but before it is sent (headers, auth tokens)
afterSuccessAfter a successful response (2xx)
afterErrorAfter an error response (4xx, 5xx) or network failure

Example

import { GustoProvider, type SDKHooks } from '@gusto/embedded-react-sdk'

const hooks: SDKHooks = {
beforeRequest: [
{
beforeRequest: (context, request) => {
request.headers.set('Authorization', `Bearer ${getToken()}`)
return request
},
},
],
}

<GustoProvider config={{ baseUrl: '/api/', hooks }}>
<YourApp />
</GustoProvider>

Properties

PropertyTypeDescription
afterError?AfterErrorHook[]Hooks executed after error responses (4xx, 5xx) or network failures
afterSuccess?AfterSuccessHook[]Hooks executed after successful responses (2xx status codes)
beforeCreateRequest?BeforeCreateRequestHook[]Hooks executed before creating a Request object
beforeRequest?BeforeRequestHook[]Hooks executed after Request creation but before sending

Utility types

AfterErrorContext

AfterErrorContext = HookContext & object


AfterErrorHook

Properties

PropertyTypeDescription
afterError(hookCtx: HookContext, response: Response | null, error: unknown) => Awaitable<{ error: unknown; response: Response | null; }>A hook that is called after the SDK encounters an error, or a non-successful response. The hook can introduce instrumentation code such as logging, tracing and metrics or modify the response or error values.

AfterSuccessContext

AfterSuccessContext = HookContext & object


AfterSuccessHook

Properties

PropertyTypeDescription
afterSuccess(hookCtx: HookContext, response: Response) => Awaitable<Response>A hook that is called after the SDK receives a response. The hook can introduce instrumentation code such as logging, tracing and metrics or modify the response before it is handled or throw an error to stop the response from being handled.

BeforeCreateRequestContext

BeforeCreateRequestContext = HookContext & object


BeforeCreateRequestHook

Properties

PropertyTypeDescription
beforeCreateRequest(hookCtx: HookContext, input: RequestInput) => RequestInputA hook that is called before the SDK creates a Request object. The hook can modify how a request is constructed since certain modifications, like changing the request URL, cannot be done on a request object directly.

BeforeRequestContext

BeforeRequestContext = HookContext & object


BeforeRequestHook

Properties

PropertyTypeDescription
beforeRequest(hookCtx: HookContext, request: Request) => Awaitable<Request>A hook that is called before the SDK sends a request. The hook can introduce instrumentation code such as logging, tracing and metrics or replace the request before it is sent or throw an error to stop the request from being sent.