← Back to Chapters

HTTP Status Code 402 – Payment Required

? HTTP Status Code 402 – Payment Required

? Quick Overview

HTTP status code 402 Payment Required is a reserved response code that indicates the client must make a payment to proceed. Although defined in the HTTP specification, it is not widely standardized and is mainly used by APIs and services that enforce paid access or subscription-based features.

? Key Concepts

  • 402 is a Client Error status code
  • It is officially reserved for future use
  • Commonly used in paid APIs and SaaS platforms
  • Often paired with custom error messages

? Syntax / Theory

When a server responds with HTTP/1.1 402 Payment Required, it means the request was understood but cannot be fulfilled until payment conditions are met. Unlike 401 or 403, this code is not related to authentication or authorization.

? Code Example(s)

? View Code Example
// Example HTTP response with 402 status code
HTTP/1.1 402 Payment Required
Content-Type: application/json

{
  "error": "Payment required",
  "message": "Please upgrade your plan to access this resource"
}

? Live Output / Explanation

Server Response

The client receives a 402 response along with a message explaining that payment or subscription upgrade is required before retrying the request.

? Interactive Example

Test the 402 logic below. Try buying the item with insufficient funds first.

? Balance: $0.00

Item: Premium API Key
Cost: $5.00

 
? View Logic Used Above
// Logic used in the demo above
const itemCost = 5.00;

if (userBalance < itemCost) {
  // Return 402 Error
  return { status: 402, message: "Payment Required" };
} else {
  // Process Transaction
  return { status: 200, message: "Success" };
}

? Use Cases

  • Paid REST APIs limiting free usage
  • Subscription-based SaaS platforms
  • Feature gating in enterprise applications
  • Usage quota enforcement

✅ Tips & Best Practices

  • Always include a clear error message with 402 responses
  • Provide payment or upgrade links in the response body
  • Do not confuse 402 with authentication errors
  • Document custom usage clearly for API consumers

? Try It Yourself

  1. Create a mock API endpoint that returns 402
  2. Handle the response gracefully in frontend code
  3. Display a payment prompt to the user
  4. Retry the request after simulating payment