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:
- Building a custom HTTP client wrapper
- Integrating with a non-standard request library
- Debugging payment flow behavior
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
- Send request without
X-PAYMENT - If response is not 402, return it
- Parse 402 body as
PaymentRequirement - Call
selectPaymentOption(requirement, chain) - Call
wallet.pay(option, requirement) - Encode proof with
encodePaymentProof(proof) - Retry with
X-PAYMENTheader - Return final response
Helper exports
import {
encodePaymentProof,
decodePaymentProof,
selectPaymentOption,
buildPaymentProof,
} from "@galliun/sdk";
Common mistakes
- Passing object body —
paidFetchexpects a string body.GalliunClient.fetch()JSON-stringifies objects automatically. - Skipping wallet — 402 responses throw if no wallet is configured.