0% found this document useful (0 votes)
9 views10 pages

FastAPI API Development Guide

The document outlines the process of creating APIs using FastAPI, covering topics such as CRUD operations, handling validations and errors, and asynchronous programming. It details how to set up routes, return responses, and implement field validation with Pydantic. Additionally, it explains the differences between synchronous and asynchronous programming in Python, emphasizing the use of async and await for non-blocking operations.

Uploaded by

johnalphonso620
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

FastAPI API Development Guide

The document outlines the process of creating APIs using FastAPI, covering topics such as CRUD operations, handling validations and errors, and asynchronous programming. It details how to set up routes, return responses, and implement field validation with Pydantic. Additionally, it explains the differences between synchronous and asynchronous programming in Python, emphasizing the use of async and await for non-blocking operations.

Uploaded by

johnalphonso620
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

0.

Topics
Thursday, May 8, 2025 11:03 PM

1. Creating APIs using FastAPI

2. CRUD Operations

3. Handling Validations and Errors

4. Asynchronous Programming

3. Building APIs Page 30


1. Creating APIs using FastAPI
Saturday, May 10, 2025 12:50 PM

Route/Endpoint in FastAPI:
• A route/endpoint is a specific URL path which our application reaches out to
• Each route is associated with a function (called a path operation function) that executes when that
route is accessed

Returning Responses in FastAPI:

FastAPI allows returning:


○ Dictionaries (converted to JSON automatically)
○ Pydantic models (for validation & serialization)
○ Custom response types (e.g., HTML, plain text, streaming, etc.)

Returning Pydantic Models:


• When we call:

• What FastAPI does under the hood:

HTTP Methods in FastAPI:


3. Building APIs Page 31
HTTP Methods in FastAPI:
• FastAPI supports all standard HTTP methods
• Each method has a semantic purpose in RESTful API design

3. Building APIs Page 32


2. CRUD Operations
Saturday, May 10, 2025 1:04 PM

Create an app using FastAPI to implement CRUD operations on Employees database

Implement endpoints to:


• Show all employees
• Show particular employee
• Add a new employee
• Update an existing employee
• Delete an existing employee

3. Building APIs Page 33


3. Building APIs Page 34
3. Handling Validations and Errors
Monday, May 12, 2025 8:24 AM

1. Field Validation with Pydantic:


○ This can be done using Field, StrictInt, StrictFloat, etc.
○ In Pydantic, Field is used to provide metadata, validations, and default values
for fields in a BaseModel instance
○ Allows for more finer control over input validation and schema generation

Common Parameters of Field:

2. Optional Fields & Default Values:


○ Use Optional from the typing module

3. Custom Error Responses:


○ Can be implemented using HTTPException from FastAPI
○ Helps indicate the status code along with a custom message

3. Building APIs Page 35


3. Building APIs Page 36
4. Asynchronous Programming
Monday, May 12, 2025 9:16 AM

What is Asynchronous Programming?


• Asynchronous programming is a paradigm that allows your program to perform other tasks while waiting for
another operation to complete (ex. database query, API call) to complete, without blocking the execution of
the rest of the code
• In other words, instead of waiting for a task to finish before moving on (blocking), you can start a task, and
then continue doing other things while that task finishes in the background

Synchronous vs Asynchronous:

async and await:

In Python, asynchronous code is written using asyncio module:


• async def: Declares an asynchronous function (called a coroutine)
• await: Tells the interpreter to - pause here and come back when this operation is done

Internal Working:
1. Python uses an event loop (via asyncio) to manage asynchronous tasks
2. Tasks are added to the loop
3. When a task hits await, control is yielded back to the loop
4. The loop runs other tasks in the meantime
5. When the awaited task finishes, the loop resumes it

Use async def if your route does:


• HTTP calls (httpx, aiohttp)
• DB access with async drivers (e.g., asyncpg)
• I/O operations that support async

3. Building APIs Page 37


Use def if:
• Your function is CPU-bound (e.g., ML inference, image processing)
• You're calling a blocking library (e.g., requests, psycopg2)

3. Building APIs Page 38


3. Building APIs Page 39

You might also like