In this quickstart, you will deposit USDC into a Gateway Wallet, pay for an x402-protected resource without gas fees, and check your balance. By the end, you’ll have a working client that can make gasless payments to any x402-compatible API that supports Circle Gateway.Documentation Index
Fetch the complete documentation index at: https://developers.circle.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
Before you begin, ensure you have:- Installed Node.js v22+
- An EOA (externally owned account) wallet private key for signing transactions and payment authorizations.
- Obtained testnet USDC from the Circle Faucet.
- Testnet ETH (or native gas token) for the one-time deposit transaction.
Step 1. Set up your project
1.1. Create the project and install dependencies
1.2. Configure TypeScript (optional)
Create atsconfig.json file:
tsconfig.json file:
1.3. Set environment variables
Open.env in your editor and add:
PRIVATE_KEYis the private key for the EOA you use to deposit USDC and sign nanopayment authorizations.
npm run pay command loads variables from .env using Node.js native
env-file support.
Step 2: Initialize the client
Create a new filepay.ts and initialize the GatewayClient with your chain
and private key:
pay.ts
chain parameter determines which blockchain the client connects to for
deposits and withdrawals. See the
SDK reference for all supported chain
names.
Step 3: Deposit USDC into Gateway
Before you can make gasless payments, deposit USDC from your wallet into the Gateway Wallet contract. This is a one-time onchain transaction:pay.ts
getBalances() calls the
Get Token Balances API
endpoint. The deposit itself is an onchain transaction and does not use the
Gateway API.
After the deposit confirms, your Gateway balance can be used for gasless
payments to any supported seller. See the discussion at
Fast deposits about
increasing deposit speeds.
Step 4: Pay for a resource
Add the payment logic topay.ts. Call client.pay() with the URL of an
x402-protected resource. The client handles the full payment flow automatically:
- Sends the initial request to the URL.
- Receives the
402 Payment Requiredresponse with payment details. - Signs an EIP-3009 authorization offchain (zero gas).
- Retries the request with the
PAYMENT-SIGNATUREheader.
pay.ts
pay() negotiates the 402 flow and submits the payment
through the Settle x402 Payment
API endpoint.
Step 5: Check your balance
Add balance checking after the payment using the Get Token Balances API endpoint:pay.ts
Step 6: Run the script
Run the complete script:Step 7: Withdraw funds (optional)
You can withdraw USDC from Gateway back to your wallet at any time. Same-chain withdrawals are instant:pay.ts
pay.ts
Crosschain withdrawals require native gas tokens on the destination blockchain
to cover the minting transaction.
Check support before paying
Before attempting a payment, you can verify that the target URL supports Gateway batching. Thesupports() method requests the target URL, checks for a 402
response, and inspects the PAYMENT-REQUIRED header for a compatible Gateway
batching option: