Automatic API documentation (Swagger UI) with FastAPI
📖 Scenario: You are building a simple web API for a bookstore. You want to make it easy for other developers to see and test your API endpoints without writing extra documentation.
🎯 Goal: Create a FastAPI application that automatically generates interactive API documentation using Swagger UI.
📋 What You'll Learn
Create a FastAPI app instance named app
Define a GET endpoint at /books/ that returns a list of book titles
Use FastAPI's automatic documentation features to enable Swagger UI
Run the app so the documentation is accessible at /docs
💡 Why This Matters
🌍 Real World
APIs are used everywhere to let different programs talk to each other. Automatic documentation helps developers understand and test APIs quickly without extra writing.
💼 Career
Knowing how to build APIs with automatic docs is a key skill for backend developers, making your APIs easier to use and maintain.
Progress0 / 4 steps
1
Create the FastAPI app and initial data
Import FastAPI from fastapi and create an app instance called app. Then create a list called books with these exact strings: 'The Hobbit', '1984', 'Pride and Prejudice'.
FastAPI
Hint
Use app = FastAPI() to create the app. Define books as a list with the exact book titles.
2
Add a GET endpoint to return the books list
Use the @app.get decorator with the path /books/ to create a function called read_books that returns the books list.
FastAPI
Hint
Use @app.get('/books/') above the function. The function should return the books list.
3
Verify automatic Swagger UI documentation is enabled
Confirm that the FastAPI app app automatically provides Swagger UI documentation at /docs without extra code. No new code is needed here, just keep the existing code.
FastAPI
Hint
FastAPI enables Swagger UI automatically when you create app = FastAPI(). No extra code is needed.
4
Run the FastAPI app with Uvicorn
Add the code to run the app using uvicorn when the script is executed directly. Use if __name__ == '__main__': and call uvicorn.run with app, host '127.0.0.1', and port 8000.
FastAPI
Hint
Import uvicorn. Use if __name__ == '__main__': to run uvicorn.run(app, host='127.0.0.1', port=8000).
Practice
(1/5)
1. What is the default URL path where FastAPI serves the automatic API documentation using Swagger UI?
easy
A. /docs
B. /swagger
C. /api-docs
D. /documentation
Solution
Step 1: Understand FastAPI default docs path
FastAPI automatically provides Swagger UI documentation at the path '/docs' by default.
Step 2: Compare options with default path
Among the options, only '/docs' matches the default URL for Swagger UI in FastAPI.
Final Answer:
/docs -> Option A
Quick Check:
Default Swagger UI docs path = /docs [OK]
Hint: Remember: FastAPI docs are at /docs by default [OK]
Common Mistakes:
Confusing /swagger with /docs
Assuming /api-docs is default
Thinking documentation is at root /
2. Which of the following code snippets correctly imports and creates a FastAPI app that will have automatic Swagger UI documentation?
easy
A. import fastapi
app = fastapi.App()
B. from fastapi import API
app = API()
C. from fastapi import FastAPI
app = FastAPI()
D. import FastAPI
app = FastAPI()
Solution
Step 1: Check correct import and app creation
The correct import is 'from fastapi import FastAPI' and app is created by calling FastAPI().
Step 2: Validate each option
from fastapi import FastAPI
app = FastAPI() matches the correct syntax. Options A, C, and D have wrong imports or class names.
Final Answer:
from fastapi import FastAPI\napp = FastAPI() -> Option C
Quick Check:
Correct import and app creation = from fastapi import FastAPI
app = FastAPI() [OK]
Hint: FastAPI app is created by FastAPI() after correct import [OK]
Common Mistakes:
Using wrong import names like API or App
Trying to import FastAPI with capital letter as module
Calling wrong constructor names
3. Given this FastAPI code snippet, what will you see when you open http://localhost:8000/docs in your browser?