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
- SDK sends request —
POST /p/demo-api/summarizewith no payment header. - API returns 402 — response includes
exact_splitrequirement withaccepts[].splits[](provider + platform legs). - SDK parses requirement — selects the accept option matching
chain: "sui". - SDK asks wallet to pay — calls
wallet.pay(option, requirement)for an atomic multi-leg USDC transaction. - SDK sends payment proof — encodes proof as base64 JSON in
X-PAYMENTheader. - SDK retries original request — identical method, path, and body.
- API returns response —
200with the endpoint result and optional cost/receipt fields.
Prerequisites
Before running this example you need:
- A connected wallet with USDC on the target chain/network
- A developer-registered endpoint that accepts payments on that chain
Common mistakes
- Changing the request body between retry — the body must be byte-identical or payment verification fails.
- No USDC balance — the wallet transaction will fail before retry.
- Expired requirement — 402 responses include
expiresAt; pay promptly.