Skip to content

PYTHON FASTAPI · LESSON 1 OF 8

Python setup and your first API

Create an isolated Python environment and return JSON from a running server.

  • Python
  • venv
  • HTTP
  • Swagger UI

Your runnable checkpoint

Extract into a new practice folder. Each download includes complete source, requirements.txt and README.md. Stop any earlier server before running this example.

Create and activate a virtual environment as shown in lesson 1. Install with python -m pip install -r requirements.txt, then run fastapi dev main.py. Lessons 6–8 require TASK_API_KEY as explained in lesson 6.

1. Understand the concepts

An API lets a client ask a server for data. A browser sends GET /; FastAPI chooses the function registered for that method and path, calls it, and serializes the returned dictionary into JSON. A successful response has status 200.

Use Python 3.12 or 3.13. Check python3 --version on macOS/Linux or py --version on Windows. On an M1 Mac use an Apple Silicon Python installer. Download a checkpoint, extract it and open a terminal in the folder containing main.py.

On macOS/Linux run python3 -m venv .venv, then source .venv/bin/activate. On Windows run py -m venv .venv, then .venv\Scripts\activate.bat in Command Prompt. Run python -m pip install -r requirements.txt, then fastapi dev main.py. Keep that terminal open.

Import brings FastAPI into this file. app is the application object. @app.get("/") registers a route; it does not call home immediately. def creates a function, and the dictionary uses string keys and values. Type hints in later lessons describe expected values; FastAPI uses them for request validation.

Open http://127.0.0.1:8000/docs, expand GET /, choose Try it out and Execute. /openapi.json is the machine-readable API description. Stop the server with Ctrl+C. If a command is missing, activate the virtual environment again; if port 8000 is busy, add --port 8001.

2. Build and run

These are complete files, not fragments. Save them beside each other in the extracted project folder. Open http://127.0.0.1:8000/docs to send requests.

main.py

main.py
from fastapi import FastAPI

app = FastAPI(title="EduPoly Task API")

@app.get("/")
def home():
    return {"message": "Welcome to FastAPI"}

requirements.txt

requirements.txt
fastapi[standard]>=0.115,<1
pydantic>=2,<3
httpx>=0.27,<1
pytest>=8,<10

3. Check the result

  • GET / returns 200 and {"message":"Welcome to FastAPI"}.
  • /docs shows GET / and /openapi.json returns a schema.

4. Practice independently

Add GET /health returning {"status":"ok"}.

Show the explanation

Register another function with @app.get("/health"). Return a dictionary, not a JSON string.

When something goes wrong

For import errors, confirm the virtual environment is active and install requirements.txt using that environment’s Python. For 422, read the response detail and compare your input to the model. For 404, check the method, path and resource id. For 401 in later lessons, use Authorize in /docs with the same key configured on the server.

Read the related official FastAPI documentation ↗