Skip to content

Commit cc009f8

Browse files
committed
Add Materials for the "Get Started With FastAPI" tutorial
1 parent 815c4c6 commit cc009f8

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

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 on [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/books_api.py

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

fastapi/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from fastapi import FastAPI
2+
3+
app = FastAPI()
4+
5+
@app.get("/")
6+
def home():
7+
return {"message": "Hello, FastAPI!"}

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

0 commit comments

Comments
 (0)