This guide will teach you how to add Inngest to a FastAPI app and run an Inngest function.
💡 Inngest currently supports FastAPI, Flask, and Tornado. If you'd like support for your favorite framework please open an issue on GitHub!
⚠ Use Python 3.10 or higher.
Create and source virtual environment:
python -m venv .venv && source .venv/bin/activate
pip install fastapi inngest uvicorn
Create a FastAPI app file:
from fastapi import FastAPI app = FastAPI()
Let's add Inngest to the app! We'll do a few things
- Create an Inngest client, which is used to send events to an Inngest server.
- Create an Inngest function, which receives events.
- Serve the Inngest endpoint on the FastAPI app.
from fastapi import FastAPI import inngest import inngest.fast_api # Create an Inngest client inngest_client = inngest.Inngest( app_id="fast_api_example", logger=logging.getLogger("uvicorn"), ) # Create an Inngest function @inngest.create_function( fn_id="my_function", # Event that triggers this function trigger=inngest.TriggerEvent(event="app/my_function"), ) async def my_function(ctx: inngest.Context, step: inngest.Step) -> str: ctx.logger.info(ctx.event) return "done" app = FastAPI() # Serve the Inngest endpoint inngest.fast_api.serve(app, inngest_client, [my_function])
Start your app:
uvicorn main:app --reload
Inngest functions are run using an Inngest server. The Inngest Dev Server is the standard way to locally run an Inngest server:
npx inngest-cli@latest dev
After a few seconds, your app and function should now appear in the Dev Server UI:
💡 You can register multiple apps and multiple functions within each app.
Click the function's "Trigger" button and a run should appear in the Dev Server stream tab: