TypeScript SDK

The @agentpay/sdk package provides a drop-in HTTP client that intercepts 402 Payment Required responses, calls the AgentPay API to perform the payment, and retries the original request with the returned payment signature. Your code uses the same interface as a standard fetch wrapper (e.g. get, post) while payment handling is handled automatically.

Installation

npm install @agentpay/sdk
# or
pnpm add @agentpay/sdk
yarn add @agentpay/sdk

Basic usage

import { createAgentPayClient } from "@agentpay/sdk";

const client = createAgentPayClient({
  apiKey: process.env.AGENTPAY_API_KEY!,
  baseUrl: "https://api.agentpay.solutions",
});

const res = await client.get("https://paid-api.example/resource");
const data = await res.json();

// If the upstream API returns 402, the SDK:
// 1. Calls POST baseUrl/v1/pay with the payment-required header and URL
// 2. Retries the request with the returned paymentSignature (e.g. PAYMENT-SIGNATURE header)
// 3. Returns the final response (200) or throws on failure

Configuration

createAgentPayClient accepts an options object with apiKey (required) and baseUrl (required). Use environment variables in production so keys are never committed. For self-hosted or local development, set baseUrl to your API URL (e.g. http://localhost:3000).

API surface

The client exposes methods that mirror common HTTP verbs (e.g. get, post, put, patch, delete). Each method returns a Response (or equivalent) so you can call .json(), .text(), etc. When the server responds with 402, the SDK performs the payment flow and retries; if payment or retry fails, it throws or returns the failing response so you can handle errors.

402 flow summary

On 402, the SDK decodes the payment-required header, sends it with the request URL to POST /v1/pay, and retries the original request with the paymentSignature in the appropriate header. No additional code is required in your agent logic. For a full description of the flow, see 402 Flow.

Next steps

See the Quickstart for a full example, and the API Reference for the underlying endpoints. For Python, use the Python SDK.