> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Call a paid API

> Call a paywalled endpoint and follow the HTTP 402 flow: request, price, payment, and data, with the output of the real example code at each step.

<div className="sol-stepper not-prose" data-path="paywall-api">
<div className="sol-stepper-nav" role="navigation" aria-label="Paywall an endpoint · ~30 min">
<ol>
<li data-state="current"><a href="/developers/docs/payments/paywall-api/try-it" aria-current="step"><span className="sol-stepper-n">Step 1<span className="sol-vh">: </span></span><span className="sol-stepper-t">Call a paid API</span></a></li>
<li data-state="todo"><a href="/developers/docs/payments/paywall-api/browser"><span className="sol-stepper-n">Step 2<span className="sol-vh">: </span></span><span className="sol-stepper-t">Read the code</span></a></li>
<li data-state="todo"><a href="/developers/docs/payments/paywall-api/server"><span className="sol-stepper-n">Step 3<span className="sol-vh">: </span></span><span className="sol-stepper-t">Add it to your server</span></a></li>
<li data-state="todo"><a href="/developers/docs/payments/paywall-api/go-live"><span className="sol-stepper-n">Step 4<span className="sol-vh">: </span></span><span className="sol-stepper-t">Go live</span></a></li>
</ol>
</div>
</div>

Start as the client. The API below sells tomorrow's forecast for 0.001 USDC per
request. The client is a script with its own wallet of test USDC, so it pays
without a person approving each call. Press **Run** to step through this
tutorial's code on a local test network (Surfpool). Nothing is sent from your
browser, and no real money moves.

