cai.js is a JavaScript library that provides access to the Character.ai API, allowing you to interact with chatbots and create conversational experiences in your JavaScript applications.
You can install the library via npm:
npm install cai.js
To get started with cai.js and initiate a conversation with a chatbot, follow these steps:
const c = require('cai.js');
const cai = new c();
(async () => {
// Authenticate as a guest
await cai.guestAuth();
// Replace with the character ID of your chatbot
const characterId = "M5xMXf4FKepKTYtWPqVaEZzuEuy90uu0eNZr4GZtDsA";
// Create a chat instance
const chat = await cai.createChat(characterId);
// Send a message and await a response
const response = await chat.send('Hello', true);
console.log(response);
})();
You can find the character ID in the URL when you visit your chatbot's page, the character Id is value of char
To generate an image within the conversation, use:
await chat.generateImage("An avenue");
To upload an image from a URL to the conversation, use:
await chat.uploadImage("https://www.imageurl.com/");
You can send a message along with an image:
await chat.send("Hello", { image_rel_path: "https://www.image.com/" }, true);
Fetch the chat history:
await chat.fetchHistory(pageNumber);
Change the conversation by its external ID:
await chat.changeConversationById(conversationExternalId, force = false);
Fetch all saved conversations:
await chat.getSavedConversations(amount = 50);
Retrieve a message by its ID:
await chat.getMessageById(messageId);
Delete a message by its ID:
await chat.deleteMessage(messageId);
Delete multiple messages:
await chat.deleteMessages(messageIds);
Perform bulk message deletion:
await chat.deleteMessagesBulk(amount = 50, descending = false);
Start a new chat:
await chat.newChat();
These functions do not require a chat
object and provide general information:
Fetch chatbot categories:
await fetchCategories();
Fetch user configuration:
await fetchUserConfig();
Fetch user information:
await fetchUser();
Fetch featured chatbots:
await fetchFeaturedCharacters();
Fetch chatbots by categories:
await fetchCharactersByCategory(curate = false);
Fetch information about a specific chatbot:
await fetchCharacterInfo(characterId);
Search for chatbots by name:
await searchCharacters(characterName);
To authenticate with Character.ai, follow these steps:
-
Go to Character.ai.
-
Open your browser's developer tools and navigate to the "Storage" tab, then "Local Storage."
-
Look for the key
@@auth0spajs@@::dyD3gE281MqgISG7FuIXYhL2WEknqZzv::https://auth0.character.ai/::openid profile email offline_access
and open its body to copy the access token. -
You can log in as a guest using
guestAuth()
or log in with your account usingtokenAuth()
.
If you have a c.ai plus account, make sure to update .usePlus
in the requester.js
file and adjust .forceWaitingRoom
accordingly.
Now you're ready to create engaging conversational experiences with cai.js!
Feel free to add your logo and customize this README further to fit your project's needs.