FastAPI Day 1: Build Your First API in 5 Minutes and Speed Up Development
This guide shows how to install FastAPI with a single pip command, create a minimal four‑line API, run it instantly with the built‑in dev server, explore the automatically generated Swagger UI, and understand the clear separation between development (fastapi dev) and production (fastapi run) commands.
1. One‑line installation
Run pip install "fastapi[standard]". The standard extra bundles Uvicorn, interactive docs, and auto‑reload, so you don’t need to install each component manually. fastapi dev – development mode fastapi run – production mode
Separating these two commands is a design strength of FastAPI.
2. First API in four lines
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello World"}Save as main.py and start the server with fastapi dev main.py. The service listens on 127.0.0.1:8000 with auto‑reload enabled, so code changes take effect without a manual restart.
3. Automatic interactive documentation
Open http://127.0.0.1:8000/docs to see a full Swagger UI. You can expand any endpoint, click “Try it out”, fill parameters, and execute the request; the JSON response appears directly on the page. This UI is generated by FastAPI without any extra configuration.
Practical tip: When collaborating with front‑end developers, share the /docs URL so they can test endpoints without Postman or a separate test environment.
4. Four sample endpoints to illustrate routing
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello World"}
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/about")
def about():
return {"name": "fastapi-14days", "version": "0.1.0"}
@app.get("/greet/{name}")
def greet(name: str):
return {"message": f"Hello, {name}!"}Key observations:
Path parameters are declared directly in the URL (e.g., {name}) and FastAPI infers their type from Python annotations.
No decorator arguments or class inheritance are needed; the HTTP method name itself acts as the decorator, which feels more intuitive for developers coming from Flask.
Each endpoint automatically updates the OpenAPI schema, the /docs UI, and the alternative /redoc UI.
Type errors are caught during request validation, preventing invalid data from reaching business logic.
5. Development vs. production commands
The two commands differ in semantics, which helps avoid confusion in a team setting.
fastapi dev : listens on 127.0.0.1, enables auto‑reload, intended for local debugging.
fastapi run : listens on 0.0.0.0, no auto‑reload, suited for container or server deployment.
6. What was accomplished on Day 1
Installed fastapi[standard] Wrote four endpoints (root, health check, project info, dynamic greeting)
Verified the /docs UI works
Ran pytest with five passing test cases
The entire workflow—from package installation to passing tests—took less than 20 minutes.
7. Next steps
Day 2 will cover path and query parameters, the most common input methods in FastAPI, and how type annotations are turned into validation rules.
Related links
Official documentation (Chinese): https://fastapi.tiangolo.com/zh/
GitHub repository: https://github.com/fastapi/fastapi
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tech Ocean
Focused on AI programming, sharing ready-to-use development efficiency solutions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
