FastAPI Internals
The Speaker

Marcelo Trylesinski
OSS Maintainer
### Uvicorn

### Starlette

What is FastAPI?

How do I run FastAPI?
uvicorn main:app or...
How do I run FastAPI?
hypercorn main:app or...
How do I run FastAPI?
granian main:app
github.com/emmett-framework/granian
How the interactions happen?

Let's focus on...

But... How it really happens?

ASGI application
Scope = Dict[str, Any]
Receive = Callable[[], Awaitable[Dict[str, Any]]]
Send = Callable[[Dict[str, Any]], Awaitable[None]]
async def app(scope: Scope, receive: Receive, send: Send):
...
Simplest ASGI application
async def app(scope, receive, send):
body = b"Hello world!"
headers = [(b"content-type", b"text/plain"), (b"content-length", str(len(body)).encode())]
await send({"type": "http.response.start", "status": 200, "headers": headers})
await send({"type": "http.response.body", "body": body})
After the connection is established...

If the client sends a body...

Then... It's the application's turn!

Middleware Stack

Middleware Stack
from fastapi import FastAPI
from src.middleware import CustomMiddleware, AnotherCustomMiddleware
app = FastAPI()
app.add_middleware(CustomMiddleware)
app.add_middleware(AnotherCustomMiddleware)
Middleware Stack

Routing

Routing

Dependency Injection

Dependency Injection

Dependency Injection
### Sync
---
## Input/Output Validation
---
## Input/Output Validation

---
## WebSockets

---
## WebSockets

---
## Async vs Sync
When should I use...
---
## Bonus: FastAPI Tips
https://github.com/Kludex/fastapi-tips
---
## Try Logfire! 🚀

---
# Thank You!
[marcelotryle.com](https://www.marcelotryle.com)
Marcelo Trylesinski
@marcelotryle
Kludex