Starlette & FastAPI Package
Add the Asyntai AI chat assistant to your Starlette or FastAPI site
Overview
Starlette is a Python web framework, and FastAPI is built on it. The Asyntai package is a small middleware. Install it, add one line with your widget ID, and the chat bubble is on every HTML page of your site. You edit no template.
The assistant answers your visitors from your own content, in more than 80 languages, day and night.
Requirements
- Python 3.10 or later
- Starlette 0.27 or later, or a FastAPI version that uses it
- An Asyntai account. The Free plan is enough to try the widget
Installation
Follow these steps to add Asyntai to your site:
Get Your Widget ID
Sign in to Asyntai and open the Install page. The widget ID is the value of data-asyntai-id in the snippet shown there. It starts with asyntai_.
Install the Package
pip install starlette-ai-chatbot
Add the Middleware
For FastAPI:
from fastapi import FastAPI
from starlette_ai_chatbot import AsyntaiWidgetMiddleware
app = FastAPI()
app.add_middleware(AsyntaiWidgetMiddleware, widget_id="asyntai_1a2b3c4d5e6f")
For Starlette:
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_ai_chatbot import AsyntaiWidgetMiddleware
app = Starlette(
routes=routes,
middleware=[
Middleware(AsyntaiWidgetMiddleware, widget_id="asyntai_1a2b3c4d5e6f"),
],
)
You can paste the whole snippet from the Install page. The package keeps only the ID.
Open Your Site
Restart the server and open any page. The chat bubble is in the corner of every HTML page.
Options
| Option | Default | What it does |
|---|---|---|
widget_id | the ASYNTAI_WIDGET_ID environment variable | Your Asyntai widget ID. An empty value switches the chat off |
skip_paths | ["/admin*", "/docs", "/docs/oauth2-redirect", "/redoc"] | Paths where the chat never shows. A path that ends in * also covers everything below it |
show_to_anonymous_only | False | Set it to True to hide the chat from signed-in users. Needs Starlette's AuthenticationMiddleware |
script_url | the Asyntai CDN | Advanced. Change it only if Asyntai support asks you to |
Leave out widget_id to keep the ID in an environment variable. It is read on every request:
app.add_middleware(AsyntaiWidgetMiddleware) # reads ASYNTAI_WIDGET_ID
Placing the Widget by Hand
If you use Jinja2 templates and prefer to choose where the widget goes, leave the middleware out. Register the helper once:
from starlette.templating import Jinja2Templates
from starlette_ai_chatbot import asyntai_widget
templates = Jinja2Templates(directory="templates")
templates.env.globals["asyntai_widget"] = asyntai_widget
Then put it just before </body> in your base template:
{{ asyntai_widget() }}
</body>
The helper and the middleware never both add the widget, so it is safe to keep both.
How It Works
The middleware adds a small loader to every HTML page, just before </body>. The loader waits for the page's load event, so your own content always renders first.
The middleware leaves alone JSON and other non-HTML responses, error pages, compressed responses, websockets, the FastAPI API docs, and pages that already have the widget. HTML files served by StaticFiles get the widget too.
Tip: If you use GZipMiddleware, it must run outside the Asyntai middleware. With app.add_middleware(...), add GZipMiddleware last. In a middleware=[...] list, put it first.
Answering From Your Content
Asyntai reads your published pages. Add your site URL in the dashboard, and the assistant answers from the same pages the widget sits on. You can also add documents, and question and answer pairs.
Removing It
Remove the middleware line, or clear the widget ID, and restart the server. The chat bubble is gone.
Plans & Pricing
The package is free. View pricing.
Need Help?
If you have any questions or run into issues, contact us at [email protected].