DEV Community

Xynaptic
Xynaptic

Posted on Edited 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)