Skip to content
Go To Dashboard

Fetch API

The @sapiom/fetch package creates a Sapiom-enabled fetch function with automatic payment handling and authorization.


Terminal window
npm install @sapiom/fetch

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 fetch
const response = await fetch('https://api.example.com/data');
const data = await response.json();

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
});
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


Override configuration for individual requests using the __sapiom property:

// Disable Sapiom for a specific request
const publicRequest = new Request('/api/public');
(publicRequest as any).__sapiom = { enabled: false };
await fetch(publicRequest);
// Override metadata for a specific request
const request = new Request('/api/resource', { method: 'POST' });
(request as any).__sapiom = {
serviceName: 'different-service',
actionName: 'custom-action',
traceExternalId: 'ext-789'
};
await fetch(request);

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);
}

Automatically reads from environment:

  • SAPIOM_API_KEY (required)
  • SAPIOM_BASE_URL or SAPIOM_API_URL (optional)
  • SAPIOM_TIMEOUT (optional, in milliseconds)