SDK
Paying fetch
createPaidFetch pays a 402 and retries the claim until it is served.
import { join } from "node:path";
import {
createPaidFetch, loadAgentSdkConfig, openAgentWallet, openJournal, profileDir, readStoredCaps, resolveCaps,
} from "@px402-tech/sdk";
const config = loadAgentSdkConfig(); // PX402_* from process.env
const caps = resolveCaps(config, readStoredCaps(config.PX402_HOME, config.PX402_PROFILE));
const wallet = await openAgentWallet({ config, passphrase: config.PX402_PASSPHRASE! });
const journal = openJournal({ path: join(profileDir(config.PX402_HOME, config.PX402_PROFILE), "journal.sqlite"), caps });
const paidFetch = createPaidFetch({
wallet,
journal,
caps,
network: config.PX402_NETWORK,
maxPriceBase: config.PX402_MAX_PRICE_BASE,
allowedOrigins: config.PX402_ALLOWED_ORIGINS,
});
try {
const res = await paidFetch("https://devnet.x402.px402.tech/paid/demo");
console.log(res.status, await res.text());
} finally {
journal.close();
wallet.close();
} This example reads the passphrase from PX402_PASSPHRASE. A route that does not answer 402 is returned as it is.
Options
| Option | Required | Meaning |
|---|---|---|
wallet, journal, caps | yes | From the calls above |
network | yes | "devnet" |
maxPriceBase | yes | Price ceiling, independent of caps |
allowedOrigins | to pay | Unset or empty refuses every payment |
userAgent | no | Sent on every request. Default px402 |
maxInFlightPayments | no | Transfers built at once. Default 4 |
claimGraceSeconds | no | How long past the nonce expiry to retry a claim. Default 120 |
retry | no | { baseDelayMs, maxDelayMs }. Default 500 and 5000 |
signal | no | Stops before the transfer, or between claim retries. Never between signing and sending |
followRedirects | no | false returns any 3xx. A request carrying a payment never follows a redirect |
fetch, now, sleep | no | For tests |
What happens on a failure
| Case | What the SDK does |
|---|---|
| A local limit refuses | Throws AgentError. Nothing was sent |
TRANSFER_NOT_FOUND, HELIUS_UNAVAILABLE | Resends the same claim with backoff |
UPSTREAM_FAILED, CLAIM_IN_PROGRESS | Resends the same signature until the server’s retry.until |
| A final rejection after payment | Returns the response; the journal says rejected |
| Aborted or still unresolved at the deadline | The journal keeps submitted: unresolved, not lost |
It never sends a second transfer for the same nonce. A new call is a new nonce and a new payment, so do not blindly call again after a failure: check the journal first.
Use it with your own x402 client
createPrivateTransferScheme is the private-transfer client scheme on its own, for @x402/core. createPaidFetch
adds the claim retries, journal outcomes and the redirect protection, so prefer it.