Chainlit can be mounted as a FastAPI sub application.
my_cl_app
Copy
import chainlit as cl@cl.on_chat_startasync def main(): await cl.Message(content="Hello World").send()
main
Copy
from fastapi import FastAPIfrom chainlit.utils import mount_chainlitapp = FastAPI()@app.get("/app")def read_main(): return {"message": "Hello World from main app"}mount_chainlit(app=app, target="my_cl_app.py", path="/chainlit")
In the example above, we have a FastAPI application with a single endpoint /app. We mount the Chainlit application my_cl_app.py to the /chainlit path.Start the FastAPI server:
Copy
uvicorn main:app --host 0.0.0.0 --port 80
When using FastAPI integration, header authentication is the preferred method
for authenticating users. This approach allows Chainlit to delegate the
authentication process to the parent FastAPI application, providing a more
seamless and secure integration.