Message a chatbot
The Chatbot Interaction API allows you to interact with your chatbots using a POST
request. This API is available for users subscribed to a paid plan and provides a way to communicate with your chatbot programmatically.
Endpoint
Request Headers
The API request must include the following headers:
Authorization: Bearer <Your-Secret-Key>
- The secret key for authenticating the API request.Content-Type: application/json
- The content type of the request payload.
Request Body
The request body should contain the following parameters:
messages
(array, required): An array containing the ALL the messages between the user and the assistant. Each message object should havecontent
androle
properties. Thecontent
field represents the text content of the message, and therole
field can be either “user” or “assistant” to indicate the sender of the message.chatbotId
(string, required): Refers to the ID of the chatbot you want to interact with (found on the chatbot settings page).stream
(boolean, optional, defaults to false): A boolean value indicating whether to stream back partial progress or wait for the full response. If set totrue
, words will be sent back as data-only server-sent events as they become available, with the stream terminated by adata: [DONE]
message. If set tofalse
, the full response will be returned once it’s ready.temperature
(number, optional, defaults to 0): What sampling temperature to use, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.conversationId
(string, optional): Refers to the ID of the current conversation. The only purpose of this is to save the conversation to the chatbot dashboard. If not provided, the conversation will not be saved. This ID should be generated on your end. You should have different IDs for different conversations and make requests using the same ID for the same conversation. Every time you make a send a message with a specific chatbotId, the last message in the messages array and the response will be added to the conversation.model
(string, optional): If this is added to the body, it takes precedence over the model set in the chatbot settings. If not set, the model set in the chatbot settings is used. The option for ‘gpt-4 | gpt-4-turbo’ only works on the Standard and the Unlimited plans.- OpenAI models:
gpt-3.5-turbo
gpt-4-turbo
gpt-4
gpt-4o
gpt-4o-mini
- Claude models:
claude-3-5-sonnet
claude-3-haiku
claude-3-opus
- Gemini models:
gemini-1.5-flash
gemini-1.5-pro
- OpenAI models:
- Example:
Example Request
Response
The API response will be a JSON object with the following structure:
Example Request with Streaming Functionality
If the stream
parameter is set to true
, words will be sent back as data-only server-sent events as they become available. To read the stream, you can use the following code snippets:
Saving Conversations
In order to save conversations to the dashboard, a conversationId
parameter needs to be included in the request body. The full conversation needs to be sent on every API call because Chatbase doesn’t save previous messages. The messages received in the latest API call for a given conversationId
overrides the conversation already saved there.
Error Handling
If there are any errors during the API request, appropriate HTTP status codes will be returned along with error messages in the response body. Make sure to handle these errors gracefully in your application.
That’s it! You should now be able to message a chatbot using the Message API.