Skip to Content
Paycose Docs
Features

Crypto payments (x402)

Accept USDC on Base for x402 machine payments. Paycose gives you deposit addresses and holds the USDC; you record each payment, and refunds and payouts go out on-chain.

x402 is a way to charge for an HTTP request. Your server answers 402 Payment Required with a price and an address to pay. The client, often an AI agent, pays in USDC and sends the request again with proof of payment.

Paycose gives you the address to be paid at. It is a deposit address that Paycose creates and controls, so Paycose holds the USDC for you and you never manage keys. You run the x402 middleware and a facilitator yourself. After each payment you tell Paycose which transaction paid you, and Paycose turns it into a payment intent, checks it on-chain, adds it to your balance, and sends refunds and payouts in USDC.

Today Paycose takes USDC on Base. USDC amounts are in base units with 6 decimals: 1000000 is 1 USDC. GET /crypto/networks lists the networks open to your key, with each network's USDC contract and explorer.

Accept crypto at checkout

Crypto is also an ordinary checkout method. Your payer picks Crypto at checkout, like any other method, and you need no x402 server:

  1. Create the payment intent in your own currency, for example ฿500 in thb. Crypto is offered alongside your other methods with no setup.
  2. The payer picks the coin (for example USDC, USDT or ETH) and, when the coin runs on more than one network, the network. Paycose converts the amount to that coin at the current rate and holds the price for 30 minutes for a stablecoin or 15 minutes for a coin whose price moves. The payer sees the coin amount, the price in your currency (≈ ฿500.00), the rate, a QR code and a deposit address made for this payment only.
  3. Paycose sees the transfer and the payment intent succeeds. You are credited in the coin the payer sent, less the Paycose fee, in a balance of that coin. You can keep it, refund from it, or pay it out to your own crypto address.
  • Close enough counts. A transfer up to 1% short of the amount pays it. A larger shortfall waits for a top-up until the price expires. Anything more is paid and noted on the charge as payment_method.crypto.overpaid_token_amount.
  • Late or short transfers are not lost. USDC that arrives after the price expired, or that never reached the amount, is recorded for you as its own payment an hour later, so you can refund it.
  • Refunds go back in the coin the payer sent, in proportion to what arrived.
  • The price risk is yours. A coin like ETH or BTC can be worth more or less by the time you pay it out.
  • The charge's payment_method.crypto adds token_currency, exchange_rate (your currency per coin) and received_token_amount.
  • Connected accounts. A payment intent with transfer_data[destination] gets a deposit address owned by that connected account. The coins stay in your balance and the account's share is recorded as owed to it, as for any destination charge. You can also create standing addresses for a connected account with account on POST /crypto/deposit_addresses.

Coins and networks

NetworkCoinsAddress format
Base, Ethereum, ArbitrumUSDC, USDT (not Base), ETH0x…, one address for every EVM network
PolygonUSDC, USDT0x…
TronUSDT (TRC-20), TRXT…
SolanaUSDC, USDT, SOLbase58
BitcoinBTCbc1… (tb1… in test mode)

A Bitcoin payment counts once it has the network's required confirmations, so it can take longer than the price hold; a transfer seen before the price expired keeps its price. In test mode the same networks run on their testnets. GET /crypto/networks lists what your key can use, with each coin's contract and decimals.

The rest of this page covers x402: you take the payment yourself and record it with Paycose.

1. Create a deposit address

Create a deposit address on the base network. Pass customer to tie every payment to that address to one of your customers.

curl "$WALLET/crypto/deposit_addresses" \
  -H "Authorization: Bearer ${SECRET_KEY}" \
  -H "X-Country: TH" \
  -H "Content-Type: application/json" \
  -d '{ "network": "base" }'
{
  "id": "cda_3f8a2c1e9b7d4a6c8e0f2b4d6a8c0e1f",
  "object": "crypto.deposit_address",
  "address": "0x5b2C3a9E8f1D4c7B6a0E2d9F8c1B3a5D7e9F0a2C",
  "network": "base",
  "customer": null,
  "metadata": null,
  "livemode": false
}
  • Addresses are static. An address never expires and takes any number of payments. Each request creates a new address, so create one per service or per customer, store it, and reuse it.
  • Paycose holds the USDC. The address is derived from Paycose's keys. USDC sent to it is moved into Paycose's treasury and counted in your balance once you record it.
  • List your addresses or retrieve one. Every USDC transfer Paycose sees into them is listed under deposits.

