FastAPI Response Status Codes
HTTP status codes are part of the response, used to inform the client of the result of the request. FastAPI allows you to declare a default status code in the path operation decorator, or modify it dynamically in the function.
Declare status codes
Use status_code parameter to declare the response status code in the decorator:
Example
from
fastapi
import
FastAPI
,
status
app
=
FastAPI
(
)
When creating a resource, use the 201 status code
@
app.
post
(
"/items/"
,
status_code
=
status.
HTTP_201_CREATED
)
async
def
create_item
(
name:
str
)
:
return
{
"name"
: name
}
status_codeis a parameter of the decorator method (e.g.@app.post()), not a parameter of the path operation function. It can be a number (e.g.201), or you can use a constant fromfastapi.status(recommended, with editor autocompletion support).
HTTP status code quick reference
| Range | Category | Common status codes | Meaning |
|---|---|---|---|
| 1xx | Information | -- | Rarely used directly |
| 2xx | Success | 200 OK |
Request successful (default status code) |
201 Created |
Resource created successfully | ||
204 No Content |
Success but no content returned | ||
| 3xx | Redirection | 304 Not Modified |
Resource not modified (cache valid) |
| 4xx | Client error | 400 Bad Request |
Bad request format |
401 Unauthorized |
Unauthorized | ||
403 Forbidden |
Authenticated but forbidden | ||
404 Not Found |
Resource not found | ||
| 5xx | Server error | 500 Internal Server Error |
Internal server error |
Using status constants
fastapi.status provides constants for all HTTP status codes, which are more convenient to use with editor autocompletion:
Example
from
fastapi
import
FastAPI
,
status
app
=
FastAPI
(
)
POST - create a resource, returns 201
@
app.
post
(
"/items/"
,
status_code
=
status.
HTTP_201_CREATED
)
async
def
create_item
(
name:
str
)
:
return
{
"name"
: name
}
DELETE - delete a resource, returns 204 (no content)
@
app.
delete
(
"/items/{item_id}"
,
status_code
=
status.
HTTP_204_NO_CONTENT
)
async
def
delete_item
(
item_id:
int
)
:
Delete operations usually return no content
pass
GET - get a resource, returns 200 (default)
@
app.
get
(
"/items/{item_id}"
)
async
def
read_item
(
item_id:
int
)
:
return
{
"item_id"
: item_id
}
204 No ContentIndicates success but no content returned, suitable for delete operations. Note that a 204 response cannot contain a response body.
Common status code constants
| Constant | Value | Use Cases |
|---|---|---|
status.HTTP_200_OK |
200 | GET request successful (default) |
status.HTTP_201_CREATED |
201 | POST create resource successful |
status.HTTP_204_NO_CONTENT |
204 | DELETE successful, no content returned |
status.HTTP_400_BAD_REQUEST |
400 | Invalid request parameters |
status.HTTP_401_UNAUTHORIZED |
401 | Not logged in or invalid token |
status.HTTP_403_FORBIDDEN |
403 | No permission to access |
status.HTTP_404_NOT_FOUND |
404 | Resource not found |
status.HTTP_409_CONFLICT |
409 | Resource conflict (e.g. duplicate creation) |
status.HTTP_422_UNPROCESSABLE_ENTITY |
422 | Data validation failed |
status.HTTP_500_INTERNAL_SERVER_ERROR |
500 | Internal server error |
Summary
- Use the decorator's
status_codeparameter to declare the default status code - it is recommended to use
fastapi.statusconstants, with editor autocompletion support - POST uses 201 to create resources, DELETE uses 204 to delete resources, GET uses 200 to get resources
- 4xx indicates client errors, 5xx indicates server errors