Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

559
Views
In Python's FastAPI autogenerated OpenAPI/Swagger documentation page, how can I add more error http status codes?

FastAPI generates automatic swagger/openapi documentation.

In the tutorial at https://fastapi.tiangolo.com/tutorial/response-status-code there's an example

from fastapi import FastAPI
app = FastAPI()

@app.post("/items/", status_code=201)
async def create_item(name: str):
    return {"name": name}

If you run this, the .../docs page shows two http response options:

Status Code 201 for success and Status Code 422 for Validation error

The above tutorial shows a picture of this pageFastAPI docs)

I would like to document more responde status_code descriptions in the docs, for example code 403, "Forbidden"

While I can run exceptions like this in code

raise HTTPException(status_code=403, detail="Forbidden")

I have not found a way to describe them in the autogenerated docs.

Any idea how to do that?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Does this solve your problem?

https://fastapi.tiangolo.com/advanced/additional-responses/

EDIT

With the response model you can add the different responses your API may return.

from pydantic import BaseModel
# Define your models here like
class model200(BaseModel):
    message: str = ""
    
@api.get("/my-route/", responses={200: {"response": model200}, 404: {"response": model404}, 500: {"response": model500}})
    async def api_route():
        return "I'm a wonderful route"

This will provide examples of your response models, making it simpler for users to interact with the api

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!