Skip to content
x402 is an open payment protocol for internet resources. A resource server returns 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:
DirectionHeaderValue
Server to clientPAYMENT-REQUIREDA PaymentRequired object
Client to serverPAYMENT-SIGNATUREA signed PaymentPayload
Server to clientPAYMENT-RESPONSEThe verification and settlement result
x402 V2 Flowx402 V2 Flow

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:
NetworkIdentifier
Mainnetsolana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp
Devnetsolana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1
These CAIP-2 identifiers are protocol identifiers, not the full value returned by the Solana 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 Solana upto implementation.
Scheme support is operational, not only syntactic. Before advertising a (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:
Set a Solana recipient and an x402 facilitator URL, then protect one route:
server.ts
Start the server with your recipient wallet and a facilitator. On devnet you can use the public facilitator at https://x402.org/facilitator. It supports the exact scheme on Solana devnet and pays the network fee when it settles a payment:
An ordinary request now receives a V2 payment requirement:
The facilitator must list 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:
The client signs with a disposable devnet keypair, which it reads as a base58 secret key from SOLANA_PRIVATE_KEY. Create one with the Solana CLI and encode it (run this in the project folder, where @scure/base is installed):
Send devnet USDC to the client’s address (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
With the server running, pay for one request:
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 Solana exact 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.
Use the reference SDK or a conforming implementation rather than writing a partial transaction verifier.

​Next steps

​Protocol references