@sapiom/fetch
Native fetch() wrapper with automatic payment handling
Installation
Section titled “Installation”npm install @sapiom/fetchFunctions
Section titled “Functions”createFetch
Section titled “createFetch”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 fetchParameters:
| Name | Type | Description |
|---|---|---|
config | SapiomFetchConfig | 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 metadataimport { 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 propertyconst request = new Request('/api/resource', { method: 'POST' });(request as any).__sapiom = { serviceName: 'different-service', actionName: 'custom-action'};await fetch(request);
// Disable Sapiom for specific requestconst publicRequest = new Request('/api/public');(publicRequest as any).__sapiom = { enabled: false };await fetch(publicRequest);Classes
Section titled “Classes”AuthorizationDeniedError
Section titled “AuthorizationDeniedError”Custom error classes
Constructor:
new AuthorizationDeniedError(transactionId: string, endpoint: string, reason: string | undefined)| Parameter | Type | Description |
|---|---|---|
transactionId | string | - |
endpoint | string | - |
reason | string | undefined | - |
AuthorizationTimeoutError
Section titled “AuthorizationTimeoutError”Constructor:
new AuthorizationTimeoutError(transactionId: string, endpoint: string, timeout: number)| Parameter | Type | Description |
|---|---|---|
transactionId | string | - |
endpoint | string | - |
timeout | number | - |
Interfaces
Section titled “Interfaces”SapiomFetchConfig
Section titled “SapiomFetchConfig”Configuration for Sapiom-enabled Fetch client
Extends: BaseSapiomIntegrationConfig