r/FastAPI • • May 20 '22

Question Does async def do anything on its own? ELI5

8 Upvotes

Context to explain why I'm asking something that possibly should be obvious - I'm a hobbyist working on small personal projects and using FastAPI to share stuff via simple web apps with friends and colleagues, so there's a lot I don't know.

I currently have an API that grabs html from a third party API using requests, parses it and builds some stuff with Python and Beautiful Soup, and serves back JSON to a simple html / javascript interface.

On average the profiler says it takes about 1s parsing and processing and 6s with requests stuff, mostly waiting for the third party API, which is not really fast.

FastAPI docs say use async def if you have async code with await, use def if you're communicating with an API or database, use async def if you're not communicating with anything.

So, my question is... Should I consider that on a function by function basis, avoiding async def only on functions that request information from third party apps?

If I'm running this on Heroku and have multiple users / calls at the same time, does that mean that the whole thing will have to wait for one 7s API call to be over before dealing with the next one? Would my ability to serve multiple concurrent users drastically improve if I switched from requests to an async library?

Thanks!


r/FastAPI • • Aug 07 '26

feedback request Built a lightweight FastAPI reverse proxy (24MB RAM) to sanitize PII from SSE streams

8 Upvotes

I recently built an open-source reverse proxy using FastAPI and Uvicorn to handle strict PII redaction for LLM applications.

A common issue with Python based redaction tools is memory bloat from heavy NLP libraries and broken token delivery when intercepting real-time Server-Sent Events (SSE). To keep the resource footprint minimal and the streaming smooth, I structured it around a few specific patterns:

- Minimal Footprint ( approx. 24MB): It bypasses heavy frameworks entirely, using a compiled regex engine combined with a local quantized ONNX runtime model.

- Async Lookahead Buffer: To prevent PII placeholders from fragmenting across split SSE network chunks and leaking raw tags to the UI, I implemented an asynchronous sliding-window buffer that holds back unclosed bracket tags.

- Stateless Vault: Mappings are handled via a transient Redis TTL vault.

It operates as a drop-in base_url substitute for the OpenAI SDK. If you work with FastAPI and async streaming middleware, I would love feedback on the lookahead buffer logic or any edge cases in the request-response cycle!!

Repo: https://github.com/ninadphalak/LLM-Shield-Proxy


r/FastAPI • • Jun 28 '24

Question FastAPI + React

25 Upvotes

Hey
I am using FastAPI and React for an app. I wanted to ask a few questions:

  1. Is this is a good stack?

  2. What is the best way to send sensitive data from frontend to backend and backend to frontend? I know we can use cookies but is there a better way? I get the access token from spotify and then i am trying to send that token to the frontend.

  3. How do I deploy an app like this? Using Docker?

Thanks!


r/FastAPI • • Dec 11 '25

Question How I deploy FastAPI apps to production for $2/month instead of $50+ on Heroku

112 Upvotes

I've been running production FastAPI apps on ultra-cheap VPS ($2-5/month) with the same reliability as expensive platforms.

Here's my complete stack:

Backend: • FastAPI + Uvicorn workers • Systemd for process management (auto-restart on crashes) • Gunicorn as process manager

Infrastructure: • Nginx reverse proxy (handles SSL termination) • Let's Encrypt for free HTTPS certificates (auto-renewal) • UFW firewall + fail2ban • Ubuntu 22.04 LTS

Deployment process:

  1. Git push to VPS

  2. Systemd restarts service automatically

  3. Nginx serves with zero downtime

  4. SSL cert renews via cron

Performance: My production app (shiolplus.com) runs on a $2/month Contabo VPS:

  • Handles Stripe webhooks

  • Serves ML predictions

  • 99.9% uptime over 6 months

  • <100ms response times

Cost comparison:

  • Heroku Eco: $5/month (sleeps after 30min)

  • Heroku Basic: $7/month (no sleep)

  • Render: $7/month

  • Railway: $5/month

  • My setup: $2/month (IONOS VPS) - always on

I documented my full deployment checklist if anyone's interested. Happy to share the Nginx config or systemd service file if helpful.

