paidFetch is the low-level function that implements the 402 → pay → retry flow. GalliunClient.fetch() uses it internally.

When to use paidFetch

Use paidFetch directly when:

For most applications, use GalliunClient.fetch() instead.

Usage

import { paidFetch, paidFetchJson } from "@galliun/sdk";

const response = await paidFetch({
  url: "https://api.galliun.com/p/demo/summarize",
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ text: "..." }),
  chain: "sui",
  wallet: suiWallet,
  hooks: {
    onStatusChange: (status) => console.log(status),
  },
});

const data = await paidFetchJson<{ summary: string }>({
  url: "https://api.galliun.com/p/demo/summarize",
  method: "POST",
  body: JSON.stringify({ text: "..." }),
  chain: "sui",
  wallet: suiWallet,
});

Options

Option Required Description
url Yes Full request URL
chain For paid routes Chain for payment selection
wallet For paid routes Wallet adapter
method No HTTP method (default GET)
headers No Request headers
body No Request body string
fetch No Custom fetch fn
hooks No Payment flow callbacks
authToken No Bearer token

Flow

  1. Send request without X-PAYMENT
  2. If response is not 402, return it
  3. Parse 402 body as PaymentRequirement
  4. Call selectPaymentOption(requirement, chain)
  5. Call wallet.pay(option, requirement)
  6. Encode proof with encodePaymentProof(proof)
  7. Retry with X-PAYMENT header
  8. Return final response

Helper exports

import {
  encodePaymentProof,
  decodePaymentProof,
  selectPaymentOption,
  buildPaymentProof,
} from "@galliun/sdk";

Common mistakes

Next steps