2. Use it as payTo in your x402 server

Put the address in your x402 payment middleware as the address to pay, and price your routes in USDC on Base. Any facilitator works, for example Coinbase's CDP facilitator. This Hono server charges $0.01 for /weather and records each settled payment with Paycose:

import { Hono } from 'hono';
import { paymentMiddleware } from 'x402-hono';
import { facilitator } from '@coinbase/x402';
import { recordPayment } from './paycose';

const app = new Hono();
const payTo = process.env.PAYCOSE_DEPOSIT_ADDRESS; // the address from step 1

// Runs after the payment middleware: X-PAYMENT-RESPONSE carries the settled transaction.
app.use('/weather', async (c, next) => {
  await next();
  const header = c.res.headers.get('X-PAYMENT-RESPONSE');
  if (!header) return;
  const settled = JSON.parse(Buffer.from(header, 'base64').toString('utf8'));
  if (settled.success) {
    recordPayment(settled.transaction, 1).catch((err) => console.error(err)); // 1 cent
  }
});

app.use(
  paymentMiddleware(payTo, { '/weather': { price: '$0.01', network: 'base' } }, facilitator),
);

app.get('/weather', (c) => c.json({ report: 'sunny', temperature: 31 }));

export default app;

The facilitator checks the client's payment and puts the transfer on-chain; Paycose plays no part in that step. Paycose comes in once the USDC has reached your deposit address. @coinbase/x402 reads your CDP API key from CDP_API_KEY_ID and CDP_API_KEY_SECRET.

3. Record the payment

After a payment settles, create a payment intent that names the transaction. Paycose checks that the transaction sent that much USDC to one of your deposit addresses.

ParameterValue
amountWhat the client paid, in currency's smallest unit
currencyusd (cents) or usdc (base units)
confirmtrue
payment_method_data[type]crypto
payment_method_options[crypto][mode]transaction_verification
payment_method_options[crypto][transaction_verification_options][network]base
payment_method_options[crypto][transaction_verification_options][transaction_hash]The 0x… hash of the settled transaction

You can also send customer_id, description, metadata and labels as on any payment intent. Without customer_id, a payment to an address created for a customer belongs to that customer.

curl "$WALLET/payment_intents" \
  -H "Authorization: Bearer ${SECRET_KEY}" \
  -H "X-Country: TH" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": 1,
    "currency": "usd",
    "confirm": true,
    "payment_method_data": { "type": "crypto" },
    "payment_method_options": {
      "crypto": {
        "mode": "transaction_verification",
        "transaction_verification_options": {
          "network": "base",
          "transaction_hash": "0x9c2f6d1e8a7b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4e3f2a1b0c9d"
        }
      }
    }
  }'
  • The amount must match the transfer exactly. With usd, the amount in cents times 10,000 must equal the USDC base units sent: $0.01 is 1 cent and 10,000 base units. For a price below one cent, use usdc and the base units, for example "amount": 5000, "currency": "usdc" for 0.005 USDC.
  • One payment intent per transaction. Recording the same transaction again returns the same payment intent, so retries are safe. Only after that payment intent failed does a new record open a new one.
  • Which balance. A payment in usd goes to your USD balance, and a payment in usdc to your USDC balance. Crypto payouts are paid from the USDC balance.
  • The charge shows the transfer under payment_method.crypto: network, transaction_hash, buyer_address, deposit_address, token_amount and a transaction_url on the block explorer.

4. Statuses

StatusMeaning
processingRecorded. Paycose is waiting to see the transfer on-chain, or for screening to finish.
succeededThe transfer reached your deposit address with the right amount and passed screening. The money is added to your balance like any other payment, and you receive payment_intent.succeeded.
requires_payment_methodThe payment failed and nothing was added to your balance. You receive payment_intent.payment_failed.

