| title | HTTP interceptors |
|---|---|
| description | Callback hooks for intercepting and modifying HTTP requests and responses. Pass an `SDKHooks` instance to `GustoProvider` via `config.hooks`. |
| sidebar_position | 12 |
| generated_by | typedoc |
| custom_edit_url |
Callback hooks for intercepting and modifying HTTP requests and responses. Pass an SDKHooks instance to GustoProvider via config.hooks.
Request interceptors for customizing HTTP requests and responses.
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.
| Stage | When it runs |
|---|---|
beforeCreateRequest |
Before the Request object is constructed (URL / method changes) |
beforeRequest |
After the Request is created but before it is sent (headers, auth tokens) |
afterSuccess |
After a successful response (2xx) |
afterError |
After an error response (4xx, 5xx) or network failure |
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>| Property | Type | Description |
|---|---|---|
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 |
AfterErrorContext =
HookContext&object
| Property | Type | Description |
|---|---|---|
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 =
HookContext&object
| Property | Type | Description |
|---|---|---|
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 =
HookContext&object
| Property | Type | Description |
|---|---|---|
beforeCreateRequest |
(hookCtx: HookContext, input: RequestInput) => RequestInput |
A 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 =
HookContext&object
| Property | Type | Description |
|---|---|---|
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. |