All TypeScript types and interfaces defined in @stripe402/core. These are the data structures that flow through the stripe402 protocol.
Protocol Messages
PaymentRequiredResponse
Server → Client. Included in the payment-required header and body of every 402 response.
interface PaymentRequiredResponse {
stripe402Version: number
resource: ResourceInfo
accepts: PaymentRequirements[]
error?: string
}
Field
Type
Required
Description
Protocol version. Currently 1.
Describes the resource being requested.
Array of accepted payment options. Currently always contains one element with scheme: 'stripe'.
Error code indicating why the 402 was sent. Values: undefined (initial challenge), 'insufficient_credits' (balance too low).
Describes the resource being requested.
interface ResourceInfo {
url: string
description?: string
mimeType?: string
}
Field
Type
Required
Description
The requested URL path (e.g., '/api/weather').
Human-readable description of the resource.
Expected MIME type of the response.
PaymentRequirements
A single accepted payment option within a 402 response.
Field
Type
Required
Description
Payment rail identifier. Always 'stripe'.
ISO 4217 currency code (e.g., 'usd').
Cost of this request in units (1 unit = 1/10,000 of a dollar). Example: 500 = $0.05.
Minimum top-up charge in units. Example: 50000 = $5.00.
Stripe publishable key for client-side card tokenization via Stripe.js.
Human-readable description of what the payment is for.
Client → Server. Included in the payment header of retry requests.
Field
Type
Required
Description
Protocol version. Must match server's version.
Stripe PaymentMethod ID from client-side tokenization (e.g., 'pm_...'). Required for new payments.
Client identifier for credit balance lookups. Used for subsequent requests after initial payment.
Amount to top up in units. Must be >= minTopUp. Defaults to minTopUp if not specified.
PaymentResponse
Server → Client. Included in the payment-response header of successful responses.
Field
Type
Required
Description
Whether the payment or deduction succeeded.
Stripe PaymentIntent ID. Only present on new payments (not credit deductions).
Remaining credit balance in units after this request.
Client identifier for future requests. Store this and include in subsequent requests.
Human-readable error message (when success is false).
Machine-readable error code (when success is false).
Persistence Types
A client record stored in the persistence layer.
HMAC-derived client identifier (64-char hex string).
Stripe Customer ID (e.g., 'cus_...').
Current credit balance in units.
ISO 4217 currency code (e.g., 'usd').
When the client record was created.
When the client record was last modified.
TransactionRecord
A transaction log entry for audit purposes.
Unique transaction ID (UUID).
Whether credits were added or spent.
Stripe PaymentIntent ID (top-ups only).
Route key, e.g., 'GET /api/joke' (deductions only).
When the transaction occurred.
Persistence store interface. Implementations must ensure atomic balance operations.
Get a client record by ID, or null if not found.
Create a new client record.
deductBalance(clientId, amount)
Atomically deduct from balance. Returns new balance, or null if insufficient funds. Must be atomic.
addBalance(clientId, amount)
Add to balance. Returns the new balance.
recordTransaction(transaction)
Record a transaction for audit logging. Optional — stores that don't support this can omit the method.
See Store Interface for implementation details.
Server Configuration
Configuration for a single paid route.
Field
Type
Required
Default
Description
Cost per request in units (1 unit = 1/10,000 of a dollar).
Minimum top-up amount in units.
Human-readable description shown in 402 responses and Stripe charge descriptions.
Stripe402ServerConfig
Full middleware configuration.
Field
Type
Required
Description
Stripe secret key (sk_test_... or sk_live_...).
Stripe publishable key (pk_test_... or pk_live_...). Sent to clients in 402 responses.
HMAC key for deriving client IDs from card fingerprints.
Persistence store instance (e.g., RedisStore, PostgresStore).
Record<string, RouteConfig>
Map of "METHOD /path" to route configuration. Example key: 'GET /api/weather'.
Client Configuration
OnPaymentRequired
Callback invoked when a 402 response requires payment.
Parameters:
The payment requirements from the 402 response (price, currency, minimum top-up, Stripe publishable key).
Returns: Promise<{ paymentMethodId: string; topUpAmount?: number } | null>
{ paymentMethodId, topUpAmount? }
Payment details. topUpAmount defaults to requirements.minTopUp if omitted.
User declined to pay. The original 402 error is re-thrown (Axios) or the 402 response is returned (Fetch).
Stripe402ClientConfig
Configuration for client wrappers.
Field
Type
Required
Description
Callback to handle payment when a 402 is received.
Pre-existing client ID to include in requests. If provided, the client will first attempt to use credits before calling onPaymentRequired.
PaymentErrorCode
Union type of all possible error codes.
See Error Codes for detailed descriptions of each code.
Last updated