Quickstart Guide

Start making gasless payments on ApeChain in less than 5 minutes.

Prerequisite: You need a wallet with USDC on ApeChain (Chain ID: 33139).

1. Install the SDK

Install our TypeScript SDK and viem for wallet interactions.

npm install @x402apechain/sdk viem

2. Setup the Client

Initialize the FacilitatorClient and AgentPaymentClient with your wallet.

import { createPublicClient, createWalletClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import {
  FacilitatorClient,
  AgentPaymentClient,
  APECHAIN,
  APECHAIN_USDC,
  DEFAULT_FACILITATOR_URL,
  DEFAULT_SETTLEMENT_ADDRESS,
  toTokenUnits
} from '@x402apechain/sdk';

// Setup viem clients
const account = privateKeyToAccount(process.env.PRIVATE_KEY);
const publicClient = createPublicClient({ 
  chain: APECHAIN, 
  transport: http() 
});
const walletClient = createWalletClient({ 
  account, 
  chain: APECHAIN, 
  transport: http() 
});

// Create facilitator client
const facilitator = new FacilitatorClient({
  baseUrl: DEFAULT_FACILITATOR_URL,
  chainId: APECHAIN.id,
  settlementAddress: DEFAULT_SETTLEMENT_ADDRESS,
  defaultToken: APECHAIN_USDC
});

// Create agent payment client
const agent = new AgentPaymentClient({
  facilitator,
  publicClient,
  walletClient
});

3. Make a Payment

Create an invoice and pay. The SDK handles permit signing, verification, and settlement automatically.

const receipt = await agent.pay({
  invoice: {
    chainId: APECHAIN.id,
    tokenAddress: APECHAIN_USDC,
    recipient: '0xRecipientAddress...',
    amount: toTokenUnits('1.00', 6) // 1.00 USDC
  }
});

console.log('Payment settled:', receipt.txHash);

How It Works

The X402 protocol enables gasless payments through EIP-2612 permits:

  1. Sign Permit - Your wallet signs an off-chain permit authorizing the transfer.
  2. Verify - The facilitator verifies the signature, balance, and nonce.
  3. Settle - The facilitator submits the transaction on-chain (paying gas for you).
Zero Gas Fees: You never pay gas - the facilitator handles all on-chain execution.

Next Steps