Status Codes
You can import the status module from fastapi:
from fastapi import status
status 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 remember the integer status codes by memory.
Read more about it in the FastAPI docs about Response Status Code.
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"}]
    
        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
  
module-attribute
  
    
    
 
module-attribute
  
HTTP_101_SWITCHING_PROTOCOLS = 101
    
 
module-attribute
  
HTTP_102_PROCESSING = 102
    
 
module-attribute
  
HTTP_103_EARLY_HINTS = 103
    
 
module-attribute
  
    
    
 
module-attribute
  
    
    
 
module-attribute
  
    
    
 
HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203
    
 
module-attribute
  
HTTP_204_NO_CONTENT = 204
    
 
module-attribute
  
HTTP_205_RESET_CONTENT = 205
    
 
module-attribute
  
HTTP_206_PARTIAL_CONTENT = 206
    
 
module-attribute
  
HTTP_207_MULTI_STATUS = 207
    
 
module-attribute
  
HTTP_208_ALREADY_REPORTED = 208
    
 
module-attribute
  
    
    
 
module-attribute
  
HTTP_300_MULTIPLE_CHOICES = 300
    
 
module-attribute
  
HTTP_301_MOVED_PERMANENTLY = 301
    
 
module-attribute
  
    
    
 
module-attribute
  
    
    
 
module-attribute
  
HTTP_304_NOT_MODIFIED = 304
    
 
module-attribute
  
    
    
 
module-attribute
  
    
    
 
module-attribute
  
HTTP_307_TEMPORARY_REDIRECT = 307
    
 
module-attribute
  
HTTP_308_PERMANENT_REDIRECT = 308
    
 
module-attribute
  
HTTP_400_BAD_REQUEST = 400
    
 
module-attribute
  
HTTP_401_UNAUTHORIZED = 401
    
 
module-attribute
  
HTTP_402_PAYMENT_REQUIRED = 402
    
 
module-attribute
  
    
    
 
module-attribute
  
    
    
 
module-attribute
  
HTTP_405_METHOD_NOT_ALLOWED = 405
    
 
module-attribute
  
HTTP_406_NOT_ACCEPTABLE = 406
    
 
module-attribute
  
HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407
    
 
module-attribute
  
HTTP_408_REQUEST_TIMEOUT = 408
    
 
module-attribute
  
    
    
 
module-attribute
  
    
    
 
module-attribute
  
HTTP_411_LENGTH_REQUIRED = 411
    
 
module-attribute
  
HTTP_412_PRECONDITION_FAILED = 412
    
 
module-attribute
  
HTTP_413_CONTENT_TOO_LARGE = 413
    
 
module-attribute
  
HTTP_414_URI_TOO_LONG = 414
    
 
HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415
    
 
module-attribute
  
HTTP_416_RANGE_NOT_SATISFIABLE = 416
    
 
module-attribute
  
HTTP_417_EXPECTATION_FAILED = 417
    
 
module-attribute
  
HTTP_418_IM_A_TEAPOT = 418
    
 
module-attribute
  
HTTP_421_MISDIRECTED_REQUEST = 421
    
 
module-attribute
  
HTTP_422_UNPROCESSABLE_CONTENT = 422
    
 
module-attribute
  
    
    
 
module-attribute
  
HTTP_424_FAILED_DEPENDENCY = 424
    
 
module-attribute
  
    
    
 
module-attribute
  
HTTP_426_UPGRADE_REQUIRED = 426
    
 
module-attribute
  
HTTP_428_PRECONDITION_REQUIRED = 428
    
 
module-attribute
  
HTTP_429_TOO_MANY_REQUESTS = 429
    
 
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431
    
 
module-attribute
  
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS = 451
    
 
module-attribute
  
HTTP_500_INTERNAL_SERVER_ERROR = 500
    
 
module-attribute
  
HTTP_501_NOT_IMPLEMENTED = 501
    
 
module-attribute
  
HTTP_502_BAD_GATEWAY = 502
    
 
module-attribute
  
HTTP_503_SERVICE_UNAVAILABLE = 503
    
 
module-attribute
  
HTTP_504_GATEWAY_TIMEOUT = 504
    
 
module-attribute
  
HTTP_505_HTTP_VERSION_NOT_SUPPORTED = 505
    
 
module-attribute
  
HTTP_506_VARIANT_ALSO_NEGOTIATES = 506
    
 
module-attribute
  
HTTP_507_INSUFFICIENT_STORAGE = 507
    
 
module-attribute
  
HTTP_508_LOOP_DETECTED = 508
    
 
module-attribute
  
HTTP_510_NOT_EXTENDED = 510
    
 
module-attribute
  
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511
        WebSocket codes
https://www.iana.org/assignments/websocket/websocket.xml#close-code-number
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
     
 
module-attribute
  
WS_1000_NORMAL_CLOSURE = 1000
    
 
module-attribute
  
WS_1001_GOING_AWAY = 1001
    
 
module-attribute
  
WS_1002_PROTOCOL_ERROR = 1002
    
 
module-attribute
  
WS_1003_UNSUPPORTED_DATA = 1003
    
 
module-attribute
  
WS_1005_NO_STATUS_RCVD = 1005
    
 
module-attribute
  
WS_1006_ABNORMAL_CLOSURE = 1006
    
 
module-attribute
  
WS_1007_INVALID_FRAME_PAYLOAD_DATA = 1007
    
 
module-attribute
  
WS_1008_POLICY_VIOLATION = 1008
    
 
module-attribute
  
WS_1009_MESSAGE_TOO_BIG = 1009
    
 
module-attribute
  
WS_1010_MANDATORY_EXT = 1010
    
 
module-attribute
  
WS_1011_INTERNAL_ERROR = 1011
    
 
module-attribute
  
WS_1012_SERVICE_RESTART = 1012
    
 
module-attribute
  
WS_1013_TRY_AGAIN_LATER = 1013
    
 
module-attribute
  
WS_1014_BAD_GATEWAY = 1014
    
 
module-attribute
  
WS_1015_TLS_HANDSHAKE = 1015