The payment fails, with payment_intent.payment_failed and the reason in the charge's failure_code, when:

  • transaction_not_found: the transfer did not show up within the claim timeout (15 minutes).
  • amount_mismatch: the transfer sent a different amount than you recorded.
  • deposit_held: the sender failed sanctions screening.

To try again, record the transaction again with the right details. That opens a new payment intent, and the failed one stays as it is.

If Paycose has already seen the transfer when you record it, a wrong amount is refused straight away with E4872.

Nothing is lost if you forget to record. A deposit nobody records within 24 hours is recorded for you in usdc, with payment_method.crypto.auto_recorded: true. Paycose already holds that USDC, so it always reaches your balance.

Sanctions screening

In live mode, Paycose checks the sender of every deposit against a sanctions list before the money counts. A transfer from a sanctioned address is held: it is not added to your balance, recording it returns E4874, and a payment intent recorded before the check fails with deposit_held. Paycose's compliance team reviews held transfers. If one is cleared, you can record it again, or it is recorded for you. Test mode skips screening.

Refunds

Refund a crypto payment as any other, with payment_intent or charge and an optional amount. Paycose sends the USDC on-chain once the refund is submitted, and the refund succeeds when the transfer confirms.

Where the USDC goes:

  • If the payer signed an EIP-3009 authorization for the payment (what x402 clients do), the refund goes back to the address that paid.
  • Otherwise the sender may be an exchange's shared address, so Paycose will not guess. Pass crypto[destination_address], or the refund is refused with E4876.
curl "$WALLET/refunds" \
  -H "Authorization: Bearer ${SECRET_KEY}" \
  -H "X-Country: TH" \
  -H "Content-Type: application/json" \
  -d '{
    "payment_intent": "pi_7d1c9e3a5b2f4c8e0a6d4b2f9e1c3a5d",
    "crypto": { "destination_address": "0x8E1b3D5f7A9c2E4b6D8f0A1c3E5b7D9f2A4c6E8b" }
  }'

Payouts in USDC

Pay out your USDC balance to an address you control. Each payout address has a waiting period, so an address added by someone who stole a login cannot receive money at once.

Register a payout address with network, address and an optional label. It starts pending, and the account owner is emailed straight away. In the Dashboard, adding or removing an address asks for a two-factor code.

curl "$WALLET/crypto/payout_destinations" \
  -H "Authorization: Bearer ${SECRET_KEY}" \
  -H "X-Country: TH" \
  -H "Content-Type: application/json" \
  -d '{ "network": "base", "address": "0x2A4c6E8b0D1f3A5c7E9b1D3f5A7c9E1b3D5f7A9c", "label": "Treasury" }'

After 24 hours (available_at), the address becomes active. A payout to a pending address is refused with E4878.

Create a payout from your USDC balance with crypto_payout_destination instead of bank_account_id. The amount is in USDC base units.

curl "$WALLET/payouts" \
  -H "Authorization: Bearer ${SECRET_KEY}" \
  -H "X-Country: TH" \
  -H "Content-Type: application/json" \
  -d '{
    "account": "acct_1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d",
    "currency": "usdc",
    "amount": 25000000,
    "crypto_payout_destination": "cpd_4e6a8c0e2b4d6f8a1c3e5b7d9f0a2c4e"
  }'

The payout goes through approval like any other, then Paycose sends the USDC on-chain. It is paid when the transfer confirms.

Crypto payouts pause (E4881) while Paycose's latest check that it holds enough USDC on-chain has failed or is too old. Try again later; nothing is sent in the meantime.

Errors

Crypto errors use codes E4867 to E4886. Each one is listed with its meaning in the error reference.

