API
You can mount your Chainlit app on an existing FastAPI app to create custom endpoints. One good use case for this is to serve an assistant through a rest API.
How it works
To mount your Chainlit app on an existing FastAPI app, you will have to define a subpath for your Chainlit app.
Now run your FastAPI app and you will be able to access your Chainlit app at /chainlit
.
Use Chainlit APIs in your endpoint
To use Chainlit APIs, a Chainlit context is required.
HTTP context
In an HTTP context, Chainlit APIs such as Message.send() will do nothing.
If data persistence is enabled, the Chainlit APIs will still persist data.
Websocket context
The only use case that requires to use the Websocket context within a custom endpoint is to send data to a websocket client (which you know the session ID of) based on some arbitrary HTTP request.
Authentication
You can use any authentication system since the request is accessible. However Chainlit authentication is fully compatible with custom endpoints.
Once an endpoint is protected, it will require to have a valid token in the Authorization
header.
Generate a token
The token is the same token generated when you login in the Chainlit app. You can generate a token manually for a given user with this python script.
This will require to have a CHAINLIT_AUTH_SECRET. You can run chainlit create-secret
to create one.