
FastAPI
Status codes are three-digit numbers that tell the client what happened with their request. Understanding them is essential for API development.
Status codes are grouped into five categories:
| Code | Meaning | When to Use |
|---|---|---|
| 200 | OK | Request succeeded |
| 201 | Created | New resource created |
| 204 | No Content | Success, no body |
| 400 | Bad Request | Invalid request |
| 401 | Unauthorized | Authentication required |
| 403 | Forbidden | Access denied |
| 404 | Not Found | Resource doesn't exist |
| 500 | Server Error | Server problem |
from fastapi import status
@app.get("/items/{item_id}", status_code=status.HTTP_200_OK)
async def get_item(item_id: int):
return {"id": item_id}
@app.post("/items", status_code=status.HTTP_201_CREATED)
async def create_item(item: dict):
return item
@app.delete("/items/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_item(item_id: int):
return None@app.get("/users/{user_id}")
async def get_user(user_id: int):
user = db.get_user(user_id)
if not user:
# 404 if not found
raise HTTPException(status_code=404, detail="User not found")
# 200 if found
return userResources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward