Skip to content

Commit 4d2d923

Browse files
authored
Merge pull request #692 from realpython/fastapi
Add materials for the "Get Started With FastAPI tutorial"
2 parents 0380626 + 272d702 commit 4d2d923

File tree

11 files changed

+124
-9
lines changed

11 files changed

+124
-9
lines changed

crud-operations/crud_fastapi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from crud_sql_alchemy import Bird, init_db
22
from crud_sql_alchemy import Session as SessionLocal
3-
from fastapi import Depends, FastAPI, HTTPException
43
from pydantic import BaseModel, ConfigDict
54
from sqlalchemy import select
65
from sqlalchemy.orm import Session
76

7+
from fastapi import Depends, FastAPI, HTTPException
8+
89
app = FastAPI()
910
init_db()
1011

fastapi-url-shortener/source_code_final/shortener_app/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import validators
2-
from fastapi import Depends, FastAPI, HTTPException, Request
3-
from fastapi.responses import RedirectResponse
42
from sqlalchemy.orm import Session
53
from starlette.datastructures import URL
64

5+
from fastapi import Depends, FastAPI, HTTPException, Request
6+
from fastapi.responses import RedirectResponse
7+
78
from . import crud, models, schemas
89
from .config import get_settings
910
from .database import SessionLocal, engine

fastapi-url-shortener/source_code_step_2/shortener_app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import secrets
22

33
import validators
4+
from sqlalchemy.orm import Session
5+
46
from fastapi import Depends, FastAPI, HTTPException, Request
57
from fastapi.responses import RedirectResponse
6-
from sqlalchemy.orm import Session
78

89
from . import models, schemas
910
from .database import SessionLocal, engine

fastapi-url-shortener/source_code_step_3/shortener_app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import validators
2+
from sqlalchemy.orm import Session
3+
24
from fastapi import Depends, FastAPI, HTTPException, Request
35
from fastapi.responses import RedirectResponse
4-
from sqlalchemy.orm import Session
56

67
from . import crud, models, schemas
78
from .database import SessionLocal, engine

fastapi/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Get Started With FastAPI
2+
3+
This repository contains code snippets discussed in the associated tutorial [Get Started With FastAPI](https://realpython.com/get-started-with-fastapi/).
4+
5+
## Installation
6+
7+
The recommended way to install FastAPI is with the `[standard]` extra dependencies. This ensures you get all the tools you need for developing an API without having to hunt down additional packages later:
8+
9+
```console
10+
$ python -m pip install "fastapi[standard]"
11+
```
12+
13+
The quotes around `"fastapi[standard]"` ensure the command works correctly across different [terminals](https://realpython.com/terminal-commands/) and operating systems. With the command above, you install several useful packages, including the [FastAPI CLI](https://fastapi.tiangolo.com/fastapi-cli/) and [uvicorn](https://www.uvicorn.org/), an ASGI server for running your application.
14+
15+
You can also use the `requirements.txt` file in this folder and run `python -m pip install -r requirements.txt` to install the standard dependencies of FastAPI.

fastapi/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from typing import Optional
2+
3+
from pydantic import BaseModel
4+
5+
from fastapi import FastAPI
6+
7+
app = FastAPI()
8+
9+
books = [
10+
{"id": 1, "title": "Python Basics", "author": "Real P.", "pages": 635},
11+
{
12+
"id": 2,
13+
"title": "Breaking the Rules",
14+
"author": "Stephen G.",
15+
"pages": 99,
16+
},
17+
]
18+
19+
20+
class Book(BaseModel):
21+
title: str
22+
author: str
23+
pages: int
24+
25+
26+
@app.get("/books")
27+
def get_books(limit: Optional[int] = None):
28+
"""Get all books, optionally limited by count."""
29+
if limit:
30+
return {"books": books[:limit]}
31+
return {"books": books}
32+
33+
34+
@app.get("/books/{book_id}")
35+
def get_book(book_id: int):
36+
"""Get a specific book by ID."""
37+
for book in books:
38+
if book["id"] == book_id:
39+
return book
40+
return {"error": "Book not found"}
41+
42+
43+
@app.post("/books")
44+
def create_book(book: Book):
45+
"""Create a new book entry."""
46+
new_book = {
47+
"id": len(books) + 1,
48+
"title": book.title,
49+
"author": book.author,
50+
"pages": book.pages,
51+
}
52+
books.append(new_book)
53+
return new_book

fastapi/requirements.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
annotated-types==0.7.0
2+
anyio==4.10.0
3+
certifi==2025.8.3
4+
click==8.2.1
5+
dnspython==2.7.0
6+
email_validator==2.2.0
7+
fastapi==0.116.1
8+
fastapi-cli==0.0.8
9+
fastapi-cloud-cli==0.1.5
10+
h11==0.16.0
11+
httpcore==1.0.9
12+
httptools==0.6.4
13+
httpx==0.28.1
14+
idna==3.10
15+
Jinja2==3.1.6
16+
markdown-it-py==4.0.0
17+
MarkupSafe==3.0.2
18+
mdurl==0.1.2
19+
pydantic==2.11.7
20+
pydantic_core==2.33.2
21+
Pygments==2.19.2
22+
python-dotenv==1.1.1
23+
python-multipart==0.0.20
24+
PyYAML==6.0.2
25+
rich==14.1.0
26+
rich-toolkit==0.15.0
27+
rignore==0.6.4
28+
sentry-sdk==2.34.1
29+
shellingham==1.5.4
30+
sniffio==1.3.1
31+
starlette==0.47.2
32+
typer==0.16.0
33+
typing-inspection==0.4.1
34+
typing_extensions==4.14.1
35+
urllib3==2.5.0
36+
uvicorn==0.35.0
37+
uvloop==0.21.0
38+
watchfiles==1.1.0
39+
websockets==15.0.1

langchain-rag-app/source_code_final/chatbot_api/src/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from agents.hospital_rag_agent import hospital_rag_agent_executor
2-
from fastapi import FastAPI
32
from models.hospital_rag_query import HospitalQueryInput, HospitalQueryOutput
43
from utils.async_utils import async_retry
54

5+
from fastapi import FastAPI
6+
67
app = FastAPI(
78
title="Hospital Chatbot",
89
description="Endpoints for a hospital system graph RAG chatbot",

langchain-rag-app/source_code_step_5/chatbot_api/src/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from agents.hospital_rag_agent import hospital_rag_agent_executor
2-
from fastapi import FastAPI
32
from models.hospital_rag_query import HospitalQueryInput, HospitalQueryOutput
43
from utils.async_utils import async_retry
54

5+
from fastapi import FastAPI
6+
67
app = FastAPI(
78
title="Hospital Chatbot",
89
description="Endpoints for a hospital system graph RAG chatbot",

python-serialize/http-payload/fastapi-rest-api/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from datetime import datetime
22
from uuid import UUID, uuid4
33

4-
from fastapi import FastAPI
54
from pydantic import BaseModel, Field
65

6+
from fastapi import FastAPI
7+
78
app = FastAPI()
89

910

0 commit comments

Comments
 (0)