Skip to content
Sarj Code Standards

delete-route-returns-no-content

FastAPI DELETE routes declare status code 204 No Content.

Why

Without an explicit 204, FastAPI answers a DELETE with 200 and a JSON null body. Generated clients then type the result as nullable data, and API routes disagree on what a successful removal returns.

Fix

Declare status_code=204 (or status.HTTP_204_NO_CONTENT) on the DELETE route and return None.

Examples

Before — flagged A DELETE route answers 200 with a null body
api.py
from fastapi import APIRouter
router = APIRouter()
@router.delete("/keys/{key_id}")
async def revoke(key_id: str) -> None:
return None
After — preferred A DELETE route declares 204
api.py
from fastapi import APIRouter, status
router = APIRouter()
@router.delete("/keys/{key_id}", status_code=status.HTTP_204_NO_CONTENT)
async def revoke(key_id: str) -> None:
return None