<div className="sol-term not-prose" data-term="paths/paywall-api/demo" data-kind="paywall" data-steps="2" data-l-run="Run" data-l-run-step="Run step" data-l-next="Next step" data-l-all="Run all" data-l-again="Run again" data-l-running="Running…" data-l-tx="Sending transaction…" data-l-http="Requesting…" data-l-call="Running…" data-l-confirming="Confirming…" data-l-done="Done" data-l-copied="Copied" data-l-step="Step">
<div className="sol-term-head"><span className="sol-term-title">Paid forecast API · 0.001 USDC per request</span><span className="sol-term-env">Local test network (Surfpool)</span><button type="button" className="sol-term-copy" data-act="copy">Copy</button></div>
<div className="sol-term-stage" data-stage="paywall">
<div className="sol-term-item"><span className="sol-term-item-label">Request</span><span className="sol-term-pill" data-at="0" data-tone="todo">GET /api/forecast</span><span className="sol-term-pill" data-at="1" data-tone="info">GET /api/forecast</span><span className="sol-term-pill" data-at="2" data-tone="info" data-final="">GET /api/forecast + X-PAYMENT</span></div>
<div className="sol-term-item"><span className="sol-term-item-label">Response</span><span className="sol-term-pill" data-at="0" data-tone="todo">Waiting for a request</span><span className="sol-term-pill" data-at="1" data-tone="warn">402 Payment Required · 0.001 USDC</span><span className="sol-term-pill" data-at="2" data-tone="ok" data-final="">200 OK · Sunny, 24°C</span></div>
</div>
<ol className="sol-term-plan">
<li data-step="1"><span className="sol-term-plan-n" aria-hidden="true">1</span><span className="sol-term-plan-body"><strong className="sol-term-plan-title">Call the API without paying</strong> <span className="sol-term-plan-desc">The server answers *402 Payment Required* with the price (1000 base units, 0.001 USDC) and the address to pay.</span></span></li>
<li data-step="2"><span className="sol-term-plan-n" aria-hidden="true">2</span><span className="sol-term-plan-body"><strong className="sol-term-plan-title">Pay and call again</strong> <span className="sol-term-plan-desc">The client signs a 0.001 USDC payment and sends the same request again with it in the `X-PAYMENT` header. The server settles the payment onchain and answers with the forecast.</span></span></li>
</ol>
<div className="sol-term-screen" role="region" tabIndex="0" aria-label="Paid forecast API · 0.001 USDC per request: Output">
<div className="sol-term-step" data-step="1" data-pending="http">
<div className="sol-term-row sol-term-note">\# Step 1/2 · Call the API without paying</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await fetch(url)</div>
<div className="sol-term-row sol-term-ln" data-tone="warn">GET /api/forecast -\> <span className="sol-term-code" data-code="4xx">402</span> X-PAYMENT header is required</div>
<div className="sol-term-row sol-term-ln" data-indent="2"><span className="sol-term-k">price\:</span> 1000 base units of <span className="sol-h" title="4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU"><span className="sol-h-f">4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU</span><span className="sol-h-s" aria-hidden="true">4zMM…ncDU</span></span> (0.001 USDC)</div>
<div className="sol-term-row sol-term-ln" data-indent="2"><span className="sol-term-k">payTo\:</span> <span className="sol-h" title="4SnM7PENj3Amhyc6rRA5XG2rq3NpiyZbPvpgAQbA3FLE"><span className="sol-h-f">4SnM7PENj3Amhyc6rRA5XG2rq3NpiyZbPvpgAQbA3FLE</span><span className="sol-h-s" aria-hidden="true">4SnM…3FLE</span></span> <span className="sol-term-k">network\:</span> solana\:<span className="sol-h" title="EtWTRABZaYq6iMfeYKouRu166VU2xqa1"><span className="sol-h-f">EtWTRABZaYq6iMfeYKouRu166VU2xqa1</span><span className="sol-h-s" aria-hidden="true">EtWT…xqa1</span></span></div>
<div className="sol-term-result"><span className="sol-term-badge" data-tone="warn">402 Payment Required</span></div>
</div>
<div className="sol-term-step" data-step="2" data-pending="http">
<div className="sol-term-row sol-term-note">\# Step 2/2 · Pay and call again</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> const header = await createPaymentHeader(required, \{ signer\: client, maxAmount\: 5\_000n \})</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> `await fetch(url, { headers: { "X-PAYMENT": header } })`</div>
<div className="sol-term-row sol-term-ln" data-tone="ok">GET /api/forecast (paid) -\> <span className="sol-term-code" data-code="2xx">200</span> `{"city":"Lisbon","tomorrow":"Sunny, 24°C"}`</div>
<div className="sol-term-row sol-term-ln" data-indent="2">settled <span className="sol-term-k">signature\:</span> <span className="sol-h" title="4JZW7fB3h53qP6f66eat4cwDVDFvxwTtKWbV8tfJJHVo4eHzK9riSvetQSwPEtungFxfbWC968JoyDoqRo7XauLZ"><span className="sol-h-f">4JZW7fB3h53qP6f66eat4cwDVDFvxwTtKWbV8tfJJHVo4eHzK9riSvetQSwPEtungFxfbWC968JoyDoqRo7XauLZ</span><span className="sol-h-s" aria-hidden="true">4JZW…auLZ</span></span></div>
<div className="sol-term-row sol-term-ln" data-indent="2"><span className="sol-term-k">fee\:</span> 5000 lamports</div>
<div className="sol-term-result"><span className="sol-term-badge" data-tone="ok">200 OK · paid 0.001 USDC</span></div>
</div>
</div>
<div className="sol-term-success"><span className="sol-term-success-icon" aria-hidden="true">✓</span><span>Paid 0.001 USDC and got the forecast. No signup, no API key.</span></div>
<div className="sol-term-foot"><button type="button" className="sol-term-btn" data-act="run">Run step 1</button><button type="button" className="sol-term-btn" data-act="all" data-variant="secondary">Run all</button><span className="sol-term-status"></span><span className="sol-term-sr" role="status"></span></div>
</div>

What happened:

- The first call got `402 Payment Required`. The body listed the price as
  `1000` base units of USDC (USDC has 6 decimals, so that is 0.001 USDC), the
  USDC mint address, the address to pay (`payTo`), and the network.
- The client signed a USDC transfer for exactly that amount and sent the same
  request again with the signed transfer in the `X-PAYMENT` header.