NumberCodeWhen
E4867crypto.custody.unavailableCrypto deposits are not set up for your account yet.
E4868crypto.network.unsupportedThe network is not open to your key. Check GET /crypto/networks.
E4869crypto.deposit_address.not_foundNo deposit address has this id.
E4870crypto.keystore.failedThe address could not be created right now. Try again shortly.
E4871crypto.transaction.not_foundThe transaction sent no USDC to your deposit addresses.
E4872crypto.transaction.amount_mismatchThe amount does not match the USDC the transaction sent.
E4873crypto.currency.unsupportedcurrency is neither the coin nor the currency a stablecoin tracks (usd for USDC and USDT).
E4874crypto.transaction.heldThe transfer is held by sanctions screening.
E4875crypto.transaction.invalidtransaction_hash is not a 0x hash of 32 bytes.
E4876crypto.refund.destination_requiredThe refund needs crypto[destination_address].
E4877crypto.address.invalidThe address is not a 0x address of 20 bytes.
E4878crypto.payout_destination.pendingThe payout address is still in its waiting period.
E4879crypto.payout_destination.not_foundNo payout address has this id.
E4880crypto.deposit.not_foundNo deposit has this id.
E4881crypto.payouts.pausedCrypto payouts are paused until Paycose's reserve check passes again.
E4882crypto.payout_destination.duplicateThe address is already one of your payout addresses on this network.
E4883crypto.checkout.currency_unsupportedCrypto can't be priced in the checkout currency right now.
E4884crypto.account.not_connectedaccount is not one of your connected accounts.
E4885crypto.asset.unsupportedThe coin isn't accepted on that network.
E4886crypto.price.unavailableThe coin's price isn't available right now. Try again or pick another coin.

Testing on Base Sepolia

With a test key (sk_test_…), the base network is Base Sepolia, a test network with test USDC. Live keys use Base itself.

  • Get test USDC for the paying side from Circle's faucet, choosing Base Sepolia.
  • In your x402 middleware, use network: 'base-sepolia' and a testnet facilitator, such as { url: 'https://x402.org/facilitator' }. In Paycose requests, keep network: "base".
  • GET /crypto/networks returns the chain id (eip155:84532), the USDC contract and the explorer for your key's mode.
  • Sanctions screening is skipped in test mode.

Let Paycose settle to your own wallet

You can also skip the deposit address and your own facilitator. Your server keeps the x402 paywall, Paycose checks each payment and sends it on-chain, and the USDC goes from the payer to your own wallet. Paycose never holds it, pays the gas, and takes its fee from your platform credit or, if your account is set to, on-chain (see below).

This is open on test keys only, on Base Sepolia.

  1. Register the receiving wallet as a payout destination on base. It works once its cooling-off period has passed.
  2. Add the paywall to your Next.js API routes with @paycose/sdk-nextjs:
import { createPaywall, withPaycose } from '@paycose/sdk-nextjs';

const paywall = createPaywall({
  apiKey: process.env.PAYCOSE_API_KEY!,
  gatewayUrl: process.env.PAYCOSE_API_URL!,
  country: 'TH',
  network: 'eip155:84532',
  payTo: '0xYourWallet',
  routes: { '/api/v1/report': { price: '0.01', currency: 'USDC' } },
});

export const GET = withPaycose(paywall, async () => Response.json({ report: '…' }));

The client gets 402 Payment Required, signs a USDC transfer with no gas, and sends the request again. Your handler runs after the payment is verified, and the payment settles only if the handler answers below 400, so an agent is never charged for a failed response. Any other x402 library can use <API base>/x402 as its facilitator with your secret key and X-Country: /x402/supported, /x402/verify and /x402/settle.

Each settled payment appears as a crypto payment intent in USDC, and its charge sends the usual webhooks.

How the fee is taken

Paycose sets this for your account:

  • From platform credit. Payers pay your wallet, and the fee is drawn from your platform credit.
  • On-chain. Payers pay your wallet's fee splitter. It keeps Paycose's fee and sends you the rest in the same transaction, so no platform credit is needed. The charge shows the fee in crypto.onchain_fee_amount.

@paycose/sdk-nextjs asks GET /x402/pay_to which address to put in each 402 and accepts payments signed for either, so a change of setting never fails a payment already signed. With another x402 library, call it every few minutes and use its pay_to.

  • settlement_pending means the outcome isn't known yet, not that it failed. Keep the resource back. If the transfer lands, the charge's webhook tells you.
  • A repeated signature answers replayed: true. Don't serve the resource for it again.
  • The payer can cancel an authorization until the transfer lands, so settle before you hand over anything you can't take back.
  • Only signatures from ordinary wallets are accepted today, not smart contract wallets.
  • The money is already yours, so it isn't added to your Paycose balance, and refunds through Paycose aren't available. Send any refund from your own wallet.

Works with

On this page