Chat

Chatbase offers an API for you to interact with your chatbots if you are subscribed to a paid plan. To get an API key, go to your account page and scroll to the bottom.

Request

To talk to a chatbot you need to make a POST request to https://www.chatbase.co/api/v1/chat with and the Headers: Authorization: Bearer <Your-Secret-Key> and have messages, chatId, temperatureand stream in the body of the request.

chatId is the id in the URL on the chatbot page. It will look something like chatbase--1--pdf-p680fxvnm.

messages is an array holding maximum the last 7 messages between the user and the assistant. It will look like

[
  { content: 'How can i help you?', role: 'assistant' },
  { content: 'What is chatbase?', role: 'user' }
];

stream is a boolean to indicate whether to stream back partial progress or send the full response once it's ready.

Example Requests

cURL

curl https://www.chatbase.co/api/v1/chat \
  -H 'Authorization: Bearer <Your-Secret-Key>' \
  -d '{"messages":[{"content":"How can i help you?","role":"assistant"},{"content":"What is chatbase?","role":"user"}],"chatId":"chatbase--1--pdf-p680fxvnm","stream":false, "temperature": 0}'

Javascript

const response = await fetch('https://www.chatbase.co/api/v1/chat', {
  method: 'POST',
  headers: {
    Authorization: `Bearer <Your-Secret-Key>`
  },
  body: JSON.stringify({
    messages: [
      { content: 'How can I help you?', role: 'assistant' },
      { content: 'What is chatbase?', role: 'user' }
    ],
    chatId: 'chatbase--1--pdf-p680fxvnm',
    stream: false,
    temperature: 0
  })
});

Response

If stream is set to true, words will be sent back as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. If stream is set to false the response will look like

{"text": "Chatbase is an AI chatbot builder that lets you create a GPT based chatbot that knows data."}

To read the stream in javascript

// This data is a ReadableStream
const data = response.body;

if (!data) {
  // error happened
}

const reader = data.getReader();
const decoder = new TextDecoder();
let done = false;

while (!done) {
  const { value, done: doneReading } = await reader.read();
  done = doneReading;
  const chunkValue = decoder.decode(value);
  console.log(chunkValue); // This will log chunks of the chatbot reply until the reply is finished.
}

If you have any more questions on how to use the API message me on twitter.