402 Protocol

402 Protocol Implementation

What is HTTP 402?

HTTP status code 402 was reserved in 1999 for "Payment Required" but was never officially standardized. The original HTTP spec authors knew that someday, micropayments on the internet would be important.


The Traditional Problem

Subscription Model Issues

Problem
Impact

Pay $50/month, use 3 times

94% waste

Need analysis NOW, no subscription

Can't access

Usage spikes during events

Hit rate limits

Multiple tiers

Complex pricing decisions

The 402 Solution

Pay exactly for what you use. No subscription, no minimums, no rate limits (just cost).


How It Works

Flow Diagram

Request Without Balance

When a client makes a request without sufficient balance, they receive:

The client can then:

  1. Pay the Lightning invoice

  2. Send USDC to the address

  3. Top up their prepaid balance


Server Implementation

Middleware Approach

The payment check happens in middleware, before the request handler:

Creating Payment Options

Each payment method requires different setup:

def create_payment_options(self, amount: float) -> list: """Generate payment options for 402 response.""" options = []


Payment Verification

SOL payments (native SOL)

USDC Payments


Balance Management

Users can prepay to avoid per-request payment friction.

Database Schema

Balance Operations


Client Integration

Simple SDK

Usage Example


Security Considerations

Preventing Double-Spend

For prepaid balances, use database transactions:

Rate Limiting

Even with payments, prevent abuse:

Last updated