How to Use Prisma with FastAPI: Simple Integration Guide
To use
Prisma with FastAPI, install the Prisma client for Python, generate the client from your Prisma schema, and then import and use it asynchronously in your FastAPI routes. This setup allows you to perform database operations efficiently with async/await syntax inside FastAPI endpoints.Syntax
Here is the basic syntax pattern to use Prisma client in FastAPI:
- Import
Prismafromprismapackage. - Create an instance of
Prisma(). - Connect to the database asynchronously using
await prisma.connect(). - Use Prisma client methods like
prisma.model.find_many()inside async route functions. - Disconnect with
await prisma.disconnect()when done.
python
from fastapi import FastAPI from prisma import Prisma app = FastAPI() prisma = Prisma() @app.on_event("startup") async def startup(): await prisma.connect() @app.on_event("shutdown") async def shutdown(): await prisma.disconnect() @app.get("/items") async def read_items(): items = await prisma.item.find_many() return items
Example
This example shows a complete FastAPI app using Prisma to fetch all records from an Item table asynchronously.
It demonstrates connecting Prisma on startup, disconnecting on shutdown, and querying the database in a GET endpoint.
python
from fastapi import FastAPI from prisma import Prisma app = FastAPI() prisma = Prisma() @app.on_event("startup") async def startup(): await prisma.connect() @app.on_event("shutdown") async def shutdown(): await prisma.disconnect() @app.get("/items") async def get_items(): items = await prisma.item.find_many() return items
Output
[{"id":1,"name":"Apple"},{"id":2,"name":"Banana"}]
Common Pitfalls
1. Forgetting to run await prisma.connect() before queries: This causes connection errors.
2. Using Prisma client synchronously: Prisma client methods are async and must be awaited.
3. Not disconnecting Prisma on shutdown: Can cause resource leaks.
4. Missing Prisma schema generation: Always run prisma generate after schema changes.
python
from fastapi import FastAPI from prisma import Prisma app = FastAPI() prisma = Prisma() @app.get("/items") async def get_items(): # Wrong: missing await prisma.connect() on startup # Wrong: calling async method without await items = await prisma.item.find_many() # Added await return items # Correct usage requires async def startup with await prisma.connect()
Quick Reference
- Install Prisma client:
pip install prisma - Generate client:
prisma generateafter defining schema.prisma - Connect on startup:
await prisma.connect() - Disconnect on shutdown:
await prisma.disconnect() - Use async Prisma calls:
await prisma.model.find_many()
Key Takeaways
Always connect Prisma asynchronously on FastAPI startup before database queries.
Use async/await with Prisma client methods inside FastAPI route handlers.
Run prisma generate after any schema changes to update the client.
Disconnect Prisma on FastAPI shutdown to free resources.
Avoid calling Prisma methods without await to prevent runtime errors.