X402 Quick Start

This tutorial uses a minimal API request to illustrate the complete Ace Data Cloud X402 flow. The goal is not to write complex code first, but to first understand: why the first request returns 402, what is in accepts, and how PAYMENT-SIGNATURE turns the same API request into a paid request.

Preparation

You need to prepare:

Item Description
Wallet A wallet that supports the target network. Base uses an EVM wallet, and Solana uses a Solana wallet.
USDC The wallet needs to have sufficient USDC. The actual amount is subject to maxAmountRequired in the 402 response.
Development environment TypeScript recommends Node.js 18+; Python recommends Python 3.10+.
SDK It is recommended to use the official SDK; manually writing signing details is not recommended.

No API Token is required when calling the Ace Data Cloud API with X402. The SDK does not include Authorization in the first request, and the Gateway returns 402 Payment Required and payment requirements; the SDK automatically retries after signing.

Install the SDK

Source code and package addresses:

TypeScript:

npm install @acedatacloud/sdk @acedatacloud/x402-client ethers

Python:

pip install acedatacloud acedatacloud-x402

If you want to use Solana, you also need to install the corresponding dependency:

npm install @solana/web3.js

The Solana signer dependency for the Python version is already included in acedatacloud-x402.

Installation and import check output in a clean temporary environment:

@acedatacloud/sdk@2026.504.2
@acedatacloud/x402-client@2026.531.3
ethers@6.16.0
@solana/web3.js@1.98.4

acedatacloud 2026.4.26.1
acedatacloud-x402 2026.5.31.3
imports_ok True True True True True True
usage: acedatacloud-x402 [-h] {approve-permit2} ...

Result description:

  • Both npm packages and PyPI packages are actually published packages, not placeholder names in the documentation.
  • acedatacloud-x402[cli] installs the CLI, and the approve-permit2 subcommand can be used for Permit2 authorization in upto scenarios.

The First Request Returns 402

You can first use curl to see what an unpaid request returns. The following example does not incur charges because it does not carry PAYMENT-SIGNATURE:

curl -sS -X POST https://x402.acedata.cloud/openai/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "hi"}],
    "max_tokens": 1
  }'

The response body contains the accepts array, with a common structure as follows:

{
  "x402Version": 2,
  "resource": {
    "url": "/openai/chat/completions",
    "description": "AceDataCloud API call",
    "mimeType": "application/json"
  },
  "accepts": [
    {
      "scheme": "exact",
      "network": "eip155:8453",
      "maxAmountRequired": "95215",
      "amount": "95215",
      "maxTimeoutSeconds": 3600,
      "resource": "/openai/chat/completions",
      "description": "...",
      "payTo": "0x...",
      "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
      "extra": {
        "name": "USD Coin",
        "version": "2",
        "chainId": 8453,
        "verifyingContract": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
      }
    }
  ],
  "error": "PAYMENT-SIGNATURE header is required"
}

The same challenge content is also placed in the PAYMENT-REQUIRED response header in base64 form, making it convenient for clients to read payment requirements without parsing the body.

The program output summary for an unpaid request to the production API is as follows:

status=402
x402Version 2
accepts [
  ('eip155:8453', 'exact', '95215'),
  ('eip155:8453', 'upto', '95215'),
  ('solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', 'exact', '95215'),
]

Result description:

  • The first request does not carry Authorization or PAYMENT-SIGNATURE, so it returns HTTP 402 and does not incur charges.
  • accepts is the only trusted signing basis for this request, containing the available networks, schemes, maximum amount, recipient address, and asset address.
  • network is a CAIP-2 identifier, and clients must match by the CAIP-2 string when selecting a network.
  • The maximum amount for this minimal gpt-4o-mini chat request is 95215 atomic USDC, which is 0.095215 USDC.
  • Every request should read the current 402 response; do not hardcode example amounts into business code.

Field meanings:

Field Description
scheme Payment scheme. exact indicates a fixed amount, while upto indicates an authorization limit and settlement based on actual usage.
network The CAIP-2 identifier of the payment network, such as eip155:8453, solana:5eykt4....
maxAmountRequired The maximum payment amount, in USDC atomic units; 95215 indicates 0.095215 USDC.
amount The amount to settle for this request; exact is the same as maxAmountRequired, while upto is rewritten based on actual usage during settlement.
payTo Recipient address.
asset USDC contract address or Solana mint address.
extra Extended information required for signing, such as chain ID, EIP-712 domain, Permit2 address, and so on.

Complete the Payment Retry with the SDK

The following is a minimal TypeScript example. It specifies network: 'base', and the handler selects the Base payment requirement from the current 402 response; the actual amount and recipient address are still subject to accepts:

import { Wallet } from 'ethers';
import { AceDataCloud } from '@acedatacloud/sdk';
import { createX402PaymentHandler } from '@acedatacloud/x402-client';

const wallet = new Wallet(process.env.EVM_PRIVATE_KEY!);

const evmProvider = {
  async request({ method, params }: { method: string; params?: unknown[] }) {
    if (method !== 'eth_signTypedData_v4') {
      throw new Error(`unsupported method: ${method}`);
    }
    const [, typedDataJson] = params as [string, string];
    const typedData = JSON.parse(typedDataJson);
    return wallet.signTypedData(typedData.domain, typedData.types, typedData.message);
  }
};

const client = new AceDataCloud({
  paymentHandler: createX402PaymentHandler({
    network: 'base',
    evmProvider,
    evmAddress: wallet.address
  })
});

const response = await client.openai.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [{ role: 'user', content: 'Reply with exactly: hello' }],
  max_tokens: 8
});

console.log(response.choices[0].message.content);

Four steps occur behind this code:

  1. The SDK sends a normal API request, without Authorization.
  2. The Gateway returns 402 Payment Required and accepts.
  3. createX402PaymentHandler selects the payment requirement for network = 'base' and signs a PAYMENT-SIGNATURE.
  4. The SDK retries with the same request body, and the Gateway calls the Facilitator to verify and settle before allowing the request through to the target API.

View Facilitator capabilities

The X402 API does not depend on a resource catalog. The client directly calls a known API, and uses the 402 Payment Required and accepts returned in real time by that request as the sole basis for pricing and signing.

The Facilitator's capability declaration is located at:

curl https://facilitator.acedata.cloud/.well-known/x402

It only describes /supported, /verify, /settle, and the currently enabled payment networks, and does not list API resources.

Ace Data Cloud's production Facilitator address is:

https://facilitator.acedata.cloud

You can check which networks and schemes it supports:

curl https://facilitator.acedata.cloud/supported

The returned kinds lists the networks and schemes supported by the Facilitator. For actual calls, the accepts returned by the API still takes precedence.

Facilitator /supported output:

kinds [
  ('eip155:8453', 'exact'),
  ('eip155:8453', 'upto', {'facilitatorAddress': '0xd019238EAA8a9Ca13C5792Ca10B4029D6ce25708'}),
  ('solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', 'exact')
]

The results indicate:

  • /supported indicates that the Facilitator has verification and settlement capabilities for these networks and schemes.
  • Both Base and Solana support exact; upto is currently available only on Base.
  • Whether a specific API allows a certain network is still determined by that API's 402 accepts.