LicenRoute

LicenRoute › Guides

HTTP 402 Payment Required: letting AI agents pay for your API

Updated 21 November 2026 · 6 min read

Short version: more and more of your API traffic comes from programs (scripts, other apps and AI agents), not people. When they hit something paid, a login page or a generic error tells them nothing. A 402 with a small JSON body (which plan, what price, where to buy) turns “blocked” into “here's how to get access”.

What 402 means

402 Payment Required has been in HTTP since the beginning, but the specification still says it is “reserved for future use” (RFC 9110, §15.5.3). There is no single standard for what a 402 response must contain, so in practice each API defines its own body. That freedom is the point: you can make it useful.

Why it matters now

402 vs 401 vs 403 vs 429

StatusMeansWhat the client should do
402Access to this resource is sold; you don’t have a valid purchase for itBuy the plan named in the response, then retry with the key
401You are not authenticatedLog in or send credentials
403The credential is invalid or revokedStop; don’t retry with the same key
429Too many requests right nowWait and retry later

What a useful 402 contains

Enough for a program to act without reading your documentation:

HTTP/1.1 402 Payment Required
Content-Type: application/json

{
  "error": "payment_required",
  "request_id": "01a1fe84-…",
  "payment": {
    "plan_id": "api-reports",
    "checkout_url": "https://example.com/plans/api-reports",
    "currency": "EUR",
    "unit_price_minor": 150,
    "unit_size": 1000
  }
}

After buying, the client retries with its key, for example Authorization: Bearer bgat_…. A key for a different plan should get 402 again; an invented or revoked key, 403.

Do it safely

With LicenRoute

LicenRoute’s Express middleware answers exactly that 402 shape on the routes you choose, and its test suite checks every case: no key, valid key, other plan, invented key, quota used up, revoked key and service outage. You set it up from the CLI, or your AI coding assistant does it with your approval.

Honest status: developer preview in demo mode. The checkout link points to a local simulator and nothing is charged; real payments need the hosted service, which isn’t open yet. Express only.

See the 402 on your own machine

Run the 10-minute tutorial on the example app, or paste the prompt into your assistant. Building something agents should pay for? Tell us.

Related guides