HTTP Headers

stripe402 uses three custom HTTP headers to negotiate payments. All header values are JSON objects encoded as base64 strings.

Header Summary

Header Name
Direction
HTTP Status
Content

payment-required

Server → Client

402

Payment requirements (price, currency, Stripe key)

payment

Client → Server

Any

Payment payload (PaymentMethod ID or client ID)

payment-response

Server → Client

200

Payment result (client ID, remaining credits)

The header names are defined as constants in @stripe402/core:

import { HEADERS } from '@stripe402/core'

HEADERS.PAYMENT_REQUIRED  // 'payment-required'
HEADERS.PAYMENT           // 'payment'
HEADERS.PAYMENT_RESPONSE  // 'payment-response'

Encoding

All headers use base64-encoded JSON. This follows the x402 conventionarrow-up-right and keeps the response body free for application data (e.g., an HTML paywall on 402, or the actual resource on 200).

import { encodeHeader, decodeHeader } from '@stripe402/core'

// Encoding: object → JSON → base64
const encoded = encodeHeader({ amount: 500, currency: 'usd' })
// => 'eyJhbW91bnQiOjUwMCwiY3VycmVuY3kiOiJ1c2QifQ=='

// Decoding: base64 → JSON → object
const decoded = decodeHeader<{ amount: number; currency: string }>(encoded)
// => { amount: 500, currency: 'usd' }

Internally, encoding uses Buffer.from(JSON.stringify(data)).toString('base64') and decoding uses JSON.parse(Buffer.from(header, 'base64').toString('utf-8')).

Header 1: payment-required

Direction: Server → Client When: Included in every 402 response Also: The same data is sent as the JSON response body

Schema: PaymentRequiredResponse

ResourceInfo

PaymentRequirements

Example 402 Response

The error Field

When present, the error field indicates why the 402 was sent:

Value
Meaning

(absent)

Initial 402 challenge — client has not attempted payment yet

insufficient_credits

Client ID was recognized but balance is too low for this request

Header 2: payment

Direction: Client → Server When: Included in retry requests after receiving a 402

Schema: PaymentPayload

Usage Patterns

New payment (first request or top-up):

Using existing credits (subsequent requests):

Top-up with known client ID (replenishing credits):

Header 3: payment-response

Direction: Server → Client When: Included in successful 200 responses after payment or credit deduction

Schema: PaymentResponse

Example Success Response

Decoded payment-response header:

The clientId should be stored by the client and included in future requests to use credits without a new payment.

Last updated