betach
v1.0.4
Published
Node.js Character.AI Unofficial API
Downloads
1
Maintainers
Readme
characterai.js
Node.js Character.AI Unofficial API
Index
Installation
Installation is done using the npm install command:
npm install characterai.js
Use
import CharacterAI from "characterai.js";
const message = "YOUR MESSAGE";
const characterAI = new CharacterAI(AI_KEY, CHARACTER_ID);
const chat = await characterAI.continueOrCreateChat();
const response = await chat.sendAndAwaitResponse({ message, singleReply: true });
console.log(response);
Get AI_KEY
and CHARACTER_ID
AI_KEY
- Open your browser and press
F12
. - Go to https://beta.character.ai/.
- Click on the
Network
tab. - Click on
Fetch/XHR
filter. - Find and click on
auth0/
. - Go to the
Response
tab. - Copy the key.
CHARACTER_ID
- Open your character link in your browser.
- Copy the last part of the URL after
char=
that's your character ID.
Examples
List of examples of ways to use this package.
Text a character via the command-line
Text a character using a command prompt using the native readline
node package.
Install the necessary packages.
npm install dotenv characterai.js
Create a
.env
file in your project's root folder and paste your credentials.CHARACTER_ID = "" AI_KEY = ""
Code: cmd-chat.js
import CharacterAI from "characterai.js";
import { createInterface } from "readline";
import * as dotenv from "dotenv";
dotenv.config();
const getCharResponse = async (message) => {
const characterAI = new CharacterAI(process.env.AI_KEY, process.env.CHARACTER_ID);
const chat = await characterAI.continueOrCreateChat();
const response = await chat.sendAndAwaitResponse({ message, singleReply: true });
return response;
};
const readline = createInterface({
input: process.stdin,
output: process.stdout,
});
const chat = () => readline.question("Message: ", async (msg) => {
if (msg == "exit") {
return readline.close();
}
console.log("Response: ", await getCharResponse(msg), "\n");
chat();
});
chat();
Discord bot integration
Text a character via Discord bot using the discord.js
package. The bot looks for a message with an !ai
prefix.
Install the necessary packages.
npm install discord.js dotenv characterai.js
Create a
.env
file in your project's root folder and paste your credentials.DISCORD_TOKEN = " " CHARACTER_ID = "" AI_KEY = ""
Code: discord-bot.js
import { Client, GatewayIntentBits } from "discord.js";
import CharacterAI from "characterai.js";
import * as dotenv from "dotenv";
dotenv.config();
const getCharResponse = async (message) => {
const characterAI = new CharacterAI(process.env.AI_KEY, process.env.CHARACTER_ID);
const chat = await characterAI.continueOrCreateChat();
const response = await chat.sendAndAwaitResponse({ message, singleReply: true });
return response;
};
const client = new Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildIntegrations
] });
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", async message => {
if (message.content.startsWith("!ai")) {
await message.channel.sendTyping();
const mensaje = message.content.split("!ai ")[1];
const { username } = message.author;
try {
const response = await getCharResponse(`${username} says:\n${mensaje}`);
await message.reply(`${response}`);
}
catch (error) {
console.log(error);
}
}
});
client.login(process.env.DISCORD_TOKEN);