Skip to Content
px402Devnet
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

OptionRequiredMeaning
wallet, journal, capsyesFrom the calls above
networkyes"devnet"
maxPriceBaseyesPrice ceiling, independent of caps
allowedOriginsto payUnset or empty refuses every payment
userAgentnoSent on every request. Default px402
maxInFlightPaymentsnoTransfers built at once. Default 4
claimGraceSecondsnoHow long past the nonce expiry to retry a claim. Default 120
retryno{ baseDelayMs, maxDelayMs }. Default 500 and 5000
signalnoStops before the transfer, or between claim retries. Never between signing and sending
followRedirectsnofalse returns any 3xx. A request carrying a payment never follows a redirect
fetch, now, sleepnoFor tests

What happens on a failure

CaseWhat the SDK does
A local limit refusesThrows AgentError. Nothing was sent
TRANSFER_NOT_FOUND, HELIUS_UNAVAILABLEResends the same claim with backoff
UPSTREAM_FAILED, CLAIM_IN_PROGRESSResends the same signature until the server’s retry.until
A final rejection after paymentReturns the response; the journal says rejected
Aborted or still unresolved at the deadlineThe 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.