Introduction
Welcome to the PayAgents developer documentation. PayAgents is an all-in-one payment routing API and SDK built to power the machine-to-machine agentic economy.
As software agents evolve to complete complex workflows—such as querying external data, training LLMs, or executing web actions—they frequently hit paywalls. Traditional subscription models and credit cards fail for automated agent systems because they require interactive manual checks, incur high fraud risks, and charge expensive fees for tiny, high-frequency actions.
Why Machine-Native Payments?
Autonomous AI agents require low-latency, friction-free transactions. PayAgents handles this by routing value instantly using micropayment rails, enabling agents to pay for only what they consume, down to a fraction of a cent.
Key Protocols Supported
- L402 (Bitcoin Lightning network): A protocol that marries Lightning payment invoices with HTTP headers. Ideal for extremely high-frequency micropayments settled in SATs.
- x402 (Base Stablecoins): Standardized stablecoin payments on Coinbase's Layer-2 Base network. Perfect for stable, dollar-denominated agentic utilities with low network gas.
Unified Wallet Framework
Developers using PayAgents do not need to run their own node clusters, manage private keys, lock liquidity in payment channels, or worry about swapping base assets to pay transaction gas. In the background, our core engine auto-swaps and routes funds, exposing a unified USD balance. You deposit and withdraw standard currency, and we route the payments across whatever rails the counterparty demands.
Getting Started
Integrate transactional controls into your workspace in less than five minutes. Our SDK wraps all transaction formatting, signature generation, and verification.
1. Install the SDK
Add the PayAgents package to your project repository:
2. Obtain an API Key
Go to the PayAgents Dashboard, log in or create an account, and generate a new key inside the **API Settings** tab. Always store this key in your environment variables:
3. Initialize the Client
Import and instantiate the PayAgents client in your logic files:
Receiving Payments
Monetize your custom APIs and agentic services by gating endpoints behind a paywall. The SDK offers middleware utilities that automatically issue payment challenges and intercept verification hashes.
Cross-Rail Gating
When gating an API, you only configure the target USD fee. PayAgents automatically formats the response headers so that agents requesting via L402 receive Lightning invoices, while agents requesting via x402 receive EVM stablecoin challenges. The settlement arrives in your USD balance sheet regardless.
Non-Custodial Direct Settlement (Zero Platform Custody)
By default, incoming payments accumulate in your PayAgents USD balance ledger. If you prefer zero platform custody, you can configure PayAgents to route all payments directly into your personal crypto wallet or Lightning node.
you@getalby.com). When configured, all HTTP 402 challenges will instruct paying agents to transfer funds directly to your addresses peer-to-peer.
For a complete step-by-step walkthrough and architectural explanation, read our Non-Custodial Direct Receiving Guide.
Gating HTTP API Endpoints
Below is an integration example demonstrating how to wrap a Node.js Express server endpoint using the PayAgents middleware helper:
How the Gating Protocol Works
- Initial Unpaid Request: An AI agent calls `/api/v1/extract` without headers.
- Challenge Issued: The PayAgents middleware intercepts and responds with an `HTTP 402 Payment Required` status, attaching metadata containing payment instructions (Lightning invoice preimage details or EVM signature parameters).
- Settlement: The caller agent automatically pays the invoice or stablecoin payload via their own PayAgents client.
- Fulfillment Request: The caller retries the original API request, appending the verification token or preimage hash inside the `Authorization` header.
- Verification: The middleware parses the headers, validates the preimage through the PayAgents network, settles the balance, and passes execution to your route handler.
Enabling Spending
Give your AI agents the capability to pay for external resources autonomously using the `pay()` API wrapper. The PayAgents engine handles payment rails and checks balance limits locally.
Executing a Payment
To let your agent purchase gated data or tool actions, pass the destination URL and policy bounds directly to the SDK client:
Spend Protection against Hallucinations
AI agents can occasionally hallucinate or enter recursive calling loops that drain corporate wallets in seconds. It is highly recommended to set strict transaction caps and monthly limits in your PayAgents dashboard to prevent budget overrun.
Dashboard Spend Policies
Through your developer console, you can customize limits for every API key:
- Single-Payment Cap: Intercept and abort any transaction exceeding a specific threshold (e.g. max $0.05 per action).
- Daily/Monthly Quotas: Restrict cumulative daily or monthly spend bounds.
- Domain Allowlists: Ensure keys are only allowed to spend balances on verified API URLs.
- Human Approval Gateways: Require manual authentication in your dashboard for transactions above a specified cost (e.g. any charge above $1.00 triggers an email/Slack approval webhook).
Model Context Protocol (MCP)
The Model Context Protocol (MCP) is the open standard that connects LLMs (such as Claude Desktop, Cursor, Windsurf, and custom OpenAI/Gemini agents) to external tools and data sources. PayAgents provides a dedicated, official package payagents-mcp that delivers full spending and receiving capabilities.
1. Claude Desktop, Cursor & Custom Agent Client Setup (Sending)
Give your AI assistants an autonomous, policy-guarded wallet in 30 seconds. Add PayAgents to your claude_desktop_config.json, .cursor/mcp.json, or custom agent runtime:
Your LLM will automatically discover 3 built-in tools:
payagents_pay_url: Probes HTTP 402 paywalls, selects the cheapest rail (Lightning or Base USDC), pays the fee, and returns unlocked data.payagents_get_balance: Checks the remaining USD budget and active enterprise spending policy caps.payagents_get_transaction: Fetches transaction details and cryptographic settlement proofs.
2. Monetizing Custom MCP Tools (Receiving)
If you build an MCP server, wrap your tool handlers with mcpCharge to collect micro-fees directly into your self-custodial wallet with zero platform custody: