Set Up Agentic Payments (X402)
Paywall any API endpoint using the x402 protocol for machine-to-machine payments.
Set Up Agentic Payments (X402)
This guide walks through adding x402 pay-per-use access to your API endpoints using Bag as the Merchant of Record.
Prerequisites
- A Bag account with KYB approved (or sandbox mode for testing)
- An API key (
bag_test_sk_*for sandbox,bag_live_sk_*for production) - A payment link with agentic proxy enabled
Step 1: Create an agentic payment link
Create a payment link and enable the agentic proxy option. This tells Bag to serve this link as an x402 resource.
- Go to Payment Links in the dashboard
- Click Create New Link
- Set the amount (this is the per-request price)
- Toggle Enable Agentic Proxy (x402)
- Set the Target URL — the API endpoint that should be called after payment
- Save the link
curl -X POST https://getbags.app/api/payment-links \
-H "Authorization: Bearer $BAG_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Premium API Access",
"amount": 0.05,
"network": "base",
"isAgenticProxy": true,
"targetUrl": "https://yourapi.com/v1/premium-data"
}'Note the payment link ID — you'll need it for the x402 endpoint URL.
Step 2: Point clients to the x402 endpoint
Your x402-protected resource is available at:
https://getbags.app/api/agent/{paymentLinkId}When a client makes a request without payment:
curl https://getbags.app/api/agent/a1b2c3d4-e5f6-7890-abcd-ef1234567890The server returns HTTP 402 with payment instructions in the response headers, following the x402 protocol.
Step 3: Client pays and accesses the resource
x402-compatible clients (including AI agents) automatically:
- Read the 402 response and payment requirements
- Send USDC payment on-chain
- Include the payment proof in a retry request
- Receive the protected resource
The payment is verified by the x402 facilitator (https://x402.org/facilitator), and the request is proxied to your targetUrl.
Step 4: Handle webhooks
Each x402 payment generates a payment.completed webhook with the customer identified as "X402 Agent":
{
"event": "payment.completed",
"data": {
"sessionId": "...",
"amount": 0.05,
"network": "base",
"customerName": "X402 Agent",
"txHash": "0x..."
}
}Testing in Sandbox
- Use a
bag_test_sk_*API key - Create a payment link on a testnet (e.g.,
base_sepolia) - Use testnet USDC to make x402 payments
- Verify webhooks arrive with
"livemode": false
Supported HTTP Methods
The x402 agent endpoint supports all standard HTTP methods:
| Method | Use case |
|---|---|
GET | Read data, fetch resources |
POST | Create resources, submit data |
PUT | Replace resources |
PATCH | Partial updates |
DELETE | Remove resources |
All methods go through the same payment verification flow.