Status Codes
You can import the status module from fastapi:
from fastapi import statusstatus is provided directly by Starlette.
It contains a group of named constants (variables) with integer status codes.
For example:
- 200:
status.HTTP_200_OK - 403:
status.HTTP_403_FORBIDDEN - etc.
It can be convenient to quickly access HTTP (and WebSocket) status codes in your app, using autocompletion for the name without having to memorize the integer status codes.
Read more about it in the FastAPI docs about Response Status Code.
Example
Section titled “Example”from fastapi import FastAPI, status
app = FastAPI()
@app.get("/items/", status_code=status.HTTP_418_IM_A_TEAPOT)
def read_items():
return [{"name": "Plumbus"}, {"name": "Portal Gun"}]fastapi.status
Section titled “fastapi.status”HTTP codes See HTTP Status Code Registry: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
And RFC 9110 - https://www.rfc-editor.org/rfc/rfc9110
HTTP_100_CONTINUE module-attribute
Section titled “HTTP_100_CONTINUE module-attribute”HTTP_100_CONTINUE = 100HTTP_101_SWITCHING_PROTOCOLS module-attribute
Section titled “HTTP_101_SWITCHING_PROTOCOLS module-attribute”HTTP_101_SWITCHING_PROTOCOLS = 101HTTP_102_PROCESSING module-attribute
Section titled “HTTP_102_PROCESSING module-attribute”HTTP_102_PROCESSING = 102HTTP_103_EARLY_HINTS module-attribute
Section titled “HTTP_103_EARLY_HINTS module-attribute”HTTP_103_EARLY_HINTS = 103HTTP_200_OK module-attribute
Section titled “HTTP_200_OK module-attribute”HTTP_200_OK = 200HTTP_201_CREATED module-attribute
Section titled “HTTP_201_CREATED module-attribute”HTTP_201_CREATED = 201HTTP_202_ACCEPTED module-attribute
Section titled “HTTP_202_ACCEPTED module-attribute”HTTP_202_ACCEPTED = 202HTTP_203_NON_AUTHORITATIVE_INFORMATION module-attribute
Section titled “HTTP_203_NON_AUTHORITATIVE_INFORMATION module-attribute”HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203HTTP_204_NO_CONTENT module-attribute
Section titled “HTTP_204_NO_CONTENT module-attribute”HTTP_204_NO_CONTENT = 204HTTP_205_RESET_CONTENT module-attribute
Section titled “HTTP_205_RESET_CONTENT module-attribute”HTTP_205_RESET_CONTENT = 205HTTP_206_PARTIAL_CONTENT module-attribute
Section titled “HTTP_206_PARTIAL_CONTENT module-attribute”HTTP_206_PARTIAL_CONTENT = 206HTTP_207_MULTI_STATUS module-attribute
Section titled “HTTP_207_MULTI_STATUS module-attribute”HTTP_207_MULTI_STATUS = 207HTTP_208_ALREADY_REPORTED module-attribute
Section titled “HTTP_208_ALREADY_REPORTED module-attribute”HTTP_208_ALREADY_REPORTED = 208HTTP_226_IM_USED module-attribute
Section titled “HTTP_226_IM_USED module-attribute”HTTP_226_IM_USED = 226HTTP_300_MULTIPLE_CHOICES module-attribute
Section titled “HTTP_300_MULTIPLE_CHOICES module-attribute”HTTP_300_MULTIPLE_CHOICES = 300HTTP_301_MOVED_PERMANENTLY module-attribute
Section titled “HTTP_301_MOVED_PERMANENTLY module-attribute”HTTP_301_MOVED_PERMANENTLY = 301HTTP_302_FOUND module-attribute
Section titled “HTTP_302_FOUND module-attribute”HTTP_302_FOUND = 302HTTP_303_SEE_OTHER module-attribute
Section titled “HTTP_303_SEE_OTHER module-attribute”HTTP_303_SEE_OTHER = 303HTTP_304_NOT_MODIFIED module-attribute
Section titled “HTTP_304_NOT_MODIFIED module-attribute”HTTP_304_NOT_MODIFIED = 304HTTP_305_USE_PROXY module-attribute
Section titled “HTTP_305_USE_PROXY module-attribute”HTTP_305_USE_PROXY = 305HTTP_306_RESERVED module-attribute
Section titled “HTTP_306_RESERVED module-attribute”HTTP_306_RESERVED = 306HTTP_307_TEMPORARY_REDIRECT module-attribute
Section titled “HTTP_307_TEMPORARY_REDIRECT module-attribute”HTTP_307_TEMPORARY_REDIRECT = 307HTTP_308_PERMANENT_REDIRECT module-attribute
Section titled “HTTP_308_PERMANENT_REDIRECT module-attribute”HTTP_308_PERMANENT_REDIRECT = 308HTTP_400_BAD_REQUEST module-attribute
Section titled “HTTP_400_BAD_REQUEST module-attribute”HTTP_400_BAD_REQUEST = 400HTTP_401_UNAUTHORIZED module-attribute
Section titled “HTTP_401_UNAUTHORIZED module-attribute”HTTP_401_UNAUTHORIZED = 401HTTP_402_PAYMENT_REQUIRED module-attribute
Section titled “HTTP_402_PAYMENT_REQUIRED module-attribute”HTTP_402_PAYMENT_REQUIRED = 402HTTP_403_FORBIDDEN module-attribute
Section titled “HTTP_403_FORBIDDEN module-attribute”HTTP_403_FORBIDDEN = 403HTTP_404_NOT_FOUND module-attribute
Section titled “HTTP_404_NOT_FOUND module-attribute”HTTP_404_NOT_FOUND = 404HTTP_405_METHOD_NOT_ALLOWED module-attribute
Section titled “HTTP_405_METHOD_NOT_ALLOWED module-attribute”HTTP_405_METHOD_NOT_ALLOWED = 405HTTP_406_NOT_ACCEPTABLE module-attribute
Section titled “HTTP_406_NOT_ACCEPTABLE module-attribute”HTTP_406_NOT_ACCEPTABLE = 406HTTP_407_PROXY_AUTHENTICATION_REQUIRED module-attribute
Section titled “HTTP_407_PROXY_AUTHENTICATION_REQUIRED module-attribute”HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407HTTP_408_REQUEST_TIMEOUT module-attribute
Section titled “HTTP_408_REQUEST_TIMEOUT module-attribute”HTTP_408_REQUEST_TIMEOUT = 408HTTP_409_CONFLICT module-attribute
Section titled “HTTP_409_CONFLICT module-attribute”HTTP_409_CONFLICT = 409HTTP_410_GONE module-attribute
Section titled “HTTP_410_GONE module-attribute”HTTP_410_GONE = 410HTTP_411_LENGTH_REQUIRED module-attribute
Section titled “HTTP_411_LENGTH_REQUIRED module-attribute”HTTP_411_LENGTH_REQUIRED = 411HTTP_412_PRECONDITION_FAILED module-attribute
Section titled “HTTP_412_PRECONDITION_FAILED module-attribute”HTTP_412_PRECONDITION_FAILED = 412HTTP_413_CONTENT_TOO_LARGE module-attribute
Section titled “HTTP_413_CONTENT_TOO_LARGE module-attribute”HTTP_413_CONTENT_TOO_LARGE = 413HTTP_414_URI_TOO_LONG module-attribute
Section titled “HTTP_414_URI_TOO_LONG module-attribute”HTTP_414_URI_TOO_LONG = 414HTTP_415_UNSUPPORTED_MEDIA_TYPE module-attribute
Section titled “HTTP_415_UNSUPPORTED_MEDIA_TYPE module-attribute”HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415HTTP_416_RANGE_NOT_SATISFIABLE module-attribute
Section titled “HTTP_416_RANGE_NOT_SATISFIABLE module-attribute”HTTP_416_RANGE_NOT_SATISFIABLE = 416HTTP_417_EXPECTATION_FAILED module-attribute
Section titled “HTTP_417_EXPECTATION_FAILED module-attribute”HTTP_417_EXPECTATION_FAILED = 417HTTP_418_IM_A_TEAPOT module-attribute
Section titled “HTTP_418_IM_A_TEAPOT module-attribute”HTTP_418_IM_A_TEAPOT = 418HTTP_421_MISDIRECTED_REQUEST module-attribute
Section titled “HTTP_421_MISDIRECTED_REQUEST module-attribute”HTTP_421_MISDIRECTED_REQUEST = 421HTTP_422_UNPROCESSABLE_CONTENT module-attribute
Section titled “HTTP_422_UNPROCESSABLE_CONTENT module-attribute”HTTP_422_UNPROCESSABLE_CONTENT = 422HTTP_423_LOCKED module-attribute
Section titled “HTTP_423_LOCKED module-attribute”HTTP_423_LOCKED = 423HTTP_424_FAILED_DEPENDENCY module-attribute
Section titled “HTTP_424_FAILED_DEPENDENCY module-attribute”HTTP_424_FAILED_DEPENDENCY = 424HTTP_425_TOO_EARLY module-attribute
Section titled “HTTP_425_TOO_EARLY module-attribute”HTTP_425_TOO_EARLY = 425HTTP_426_UPGRADE_REQUIRED module-attribute
Section titled “HTTP_426_UPGRADE_REQUIRED module-attribute”HTTP_426_UPGRADE_REQUIRED = 426HTTP_428_PRECONDITION_REQUIRED module-attribute
Section titled “HTTP_428_PRECONDITION_REQUIRED module-attribute”HTTP_428_PRECONDITION_REQUIRED = 428HTTP_429_TOO_MANY_REQUESTS module-attribute
Section titled “HTTP_429_TOO_MANY_REQUESTS module-attribute”HTTP_429_TOO_MANY_REQUESTS = 429HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE module-attribute
Section titled “HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE module-attribute”HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS module-attribute
Section titled “HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS module-attribute”HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS = 451HTTP_500_INTERNAL_SERVER_ERROR module-attribute
Section titled “HTTP_500_INTERNAL_SERVER_ERROR module-attribute”HTTP_500_INTERNAL_SERVER_ERROR = 500HTTP_501_NOT_IMPLEMENTED module-attribute
Section titled “HTTP_501_NOT_IMPLEMENTED module-attribute”HTTP_501_NOT_IMPLEMENTED = 501HTTP_502_BAD_GATEWAY module-attribute
Section titled “HTTP_502_BAD_GATEWAY module-attribute”HTTP_502_BAD_GATEWAY = 502HTTP_503_SERVICE_UNAVAILABLE module-attribute
Section titled “HTTP_503_SERVICE_UNAVAILABLE module-attribute”HTTP_503_SERVICE_UNAVAILABLE = 503HTTP_504_GATEWAY_TIMEOUT module-attribute
Section titled “HTTP_504_GATEWAY_TIMEOUT module-attribute”HTTP_504_GATEWAY_TIMEOUT = 504HTTP_505_HTTP_VERSION_NOT_SUPPORTED module-attribute
Section titled “HTTP_505_HTTP_VERSION_NOT_SUPPORTED module-attribute”HTTP_505_HTTP_VERSION_NOT_SUPPORTED = 505HTTP_506_VARIANT_ALSO_NEGOTIATES module-attribute
Section titled “HTTP_506_VARIANT_ALSO_NEGOTIATES module-attribute”HTTP_506_VARIANT_ALSO_NEGOTIATES = 506HTTP_507_INSUFFICIENT_STORAGE module-attribute
Section titled “HTTP_507_INSUFFICIENT_STORAGE module-attribute”HTTP_507_INSUFFICIENT_STORAGE = 507HTTP_508_LOOP_DETECTED module-attribute
Section titled “HTTP_508_LOOP_DETECTED module-attribute”HTTP_508_LOOP_DETECTED = 508HTTP_510_NOT_EXTENDED module-attribute
Section titled “HTTP_510_NOT_EXTENDED module-attribute”HTTP_510_NOT_EXTENDED = 510HTTP_511_NETWORK_AUTHENTICATION_REQUIRED module-attribute
Section titled “HTTP_511_NETWORK_AUTHENTICATION_REQUIRED module-attribute”HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511WebSocket codes https://www.iana.org/assignments/websocket/websocket.xml#close-code-number https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
WS_1000_NORMAL_CLOSURE module-attribute
Section titled “WS_1000_NORMAL_CLOSURE module-attribute”WS_1000_NORMAL_CLOSURE = 1000WS_1001_GOING_AWAY module-attribute
Section titled “WS_1001_GOING_AWAY module-attribute”WS_1001_GOING_AWAY = 1001WS_1002_PROTOCOL_ERROR module-attribute
Section titled “WS_1002_PROTOCOL_ERROR module-attribute”WS_1002_PROTOCOL_ERROR = 1002WS_1003_UNSUPPORTED_DATA module-attribute
Section titled “WS_1003_UNSUPPORTED_DATA module-attribute”WS_1003_UNSUPPORTED_DATA = 1003WS_1005_NO_STATUS_RCVD module-attribute
Section titled “WS_1005_NO_STATUS_RCVD module-attribute”WS_1005_NO_STATUS_RCVD = 1005WS_1006_ABNORMAL_CLOSURE module-attribute
Section titled “WS_1006_ABNORMAL_CLOSURE module-attribute”WS_1006_ABNORMAL_CLOSURE = 1006WS_1007_INVALID_FRAME_PAYLOAD_DATA module-attribute
Section titled “WS_1007_INVALID_FRAME_PAYLOAD_DATA module-attribute”WS_1007_INVALID_FRAME_PAYLOAD_DATA = 1007WS_1008_POLICY_VIOLATION module-attribute
Section titled “WS_1008_POLICY_VIOLATION module-attribute”WS_1008_POLICY_VIOLATION = 1008WS_1009_MESSAGE_TOO_BIG module-attribute
Section titled “WS_1009_MESSAGE_TOO_BIG module-attribute”WS_1009_MESSAGE_TOO_BIG = 1009WS_1010_MANDATORY_EXT module-attribute
Section titled “WS_1010_MANDATORY_EXT module-attribute”WS_1010_MANDATORY_EXT = 1010WS_1011_INTERNAL_ERROR module-attribute
Section titled “WS_1011_INTERNAL_ERROR module-attribute”WS_1011_INTERNAL_ERROR = 1011WS_1012_SERVICE_RESTART module-attribute
Section titled “WS_1012_SERVICE_RESTART module-attribute”WS_1012_SERVICE_RESTART = 1012WS_1013_TRY_AGAIN_LATER module-attribute
Section titled “WS_1013_TRY_AGAIN_LATER module-attribute”WS_1013_TRY_AGAIN_LATER = 1013WS_1014_BAD_GATEWAY module-attribute
Section titled “WS_1014_BAD_GATEWAY module-attribute”WS_1014_BAD_GATEWAY = 1014WS_1015_TLS_HANDSHAKE module-attribute
Section titled “WS_1015_TLS_HANDSHAKE module-attribute”WS_1015_TLS_HANDSHAKE = 1015