This guide walks through the shortest working example: calling a protected API endpoint with automatic x402 payment handling.

Example

import { GalliunClient } from "@galliun/sdk";

const client = new GalliunClient({
  baseUrl: "https://api.galliun.com",
  chain: "sui",
  wallet: suiWallet,
});

const response = await client.fetch("/p/demo-api/summarize", {
  method: "POST",
  body: JSON.stringify({
    text: "Summarize this article...",
  }),
});

const data = await response.json();
console.log(data);

Replace suiWallet with a configured Sui wallet adapter. The same pattern works for Solana.

What happens under the hood

  1. SDK sends requestPOST /p/demo-api/summarize with no payment header.
  2. API returns 402 — response includes exact_split requirement with accepts[].splits[] (provider + platform legs).
  3. SDK parses requirement — selects the accept option matching chain: "sui".
  4. SDK asks wallet to pay — calls wallet.pay(option, requirement) for an atomic multi-leg USDC transaction.
  5. SDK sends payment proof — encodes proof as base64 JSON in X-PAYMENT header.
  6. SDK retries original request — identical method, path, and body.
  7. API returns response200 with the endpoint result and optional cost/receipt fields.

Prerequisites

Before running this example you need:

Common mistakes

Next steps