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.
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.
// 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"
}
The client receives a 402 response along with a message explaining that payment or subscription upgrade is required before retrying the request.
Test the 402 logic below. Try buying the item with insufficient funds first.
Item: Premium API Key
Cost: $5.00
// 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" };
}