Skip to content
Go To Dashboard

@sapiom/fetch

Native fetch() wrapper with automatic payment handling

Terminal window
npm install @sapiom/fetch

Creates a Sapiom-enabled fetch function with automatic authorization and payment handling

Drop-in replacement for native fetch() with Sapiom capabilities. Works directly with native Request/Response objects, preserving all native fetch features (FormData, Blob, streams, etc.).

Signature:

function createFetch(config?: SapiomFetchConfig | undefined): typeof fetch

Parameters:

NameTypeDescription
configSapiomFetchConfig | undefined-

Returns: typeof fetch - A fetch function with Sapiom payment and authorization handling

Example:

// Simplest usage (reads SAPIOM_API_KEY from environment)
import { createFetch } from '@sapiom/fetch';
const fetch = createFetch();
// Works exactly like native fetch!
const response = await fetch('https://api.example.com/premium-endpoint');
const data = await response.json();

Example:

// With API key and default metadata
import { createFetch } from '@sapiom/fetch';
const fetch = createFetch({
apiKey: 'sk_...',
agentName: 'my-agent',
serviceName: 'my-service'
});
const response = await fetch('https://api.example.com/data');

Example:

// With default metadata (applied to all requests)
import { createFetch } from '@sapiom/fetch';
const fetch = createFetch({
apiKey: 'sk_...',
agentName: 'my-agent',
serviceName: 'my-service'
});
// Per-request override via __sapiom property
const request = new Request('/api/resource', { method: 'POST' });
(request as any).__sapiom = {
serviceName: 'different-service',
actionName: 'custom-action'
};
await fetch(request);
// Disable Sapiom for specific request
const publicRequest = new Request('/api/public');
(publicRequest as any).__sapiom = { enabled: false };
await fetch(publicRequest);

Custom error classes

Constructor:

new AuthorizationDeniedError(transactionId: string, endpoint: string, reason: string | undefined)
ParameterTypeDescription
transactionIdstring-
endpointstring-
reasonstring | undefined-

Constructor:

new AuthorizationTimeoutError(transactionId: string, endpoint: string, timeout: number)
ParameterTypeDescription
transactionIdstring-
endpointstring-
timeoutnumber-

Configuration for Sapiom-enabled Fetch client

Extends: BaseSapiomIntegrationConfig