Custom API Endpoints
Chainlit is running a Fast API server that you can extend with your own endpoints. One good use case for this is to serve an assistant through a rest API.
If you create or update a custom endpoint, you will have to restart your Chainlit process. The file watcher will not pick up changes.
How it works
To add a custom endpoint, import the FastAPI app
instance from chainlit.server
and declare your endpoint as you would in FastAPI.
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.
Was this page helpful?