402 Payment Required with acceptable
payment options, and a compatible client signs one option and retries the
request.
This guide uses x402 V2. V1 integrations used headers such as X-PAYMENT and
network names such as solana-devnet. Do not use those V1 fields in a new
integration.
The Paywall an endpoint tutorial is the
exception on purpose: it builds the V1 flow (X-PAYMENT) by hand on
@solana/kit, so you can see every check a server makes. Its
go-live step moves that server to the V2
SDK described here.
To exercise the complete flow first, follow
Let an agent pay (x402)
and make one payment against a local sandbox API.
Protocol roles
x402 separates the HTTP application from settlement infrastructure:- The client requests a resource, selects a supported payment requirement, signs a payment payload, and retries.
- The resource server describes the resource and its accepted payments. It fulfills the request only after verification succeeds.
- The facilitator optionally verifies payment payloads and submits transactions. A resource server can also self-facilitate.
HTTP V2 flow
The HTTP transport carries base64-encoded protocol objects in three headers:| Direction | Header | Value |
|---|---|---|
| Server to client | PAYMENT-REQUIRED | A PaymentRequired object |
| Client to server | PAYMENT-SIGNATURE | A signed PaymentPayload |
| Server to client | PAYMENT-RESPONSE | The verification and settlement result |
x402 V2 Flow
The protocol objects are transport data. Do not parse or assemble the encoded headers with string concatenation; use an x402 client or server library.Solana networks and assets
x402 V2 identifies networks with CAIP-2 identifiers:| Network | Identifier |
|---|---|
| Mainnet | solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp |
| Devnet | solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1 |
getGenesisHash RPC method. A mainnet RPC returns
5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d. When checking that an RPC is
connected to mainnet, compare its response with that full genesis hash, then
configure x402 with the mainnet CAIP-2 identifier in the table above.
Solana requirements identify the SPL or Token-2022 mint in asset, the
recipient in payTo, and the amount in token base units. If you accept a price
string such as "$0.01", the server library resolves it to its configured
default stablecoin.
The main Solana schemes are:
exact: The client authorizes the exact amount in the requirement. Use it for fixed-price resources and per-request API calls.upto: The client authorizes a maximum and the seller settles the actual usage up to that limit. Use it only when the client, server, and facilitator all support the Solanauptoimplementation.
(scheme, network) pair, confirm that your resource server and facilitator can
verify and settle that pair in production.
Build an Express resource server
Create a project and install the x402 V2 packages."type": "module" lets the
scripts on this page use top-level await:
server.ts
https://x402.org/facilitator. It supports
the exact scheme on Solana devnet and pays the network fee when it settles a
payment:
exact support for the configured Solana network at
its /supported endpoint (curl https://x402.org/facilitator/supported) before
a client can complete this example.
Before advertising the route, create the recipient’s associated token account
for the accepted mint. Provision it idempotently during setup and decide which
account funds its rent. A facilitator may sponsor settlement transaction fees,
but that does not imply that it will create the recipient account or fund its
rent.
This script creates the devnet USDC account for SOLANA_PAY_TO with the
idempotent create instruction, so running it again changes nothing. The Solana
CLI keypair at ~/.config/solana/id.json pays the rent; fund it with devnet
SOL from faucet.solana.com:
create-recipient-account.ts
Build a payment-aware client
Install the fetch client and Solana signer dependencies.@x402/svm requires
@solana/kit 8 or later:
SOLANA_PRIVATE_KEY. Create one with the Solana CLI and encode
it (run this in the project folder, where @scure/base is installed):
solana-keygen pubkey client.json)
from the Circle faucet, choosing Solana Devnet. The
client needs no SOL: the facilitator pays the transaction fee.
The client below accepts only devnet USDC requirements up to 0.01 USDC
(10,000 base units) before registering the Solana scheme:
client.ts
Use a constrained signer in productionAn environment key is acceptable only for a disposable development account.
Production agents should receive a signing interface with per-payment and
cumulative limits, not raw private-key material.
Verification requirements
For the Solanaexact scheme, verification includes more than checking that a
transaction has a valid signature. The verifier must enforce the expected
instruction layout, amount, mint, recipient associated token account, transfer
authority, compute-budget limits, and fee-payer restrictions.
Also verify that:
- The payload matches the selected requirement and the requested resource.
- The network and asset are allowlisted by your service.
- The authorization is unexpired and has not been replayed.
- Settlement reaches the confirmation level required before fulfillment.
- A retry cannot settle or fulfill the same purchase twice.
Next steps
Tutorial: Paywall an endpoint
Build a paywalled endpoint step by step on a local network, with every
payment check visible.
Let an agent pay (x402)
Complete one x402 V2 payment against a local sandbox API without moving real
funds.
Become an x402 Facilitator
Compare managed, self-hosted, and in-process verification and settlement.
Use x402 with Kora
Run a reference facilitator with Solana transaction policy and fee
sponsorship provided by Kora.