Fetch API
The @sapiom/fetch package creates a Sapiom-enabled fetch function with automatic payment handling and authorization.
Installation
Section titled “Installation”npm install @sapiom/fetchpnpm add @sapiom/fetchyarn add @sapiom/fetchBasic Setup
Section titled “Basic Setup”Create a Sapiom-enabled fetch function:
import { createFetch } from '@sapiom/fetch';
const fetch = createFetch({ apiKey: process.env.SAPIOM_API_KEY});
// Drop-in replacement for native fetchconst response = await fetch('https://api.example.com/data');const data = await response.json();Configuration
Section titled “Configuration”All configuration options for createFetch:
import { createFetch } from '@sapiom/fetch';
const fetch = createFetch({ // Optional - Automatically uses process.env.SAPIOM_API_KEY if not provided apiKey: process.env.MY_CUSTOM_SAPIOM_KEY,
// Optional - Control enabled: true, // Enable Sapiom handling (default: true) failureMode: 'open', // 'open' | 'closed' (default: 'open') // 'open': Allow requests if Sapiom fails (prioritizes availability) // 'closed': Block requests if Sapiom fails (prioritizes security)
// Optional - Default metadata (applied to all requests) agentName: 'my-agent', // Agent identifier agentId: 'agent-123', // Agent UUID or numeric ID serviceName: 'my-service', // Service name for transactions traceId: 'trace-xyz', // Internal trace UUID traceExternalId: 'ext-456', // External trace identifier});Configuration Options
Section titled “Configuration Options”apiKey string required Your Sapiom API key. Can also be set via SAPIOM_API_KEY environment variable
enabled boolean default: true Enable Sapiom handling. When disabled, requests pass through without Sapiom processing
failureMode 'open' | 'closed' default: 'open' Control behavior when Sapiom operations fail:
'open': Allow requests to proceed (prioritizes availability)'closed': Block requests (prioritizes security)
agentName string Agent identifier for tracking and attribution
agentId string | number Agent UUID or numeric ID
serviceName string Service name for transactions
traceId string Internal trace UUID for request correlation
traceExternalId string External trace identifier for integration with external systems
Per-Request Overrides
Section titled “Per-Request Overrides”Override configuration for individual requests using the __sapiom property:
// Disable Sapiom for a specific requestconst publicRequest = new Request('/api/public');(publicRequest as any).__sapiom = { enabled: false };await fetch(publicRequest);
// Override metadata for a specific requestconst request = new Request('/api/resource', { method: 'POST' });(request as any).__sapiom = { serviceName: 'different-service', actionName: 'custom-action', traceExternalId: 'ext-789'};await fetch(request);Payment Handling
Section titled “Payment Handling”The wrapper automatically handles 402 Payment Required responses:
import { createFetch } from '@sapiom/fetch';
const fetch = createFetch({ apiKey: process.env.SAPIOM_API_KEY});
try { // Automatically handles 402 payment flows const response = await fetch('https://api.example.com/premium-endpoint'); const data = await response.json(); console.log(data);} catch (error) { // Payment errors will be handled by Sapiom console.error('Request failed:', error);}Environment Variables
Section titled “Environment Variables”Automatically reads from environment:
SAPIOM_API_KEY(required)SAPIOM_BASE_URLorSAPIOM_API_URL(optional)SAPIOM_TIMEOUT(optional, in milliseconds)