Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

FastAPI · Module

status

FastAPI's status module provides named integer constants for HTTP and WebSocket status codes. Import it from fastapi to use readable values such as status.HTTP_200_OK in application code.

How it works

The module exposes named integer constants for HTTP and WebSocket status codes. Import it from fastapi and use a descriptive constant, such as status.HTTP_200_OK, wherever a status-code value is needed.

Syntax

from fastapi import status

Returns

The module provides named integer status-code constants.

Key points

  • Import the module directly from fastapi with from fastapi import status.
  • Its members are named variables whose values are integer status codes.
  • The constants cover both HTTP and WebSocket status-code values.
  • Descriptive names improve editor completion and avoid memorizing numeric values.

Common mistakes

  • The module is supplied directly by Starlette, so its constants are not independently defined by FastAPI.

Example: Use a named status code

from fastapi import FastAPI, status

app = FastAPI()

@app.get("/items/", status_code=status.HTTP_200_OK)
def read_items():
    return [{"name": "Plumbus"}]

This runnable FastAPI app uses a named HTTP status constant in the route declaration.

When to use it

Declare endpoint response status

A route declaration needs an explicit response code without embedding a magic number.

Handle status codes in control flow

Application logic must compare or return an HTTP status value using a descriptive identifier.

Discover codes with autocomplete

An editor's completion support can replace memorizing numeric HTTP or WebSocket codes.

Interview questions

QWhat is the purpose of fastapi.status?

Import status from fastapi and select a named constant such as status.HTTP_200_OK. These constants represent integer HTTP or WebSocket status codes while making code completion and readability better than raw numbers.

Follow-up: Why use named constants instead of literals? Named constants communicate intent and reduce the need to memorize numeric codes.

QHow do you configure a route with a status constant?

Set a route's status_code argument to a status constant, for example status.HTTP_418_IM_A_TEAPOT. FastAPI then uses that value as the operation's configured response status code.

Follow-up: Can the same constants be used outside decorators? Yes; they are integer values and can be passed anywhere an HTTP status code is accepted.

QWhat kinds of codes does the module expose?

The module contains named integer constants for both HTTP and WebSocket status codes. Examples include HTTP_200_OK and HTTP_403_FORBIDDEN.

Follow-up: Where does this module originate? The documentation states that status is provided directly by Starlette.

  • FastAPI
  • Response Status Code
  • HTTP_100_CONTINUE
  • HTTP_200_OK
  • HTTP_403_FORBIDDEN
  • HTTP_418_IM_A_TEAPOT

Source: Status Codes. Checked 2026-10-15.