4xx Client errors
Last updated Nov 19, 2024

What is HTTP Status Code 402? - Payment Required

Benjamin Bouchet
Get your free
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

The HTTP Status Code 402 is reserved for future use.

What is HTTP Status Code 402?


Hypertext Transfer Protocol (HTTP), the foundation of any data exchange on the web, uses a series of status codes, or HTTP status symbols, to relay information between clients and servers. Understanding these status codes, including the HTTP status code 402, is crucial for developers, webmasters, and network admins alike.


HTTP Response Status Code 402: Payment Required


The HTTP response status code 402 is defined as "Payment Required". While many HTTP status codes are commonly seen and used, this particular status code is one of the lesser-known and infrequently utilized HTTP status codes.


The original intention of HTTP status code 402 was to be used for some form of a digital cash or micropayment scheme. However, it was left undefined in the HTTP 1.1 specification and is not widely used as of the cutoff knowledge in September 2021. It remains reserved for future use.


HTTP status code 402 is a client error status code. In terms of HTTP status constants, whether you are dealing with Rails HTTP status symbol, Symfony HTTP status constant, or Python2 HTTP status constant, this status code signifies that the client must pay to access the requested resource.


However, unlike common client errors such as a 400 Bad Request or a 413 URI Too Long, HTTP 402 doesn't denote a syntax error or a request that is too large. It instead suggests an online payment issue related to accessing a specific resource. This is where the "Payment Required" meaning comes into play.


Error Code Interpretation


In practice, when a web server returns this status code, the server includes an error message in the HTTP response's header fields. This error message typically clarifies why the requested resource requires payment.


For example, if you exceed a daily limit on the Shopify API, the server might respond with HTTP status code 402. However, as of now, the standard does not define how to proceed after a 402 error code is received.


The user agent, which is typically the client's web browser, then displays the error message to the user. This way, the user understands the cause of the issue - like an online payment problem - that led to the 402 Payment Required error.


When and How is HTTP Status Code 402 Used?


Despite the initial lack of detailed guidelines on HTTP status code 402, several innovative use cases for this status code have emerged in recent years, such as limiting API calls or implementing micropayment schemes.


API Call Limitation


APIs often have a daily limit to prevent misuse and maintain optimal performance. If a client's request exceeds this daily limit, the server could return a 402 Payment Required response. For instance, the Shopify API may utilize this status code to notify the user that they need to upgrade their package to continue using the service.


The server will include specific header fields and an appropriate error message to clarify the situation. The client application then handles the response status, prompting the user to take the necessary action.


Micropayment Scheme Implementation


As the world of digital cash evolves, HTTP status code 402 might gain prominence. The original intention was to facilitate the implementation of micropayment schemes for certain online content. This would provide a mechanism for the client to send a micropayment to the origin server to access the content, resolving the payment required issue.


This could include any type of content accessible via HTTP, including web pages, API responses, or even media types like images and videos. Upon payment, the server would send a 200 OK response with the requested resource or a 202 Accepted status for an asynchronous operation.


However, such implementation might lead to challenges, such as dealing with unsupported media types or dealing with a post request that requires a permanent redirect.


Future Possibilities


The future use of HTTP status code 402 remains an open topic of discussion among developers and technologists. Given the growing digital economy and the advent of new digital currencies, it's a good idea to keep an eye on developments around this status code.


For now, as an HTTP status symbol, the 402 code remains an interesting but under-utilized part of the HTTP standard. Whether used as a Rails HTTP status symbol, Symfony HTTP status constant, or Python2 HTTP status constant, it's another tool in the arsenal of developers to enhance their applications' usability and functionality.


While the possibilities of HTTP 402 are intriguing, it's also worth noting other status codes like 204 No Content or 425 Too Early. In the next section of this blog post, we'll examine these codes and their implications in more depth.


Example Usage of HTTP Status Code 402


Let's explore some practical examples of how HTTP Status Code 402 might be used in real-world applications.


Non-Technical Example: Daily API Request Limit


Suppose you're using an online service like the Shopify API. This service offers a free tier with a daily limit of 1000 API requests. During a busy day, your shop gets more traffic than usual, and you exhaust your daily limit by mid-day.


At this point, every new API request you make will return an HTTP Status Code 402 along with an error message indicating that you've hit your daily limit and need to upgrade your package or wait until the daily limit resets. This would be a simple, non-technical application of HTTP 402, Payment Required.


Technical Example: Implementing a Micropayment Scheme


For a more technical example, let's consider a web server that hosts premium content, accessible only by making micropayments.


Below is an example of how the server might respond to a client's request for this premium content:



from http import HTTPStatus

def premium_content(request):
    if not request.payment:
        response.status = HTTPStatus.PAYMENT_REQUIRED
        return "Payment Required to access this content."
    else:
        return "Here is your premium content."

In this code snippet, when a client makes a request without including payment, the server responds with HTTP Status Code 402, indicating that payment is required.



What is the History of HTTP Status Code 402?


The history of HTTP Status Code 402 traces back to the original HTTP 1.1 specification where it was defined with the intention to facilitate the implementation of digital cash or micropayment schemes. However, since the inception of this status code, it remained largely undefined and unused. Its future use has been the subject of much speculation and discussion among developers.


How Does HTTP Status Code 402 Relate to Other Status Codes?


HTTP Status Code 402 is part of the 4xx class of HTTP status codes, which represent client errors. This class includes several well-known status codes, such as 400 Bad Request, 404 Not Found, and 403 Forbidden.


The primary difference between 402 Payment Required and other 4xx status codes is its original intention to handle online payment issues. However, this status code has not been fully standardized for such use as of the cutoff knowledge in September 2021.


In contrast, other status codes in the 4xx class have specific use cases. For instance, 400 Bad Request is used when the server cannot or will not process the request due to something that is perceived to be a client error, like malformed request syntax.


In essence, while HTTP Status Code 402 has a unique role within the spectrum of HTTP status codes, it remains largely unexploited, yet its potential uses make it a fascinating topic of discussion for web developers and technologists alike.


Other Interesting Things About HTTP Status Code 402


HTTP status code 402, a lesser-known HTTP status symbol, is known for its "Payment Required" status. Interestingly, beyond its designated purpose, it can serve as a flexible, general-purpose client error code.


Developers can employ HTTP status code 402 creatively, particularly when the encountered client error doesn't align with any existing HTTP status codes. This allows it to signify application-specific issues that fall outside the conventional error codes like 400 Bad Request or 403 Forbidden.


Further, HTTP status code 402 can act as a "soft limit" indicator in APIs. Unlike HTTP status code 429 (Too Many Requests), which traditionally implies that the user has sent too many requests in a given time, HTTP 402 can signal to reach a soft limit. For instance, when an API user hits a daily request limit on a premium plan, the server might respond with a 402 status, suggesting an upgrade. This status code, regardless if it's treated as a Rails HTTP status symbol or a Symfony HTTP status constant, could be instrumental in creating user-friendly web services.

Get your free
API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required