Mathew K Analytics

Lesson 1 · FastAPI deep dive

FastAPI Tutorial #1: Getting Started — Build Your First Python API

Video one of an eighteen-part series on building real, production-shaped APIs with FastAPI. The FastAPI app, path operations, uvicorn, and the automatic…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

FastAPI Deep-Dive, Video 1: Getting Started#

  • Video one of an eighteen-part series on building real, production-shaped APIs with FastAPI.
  • The FastAPI app, path operations, uvicorn, and the automatic interactive docs.
  • Let's get into it.

Part 1: What FastAPI Is and Why#

import fastapi
print(fastapi.__version__)
0.124.4

Part 2: Creating the App#

from fastapi import FastAPI
app = FastAPI(title='Demo API', version='1.0.0')
print(app.title, app.version)
Demo API 1.0.0

Part 3: The First Path Operation#

@app.get('/')
def read_root():
    return {'message': 'welcome to the demo API'}
print(read_root())
{'message': 'welcome to the demo API'}

Part 4: TestClient - Calling Endpoints Without a Live Server#

from fastapi.testclient import TestClient
client = TestClient(app)
response = client.get('/')
print(response.status_code)
print(response.json())
200
{'message': 'welcome to the demo API'}

Part 5: Multiple Path Operations and HTTP Methods#

@app.get('/status')
def get_status():
    return {'status': 'ok'}
@app.post('/echo')
def echo(payload: dict):
    return {'you_sent': payload}
print(client.get('/status').json())
print(client.post('/echo', json={'a': 1}).json())
{'status': 'ok'}
{'you_sent': {'a': 1}}

Part 6: Route Order Matters#

@app.get('/users/me')
def get_current_user():
    return {'user': 'current_user_placeholder'}
@app.get('/users/{user_id}')
def get_user(user_id: str):
    return {'user_id': user_id}
print(client.get('/users/me').json())
print(client.get('/users/42').json())
{'user': 'current_user_placeholder'}
{'user_id': '42'}

Part 7: Returning Lists and Nested Data#

@app.get('/items')
def list_items():
    return [{'id': 1, 'name': 'apple'}, {'id': 2, 'name': 'banana'}]
response = client.get('/items')
print(response.status_code)
print(response.json())
200
[{'id': 1, 'name': 'apple'}, {'id': 2, 'name': 'banana'}]

Part 8: Setting an Explicit Status Code#

from fastapi import status
@app.post('/items', status_code=status.HTTP_201_CREATED)
def create_item(payload: dict):
    return {'created': payload}
response = client.post('/items', json={'name': 'cherry'})
print(response.status_code)
201

Part 9: The Automatic Interactive Docs#

schema = app.openapi()
print(schema['info']['title'], schema['info']['version'])
print(sorted(schema['paths'].keys()))
Demo API 1.0.0
['/', '/echo', '/items', '/status', '/users/me', '/users/{user_id}']

Part 10: A Real Pattern - a Small Health Check and Info API#

info_app = FastAPI(title='Service Info API')
@info_app.get('/health')
def health_check():
    return {'status': 'healthy'}
@info_app.get('/info')
def service_info():
    return {'name': 'demo-service', 'version': '0.1.0'}
info_client = TestClient(info_app)
print(info_client.get('/health').json())
print(info_client.get('/info').json())
{'status': 'healthy'}
{'name': 'demo-service', 'version': '0.1.0'}

Wrap-Up: What You Learned#

  • FastAPI uses ordinary Python type hints to drive validation, data conversion, and documentation automatically.
  • A FastAPI application starts as a single FastAPI() instance that every route and setting attaches to.
  • A path operation combines an HTTP method decorator with a URL path and a plain Python function.
  • TestClient calls endpoints in-process, without starting a live server, the fast way to exercise an API.
  • @app.get, @app.post, @app.put, and @app.delete map to the corresponding HTTP methods.
  • Routes are matched in declaration order; fixed paths must be declared before conflicting parameterized ones.
  • A path operation can return any JSON-serializable structure: dicts, lists, and nested combinations.
  • status_code overrides the default 200 response code, e.g. 201 Created for a creation endpoint.
  • Every app generates a live OpenAPI schema automatically, powering the interactive docs at /docs and /redoc.
  • That wraps up getting started. Next up: Path and Query Parameters.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.