Documentation Index
Fetch the complete documentation index at: https://docs.prudra.dev/llms.txt
Use this file to discover all available pages before exploring further.
How x402 works
x402 uses ERC-3009transferWithAuthorization — an EIP that lets a wallet owner sign a token transfer authorization off-chain. The signature authorizes moving tokens from the signer’s wallet, but the transaction is submitted by a third party (in this case, Prudra’s settlement service). The agent signs; Prudra moves the funds.
The ERC-3009 authorization
An ERC-3009transferWithAuthorization contains these fields, all signed together:
| Field | Value |
|---|---|
from | Agent’s wallet address |
to | Server’s receiving wallet address (from the payment option) |
value | Token amount in base units |
validAfter | Unix timestamp — signature not valid before this time |
validBefore | Unix timestamp — signature expires at this time (typically now + 5 minutes) |
nonce | Random 32-byte value — prevents replay |
v, r, s). Prudra encodes the signature plus all authorization fields as a base64 string and puts it in the PAYMENT-SIGNATURE header.
The full flow
What Prudra verifies
WhenpayMiddleware receives a PAYMENT-SIGNATURE header, it calls POST /payments/verify which performs these checks:
- Decode — base64-decode the credential, parse the JSON
- Signature verification — ecrecover the signer address from the EIP-712 signed data. Must match the
fromfield. - Expiry —
validBeforemust be in the future. The credential is valid for ~5 minutes after signing. - Nonce uniqueness — the nonce is checked against a UNIQUE constraint in Postgres. The same nonce cannot be used twice, even if the signature itself is valid.
- Amount — the signed
valuemust be at least the required price converted to token base units. - Recipient — the
toaddress must match the server’s registered wallet address.
settlementToken is returned. Settlement (the actual on-chain transaction) happens asynchronously.
The PAYMENT-RESPONSE header
On a successful 200 response, Prudra adds aPAYMENT-RESPONSE header containing a base64-encoded JSON object:
settlementPending is true, the on-chain transaction hasn’t been submitted yet. The agent can poll or use a webhook to confirm settlement. In practice, settlement happens within seconds.
Replay protection
The nonce field in the ERC-3009 signature is enforced at the Postgres level with a UNIQUE constraint on thenonce column in the Payment table. Application-level uniqueness checks have race conditions — two concurrent requests with the same signature can both pass before either DB write commits. The database constraint closes this window atomically.
See Replay attack protection for details.
Related
- Add x402 to an endpoint — configure payMiddleware options
- Test x402 payments — the full test script from example-06
- Handle the payment response — decode PAYMENT-RESPONSE
- Replay attack protection — how nonce uniqueness prevents replay
- Dual-protocol payments — how challenges are built atomically

