diff --git a/fastapi/README.md b/fastapi/README.md new file mode 100644 index 0000000000..c6dd0e70ca --- /dev/null +++ b/fastapi/README.md @@ -0,0 +1,15 @@ +# Get Started With FastAPI + +This repository contains code snippets discussed in the associated tutorial on [Get Started With FastAPI](https://realpython.com/get-started-with-fastapi/). + +## Installation + +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: + +```console +$ python -m pip install "fastapi[standard]" +``` + +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. + +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. diff --git a/fastapi/books_api.py b/fastapi/books_api.py new file mode 100644 index 0000000000..32c6027934 --- /dev/null +++ b/fastapi/books_api.py @@ -0,0 +1,43 @@ +from typing import Optional + +from fastapi import FastAPI +from pydantic import BaseModel + +app = FastAPI() + +books = [ + {"id": 1, "title": "Python Basics", "author": "Real P.", "pages": 635}, + {"id": 2, "title": "Breaking the Rules", "author": "Stephen G.", "pages": 99}, +] + +class Book(BaseModel): + title: str + author: str + pages: int + +@app.get("/books") +def get_books(limit: Optional[int] = None): + """Get all books, optionally limited by count.""" + if limit: + return {"books": books[:limit]} + return {"books": books} + +@app.get("/books/{book_id}") +def get_book(book_id: int): + """Get a specific book by ID.""" + for book in books: + if book["id"] == book_id: + return book + return {"error": "Book not found"} + +@app.post("/books") +def create_book(book: Book): + """Create a new book entry.""" + new_book = { + "id": len(books) + 1, + "title": book.title, + "author": book.author, + "pages": book.pages + } + books.append(new_book) + return new_book diff --git a/fastapi/main.py b/fastapi/main.py new file mode 100644 index 0000000000..7d44e7a44c --- /dev/null +++ b/fastapi/main.py @@ -0,0 +1,7 @@ +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def home(): + return {"message": "Hello, FastAPI!"} diff --git a/fastapi/requirements.txt b/fastapi/requirements.txt new file mode 100644 index 0000000000..5a7d3b17b5 --- /dev/null +++ b/fastapi/requirements.txt @@ -0,0 +1,39 @@ +annotated-types==0.7.0 +anyio==4.10.0 +certifi==2025.8.3 +click==8.2.1 +dnspython==2.7.0 +email_validator==2.2.0 +fastapi==0.116.1 +fastapi-cli==0.0.8 +fastapi-cloud-cli==0.1.5 +h11==0.16.0 +httpcore==1.0.9 +httptools==0.6.4 +httpx==0.28.1 +idna==3.10 +Jinja2==3.1.6 +markdown-it-py==4.0.0 +MarkupSafe==3.0.2 +mdurl==0.1.2 +pydantic==2.11.7 +pydantic_core==2.33.2 +Pygments==2.19.2 +python-dotenv==1.1.1 +python-multipart==0.0.20 +PyYAML==6.0.2 +rich==14.1.0 +rich-toolkit==0.15.0 +rignore==0.6.4 +sentry-sdk==2.34.1 +shellingham==1.5.4 +sniffio==1.3.1 +starlette==0.47.2 +typer==0.16.0 +typing-inspection==0.4.1 +typing_extensions==4.14.1 +urllib3==2.5.0 +uvicorn==0.35.0 +uvloop==0.21.0 +watchfiles==1.1.0 +websockets==15.0.1