DEV Community

Γ‰ric
Γ‰ric

Posted on Originally published at xynaptic.io

πŸ€–πŸ’Έ How an AI Agent Pays for an API Without an API Key (x402 in Practice)

Every API on the internet still works like it's 2010: a human signs up, gets a key, wires a monthly subscription, and pastes the key into a config file. πŸ”‘

That works when the customer is a developer. It breaks completely when the customer is an autonomous agent. An agent cannot create an account, accept terms of service, or enter a credit card for a $0.02 data call. 🀷

This post walks through, step by step, how the x402 protocol fixes it β€” using a live catalog (I run xynaptic.io, 200+ pay-per-call endpoints) so you can reproduce every command. βœ…

πŸšͺ Step 1: The request

bash
curl -i https://api.xynaptic.io/v1/crypto-price

No key, no auth header, no account. What comes back? πŸ‘€

πŸ’° Step 2: The 402 challenge

http
HTTP/1.1 402 Payment Required
X-PAYMENT: version=2, chain=base, max-total=10000 (0.01 USDC),
pay-to=0x4ff893...a4D5e9, resource="crypto-price"

HTTP 402 (Payment Required) has existed since 1998 and nobody used it. πŸ•°οΈ x402 turns it into a machine-readable price tag: the price, the receiving address, and the network β€” all stated before payment, so an agent can compare providers and budget itself. 🏷️

No login wall. No "contact sales". Just a price. ⚑

πŸ”„ Step 3: The agent pays

The agent's x402 client builds a USDC transfer for the exact amount on the stated chain (Base, Polygon or Solana here), then retries the same request with the payment proof attached:

bash
curl https://api.xynaptic.io/v1/crypto-price \
-H "X-PAYMENT: "

The server verifies the payment on-chain, settles it, and returns the data. On xynaptic.io every response is also signed with Ed25519, and every proof is auditable at api.xynaptic.io/trust. πŸ”

🧭 The full flow

GET request β†’ 402 challenge (price, payTo, network)
β†’ on-chain USDC transfer
β†’ retry with proof
β†’ 200 + your data (signed)

Three properties worth noting: πŸ‘‡

  • πŸ’Έ Pay per call, not per month. An agent that makes one $0.01 call pays one cent. No tiers, no minimums, no end-of-month invoice.
  • πŸ” Nothing to leak. No API keys to rotate, no secrets to scrape from a .env file pushed to GitHub.
  • 🧩 It composes. An agent with a wallet can buy data, storage, watch jobs, even skills β€” machine-to-machine, no human in the loop.

πŸ§ͺ Try it yourself

The cheapest way to see it is one curl:

bash
curl -i https://api.xynaptic.io/v1/crypto-price

You will get a 402. That's not an error β€” it's a price tag. πŸ˜‰

If you build agents: is the 402 β†’ pay β†’ retry flow understandable enough? What data domains would you want next? I read every comment. πŸ’¬πŸ‘‡

Top comments (0)