- The server sent the transfer to the network, waited for it to confirm, and
  answered `200` with the forecast. The network fee for that
  <span className="sol-chip" data-concept="transaction"><Tooltip headline="Transactions" tip="A transaction is a signed request to change the shared ledger. It either fully happens or doesn't happen at all, and once confirmed anyone can look it up." cta="Learn more" href="/developers/docs/core/transactions">transaction</Tooltip><span className="nojs-only"> ([Transactions](/developers/docs/core/transactions): A transaction is a signed request to change the shared ledger. It either fully happens or doesn't happen at all, and once confirmed anyone can look it up.)</span></span> was
  5,000 lamports (0.000005 SOL).

The addresses and signatures exist only on the local network. The `network`
field still shows the devnet ID (`solana:EtWT…xqa1`), the default `NETWORK` in
`x402.ts`.

## Requests that don't get the forecast

The server and the client both refuse payments that don't fit. The first
request resends the `X-PAYMENT` header from the paid request above. The second
comes from a client capped below the price.

<div className="sol-term not-prose" data-term="paths/paywall-api/checks" data-kind="paywall" data-steps="2" data-l-run="Run" data-l-run-step="Run step" data-l-next="Next step" data-l-all="Run all" data-l-again="Run again" data-l-running="Running…" data-l-tx="Sending transaction…" data-l-http="Requesting…" data-l-call="Running…" data-l-confirming="Confirming…" data-l-done="Done" data-l-copied="Copied" data-l-step="Step">
<div className="sol-term-head"><span className="sol-term-title">Requests that don't get the forecast</span><span className="sol-term-env">Local test network (Surfpool)</span><button type="button" className="sol-term-copy" data-act="copy">Copy</button></div>
<ol className="sol-term-plan">
<li data-step="1"><span className="sol-term-plan-n" aria-hidden="true">1</span><span className="sol-term-plan-body"><strong className="sol-term-plan-title">Send the same payment again</strong> <span className="sol-term-plan-desc">The same `X-PAYMENT` header a second time gets 402: one payment buys one response.</span></span></li>
<li data-step="2"><span className="sol-term-plan-n" aria-hidden="true">2</span><span className="sol-term-plan-body"><strong className="sol-term-plan-title">Call with a cap below the price</strong> <span className="sol-term-plan-desc">A client that pays at most 500 base units (0.0005 USDC) sees the 0.001 USDC price and signs nothing.</span></span></li>
</ol>
<div className="sol-term-screen" role="region" tabIndex="0" aria-label="Requests that don't get the forecast: Output">
<div className="sol-term-step" data-step="1" data-pending="http">
<div className="sol-term-row sol-term-note">\# Step 1/2 · Send the same payment again</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> `await fetch(url, { headers: { "X-PAYMENT": header } })`</div>
<div className="sol-term-row sol-term-ln" data-tone="error">GET /api/forecast (replayed X-PAYMENT) -\> <span className="sol-term-code" data-code="4xx">402</span> payment already used</div>
<div className="sol-term-result"><span className="sol-term-badge" data-tone="error">Refused\: payment already used · 402</span></div>
</div>
<div className="sol-term-step" data-step="2" data-pending="http">
<div className="sol-term-row sol-term-note">\# Step 2/2 · Call with a cap below the price</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await fetchWithPayment(url, \{ signer\: client, maxAmount\: 500n \})</div>
<div className="sol-term-row sol-term-ln" data-tone="error">client refused\: price 1000 is above maxAmount 500; not paying</div>
<div className="sol-term-result"><span className="sol-term-badge" data-tone="error">Refused by the client\: price above maxAmount</span></div>
</div>
</div>
<div className="sol-term-foot"><button type="button" className="sol-term-btn" data-act="run">Run step 1</button><button type="button" className="sol-term-btn" data-act="all" data-variant="secondary">Run all</button><span className="sol-term-status"></span><span className="sol-term-sr" role="status"></span></div>
</div>

<div className="sol-concept not-prose">
[<span className="sol-concept-name">Wallets & signatures</span> <span className="sol-concept-def">A wallet holds a key pair: a public address that anyone can pay, and a private key that signs approvals and never leaves the device.</span>](/developers/docs/core/wallets)
</div>

<div className="sol-concept not-prose">
[<span className="sol-concept-name">Transactions</span> <span className="sol-concept-def">A transaction is a signed request to change the shared ledger.</span>](/developers/docs/core/transactions)
</div>

## Why 402?

HTTP has reserved status code 402 "Payment Required" since 1997 without
defining how to pay. [x402](https://www.x402.org/) is an open protocol that fills that
gap: the 402 response lists the payments the server accepts, and the client
retries with a signed payment in a header. On Solana the network fee is a
fraction of a cent, so a price of a tenth of a cent still works.