Questions I can answer:

  • SSL certificate automation

  • Zero-downtime deployments

  • Database setup on same VPS

  • Horizontal scaling strategies


r/FastAPI • • Jul 10 '26

feedback request PEP 835: A native shorthand for Annotated

32 Upvotes

Ivan Levkivskyi and I are proposing a native shorthand for Annotated (PEP 835) to clean up heavy type-hinting patterns. We have confirmed this syntax is fully compatible with FastAPI and works out of the box with zero changes required.

Before:

def create_user(
    id: Annotated[int, Path(gt=0)],
    name: Annotated[str, Query(min_length=3)],
    role: Annotated[str, Query(min_length=2)] = "user"
) -> User: ...

After:

def create_user(  
    id: int @Path(gt=0),  
    name: str @Query(min_length=3),  
    role: str @Query(min_length=2) = "user"  
) -> User: ...

The Spec: PEP 835 Full Draft

We are gathering community sentiment before moving forward. If you are a heavy user of Annotated in FastAPI, your feedback is valuable. You can register your stance in the official Discourse poll before it closes on July 15.


r/FastAPI • • Jul 29 '26

Question Looking for Advice on a Clean FastAPI Backend Architecture for an E-learning Platform

38 Upvotes

Hi everyone,

I'm currently building a fairly large e-learning platform using FastAPI,it's my first project, and before I go too far with development, I'd like to make sure my backend architecture is well designed, scalable, and maintainable.

The project will include features such as:

  • User authentication and authorization (JWT, roles: Admin, Teacher, Student, Parent)

  • Courses and modules

  • Lessons (videos, PDFs, quizzes)

  • Assignments and submissions

  • Exams and grading

  • Progress tracking

  • Notifications

  • Payments/subscriptions (later)

  • File uploads

  • Discussion/comments

  • Certificates

  • REST API (possibly GraphQL in the future)

I'm planning to use:

  • FastAPI

  • SQLAlchemy 2.0

  • Alembic

  • PostgreSQL

  • Pydantic v2

  • JWT Authentication

At the moment, I'm trying to decide on the best project structure. I've seen many different approaches:

  • Traditional layered architecture

  • Clean Architecture

  • Domain-Driven Design (DDD)

  • Hexagonal Architecture

  • Vertical Slice Architecture

  • Feature-based architecture

I'd like to avoid ending up with a project that's difficult to maintain as it grows.

My questions

  1. Which architecture would you recommend for a medium-to-large FastAPI project like this?

  2. How would you organize the folders and modules?

  3. Where should business logic live?

  4. How do you separate models, schemas, services, repositories, and dependencies without creating unnecessary complexity?

  5. Are there any open-source FastAPI projects that you consider good examples of clean architecture?

I'm looking for an architecture that is:

  • Easy to maintain

  • Easy to test

  • Scalable

  • Production-ready

  • Follows FastAPI and Python best practices

If you have an example repository or folder structure that you've used successfully, I'd really appreciate it.

Thank you!


r/FastAPI • • Feb 22 '26

Question Evaluation of the AuthX Library for JWT Authentication in Python

14 Upvotes

Hello everyone,

During the manual implementation of JWT-based authentication in FastAPI, writing the entire logic directly in the codebase, I came across the AuthX library (https://authx.yezz.me) as a possible alternative to simplify this process.

At first glance, it seems to abstract much of the complexity involved in token generation, validation, dependency injection, and overall token management. This could potentially improve productivity and reduce the risk of security mistakes in custom implementations. However, I would like to better understand what it uses internally for JWT handling — whether it is python-jose or PyJWT — especially considering that python-jose appears to have reduced maintenance activity.

I also have a few broader questions:

- Is there another library, besides AuthX, that is more widely adopted or officially recommended for JWT authentication in FastAPI?

- For someone who is still a beginner with FastAPI, is it better to implement JWT “by hand” using a well-established library like PyJWT, in order to fully understand the mechanics?

- Or is it considered good practice to adopt an abstraction layer such as AuthX from the beginning?

My main goal is to understand whether AuthX is a solid and production-ready choice, or if it is more advisable to follow the official FastAPI approach using PyJWT directly and build the authentication flow in a more explicit and controlled way.

I appreciate any insights or recommendations.