---
title: "Verify an HTTP 402 payment before doing · Agent Wranglers"
url: https://agentwranglers.se/playbook/usage-billing/agentic-payments/http-402-payments
id: USG-24
level: Optional
applies: payments, public-api
updated: 2026-10-22
tokens: 1030
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Verify an HTTP 402 payment before doing the work

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Agentic payments](https://agentwranglers.se/playbook/usage-billing/agentic-payments.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: USG-24
- Level: Optional
- Chapter: [Agentic payments](https://agentwranglers.se/playbook/usage-billing/agentic-payments.md)
- Rule: If you charge per request with HTTP 402, as x402 and MPP do, settle each payment through a provider before the work runs. Refuse any payment proof used twice.
- Check: A test calls a paid endpoint without proof and gets 402 with a price. A valid proof gets 200 once. The same proof sent again gets 402, and the work runs once.
- Applies when: The project takes payments, or offers an API to others.
- How: The server answers an unpaid request with 402 and the price. x402 puts it in a `PAYMENT-REQUIRED` header. MPP sends a challenge in `WWW-Authenticate: Payment`. The client pays and repeats the request with its proof. Hand the proof to your payment provider or the protocol's verifying service. Do the work only after it confirms. Store each proof's ID under a unique constraint, so a replay is refused. Record a usage event for each paid request, as [Give every usage event a key and count it once](https://agentwranglers.se/playbook/usage-billing/metering/idempotent-usage-events.md) says. Many of these payments settle in stablecoins, so ask your accountant how to book them.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
app.get('/api/reports/:id', async (req, res) => {
  const proof = req.get('PAYMENT-SIGNATURE')
  if (!proof) return res.status(402).set('PAYMENT-REQUIRED', paymentRequirements(req)).end()
  const payment = await paymentVerifier.verifyAndSettle(proof, paymentRequirements(req))
  if (!payment.ok || !(await payments.claimOnce(payment.id))) return res.status(402).end()
  res.json(await reports.build(req.params.id))
})
```

### Python example

```python
@app.get("/api/reports/{report_id}")
def get_report(report_id: str, request: Request):
    proof = request.headers.get("PAYMENT-SIGNATURE")
    if proof is None:
        return Response(status_code=402, headers={"PAYMENT-REQUIRED": payment_requirements(request)})
    payment = payment_verifier.verify_and_settle(proof, payment_requirements(request))
    if not payment.ok or not payments.claim_once(payment.id):
        return Response(status_code=402)
    return reports.build(report_id)
```

## Sources

- [x402 version 2](https://www.x402.org/writing/x402-v2-launch)
- [The "Payment" HTTP Authentication Scheme, IETF draft](https://datatracker.ietf.org/doc/draft-httpauth-payment/)

## Related rules

- USG-22 [Track the agent payment protocols before you build on one](https://agentwranglers.se/playbook/usage-billing/agentic-payments/agent-payment-protocols.md) in Usage-based billing
- USG-1 [Give every usage event a key and count it once](https://agentwranglers.se/playbook/usage-billing/metering/idempotent-usage-events.md) in Usage-based billing

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/usage-billing/agentic-payments/http-402-payments">Agent Wranglers usage-based billing playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Let agents buy through the same order flow as people](https://agentwranglers.se/playbook/usage-billing/agentic-payments/agent-checkout.md)
- Next: [Keep the buyer's signed approval with each agent order](https://agentwranglers.se/playbook/usage-billing/agentic-payments/agent-mandates.md)
- [Agentic payments](https://agentwranglers.se/playbook/usage-billing/agentic-payments.md)
