Example Code
JavaScript: Generic paid endpoint helper
async function callPaidEndpoint(url: string, init?: RequestInit) {
const first = await fetch(url, init)
if (first.status !== 402) return first
const challengeHeader = first.headers.get("PAYMENT-REQUIRED")
if (!challengeHeader) {
throw new Error("Missing PAYMENT-REQUIRED header on 402 response")
}
const paymentRequired = JSON.parse(
Buffer.from(challengeHeader, "base64").toString("utf8"),
)
// Build/sign this payload with your wallet stack (EIP-3009 transferWithAuthorization)
const paymentPayload = await buildAndSignX402Payload(paymentRequired)
const paymentSignature = Buffer.from(
JSON.stringify(paymentPayload),
"utf8",
).toString("base64")
return fetch(url, {
...init,
headers: {
...(init?.headers ?? {}),
"PAYMENT-SIGNATURE": paymentSignature,
},
})
}JavaScript: wallet -> privy-id -> user-data -> chat
cURL: chat by world alias
Last updated

