Skip to main content

status

A module containing HTTP status code constants. These constants make your code more readable and maintainable by using descriptive names instead of numeric codes.

Common Status Codes

Success (2xx)

int
default:"200"
Standard response for successful HTTP requests.
int
default:"201"
The request has been fulfilled and a new resource has been created.
int
default:"202"
The request has been accepted for processing, but processing has not been completed.
int
default:"204"
The server successfully processed the request, but is not returning any content.

Redirection (3xx)

int
default:"301"
The resource has been moved permanently to a new URL.
int
default:"302"
The resource temporarily resides under a different URL.
int
default:"304"
The resource has not been modified since the last request.
int
default:"307"
The request should be repeated with another URI, but future requests should use the original URI.

Client Errors (4xx)

int
default:"400"
The server cannot process the request due to a client error (e.g., malformed syntax).
int
default:"401"
Authentication is required and has failed or has not been provided.
int
default:"403"
The server understood the request but refuses to authorize it.
int
default:"404"
The requested resource could not be found.
int
default:"405"
The method specified in the request is not allowed for the resource.
int
default:"409"
The request could not be completed due to a conflict with the current state of the resource.
int
default:"422"
The request was well-formed but contains semantic errors (commonly used for validation errors).
int
default:"429"
The user has sent too many requests in a given amount of time (rate limiting).

Server Errors (5xx)

int
default:"500"
A generic error message when the server encounters an unexpected condition.
int
default:"502"
The server received an invalid response from an upstream server.
int
default:"503"
The server is currently unable to handle the request (temporarily overloaded or down for maintenance).
int
default:"504"
The server did not receive a timely response from an upstream server.

WebSocket Status Codes

int
default:"1000"
Normal closure; the connection successfully completed.
int
default:"1001"
The endpoint is going away (e.g., server shutdown or browser navigation).
int
default:"1003"
The endpoint received data of a type it cannot accept.
int
default:"1008"
The connection was closed because an endpoint received a message that violated its policy.

Usage Examples

Setting Response Status Code

Raising Exceptions

Conditional Status Codes

Usage Notes

  • Using named constants improves code readability
  • Makes it easier to understand the intent of status codes at a glance
  • Reduces errors from typos in numeric codes
  • All standard HTTP status codes are available
  • WebSocket status codes are also included with the WS_ prefix
  • These constants are re-exported from Starlette

Learn More

Read more in the FastAPI docs for Response Status Code.