Complete guide to integrating with Onewise Chat API. Send messages, receive real-time events, and build powerful chat applications with our comprehensive API.
All API requests require an access token for authentication. Include your access token in the request headers or query parameters.
const accessToken = "*********************************************************************";
https://chatapi.onewise.app
https://web.onewise.app
Send a message to a specific chat room or group.
Header | Value | Required |
---|---|---|
Content-Type |
application/json |
โ |
Parameter | Type | Required | Description |
---|---|---|---|
accessToken |
string | โ | Authentication token |
roomId |
string | โ | Target room/group identifier |
content |
object | โ | Message content object |
content.text |
string | โ | Message text content |
content.attachments |
array | โ | Array of file attachments |
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
const raw = JSON.stringify({
"accessToken": "*********************************************************************",
"roomId": "21aa95e6-5aad-452a-aa64-592881bbc842",
"content": {
"text": "Link nhรณm: https://web.onewise.app/c/a59c709c-36f1-444d-88ce-77fc83ae9056",
"attachments": []
}
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow"
};
fetch("https://chatapi.onewise.app/api/bot-onews/send", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.error(error));
const messageWithAttachments = {
"accessToken": "your_access_token",
"roomId": "room_id",
"content": {
"text": "Check out these files!",
"attachments": [
{
"type": "image",
"name": "screenshot.png",
"url": "https://example.com/files/screenshot.png",
"size": 245760
},
{
"type": "document",
"name": "report.pdf",
"url": "https://example.com/files/report.pdf",
"size": 1048576
}
]
}
};
Create a new bot that can send and receive messages. Once created, you can interact with the bot directly or add it to groups for multi-user access.
This endpoint requires Bearer token authentication. Include your JWT token in the Authorization header.
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Header | Value | Required |
---|---|---|
Content-Type |
application/json |
โ |
Authorization |
Bearer {jwt_token} |
โ |
Accept |
application/json, text/plain, */* |
โ |
Origin |
https://web.onewise.app |
โ |
Parameter | Type | Required | Description |
---|---|---|---|
name |
string | โ | Display name for the bot |
username |
string | โ | Unique username for the bot (used for identification) |
const myHeaders = new Headers();
myHeaders.append("Accept", "application/json, text/plain, */*");
myHeaders.append("Accept-Language", "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5");
myHeaders.append("Access-Control-Allow-Origin", "*");
myHeaders.append("Authorization", "Bearer eyJhbGciOiJIU*********************************************************************");
myHeaders.append("Connection", "keep-alive");
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Origin", "https://web.onewise.app");
myHeaders.append("Sec-Fetch-Dest", "empty");
myHeaders.append("Sec-Fetch-Mode", "cors");
myHeaders.append("Sec-Fetch-Site", "cross-site");
myHeaders.append("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36");
myHeaders.append("sec-ch-ua", "\"Chromium\";v=\"136\", \"Google Chrome\";v=\"136\", \"Not.A/Brand\";v=\"99\"");
myHeaders.append("sec-ch-ua-mobile", "?0");
myHeaders.append("sec-ch-ua-platform", "\"Windows\"");
const raw = JSON.stringify({
"name": "Binh FPT Notify",
"username": "binh_notice_bot"
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow"
};
fetch("https://chatapi.onewise.app/api/bot/create", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.error(error));
{
"success": true,
"bot": {
"id": "bot_12345",
"name": "Binh FPT Notify",
"username": "binh_notice_bot",
"accessToken": "bot_access_token_here",
"createdAt": "2024-01-01T12:00:00Z"
}
}
Use the bot creation API to generate a new bot with a unique username and display name.
Save the bot's access token from the creation response - you'll need this for sending messages.
Users can message the bot directly, or add the bot to group chats for multi-user access.
Extract the roomId from incoming messages (works for both private chats and group chats).
Use the same send/receive APIs with the obtained roomId for seamless communication.
// Step 1: Create the bot
async function createBot() {
const response = await fetch("https://chatapi.onewise.app/api/bot/create", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer your_jwt_token"
},
body: JSON.stringify({
"name": "My Notification Bot",
"username": "my_notify_bot"
})
});
const botData = await response.json();
return botData.bot.accessToken;
}
// Step 2: Listen for incoming messages
const createSSEClient = require('sse-client-auth-onewise');
function startBotListener(botAccessToken) {
const sseClient = createSSEClient({
url: 'https://chatapi.onewise.app/bot-listen',
accessToken: botAccessToken,
onMessage: (data) => {
try {
const message = JSON.parse(data);
const roomId = message.roomId; // Extract roomId for responses
console.log(`๐ฉ Received in room ${roomId}: ${message.text}`);
// Auto-respond to messages
if (message.text.toLowerCase().includes('hello')) {
sendBotMessage(botAccessToken, roomId, "Hello! How can I help you?");
}
} catch (error) {
console.error('Error parsing message:', error);
}
},
onError: (error) => {
console.error('โ Bot listener error:', error);
}
});
}
// Step 3: Send messages as the bot
async function sendBotMessage(accessToken, roomId, text) {
const response = await fetch("https://chatapi.onewise.app/api/bot-onews/send", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
accessToken: accessToken,
roomId: roomId,
content: {
text: text,
attachments: []
}
})
});
return response.json();
}
// Initialize the bot
async function initializeBot() {
try {
const botToken = await createBot();
console.log('โ
Bot created successfully');
startBotListener(botToken);
console.log('๐ง Bot is now listening for messages');
// Bot is ready - users can now message it or add it to groups
} catch (error) {
console.error('โ Failed to initialize bot:', error);
}
}
A lightweight SSE client for Node.js with built-in authentication support. Perfect for receiving real-time chat messages and events.
Automatic handling of accessToken and roomId
Easy integration with just a few lines of code
Efficient SSE connection management
Automatic reconnection on connection loss
npm install sse-client-auth-onewise
const createSSEClient = require('sse-client-auth-onewise');
// Create SSE client with authentication
const sseClient = createSSEClient({
url: 'https://chatapi.onewise.app/bot-listen',
accessToken: 'your_access_token_here',
roomId: 'room_123', // optional
onMessage: (data) => {
console.log('๐ฉ Received message:', data);
},
});
// The client will automatically connect and start listening for events
Option | Type | Required | Description |
---|---|---|---|
url |
string | โ | The SSE server endpoint URL |
accessToken |
string | โ | Authentication token sent as query parameter |
roomId |
string | โ | Optional room/channel identifier |
onMessage |
function | โ | Callback function to handle incoming messages |
onError |
function | โ | Optional callback for error handling |
onOpen |
function | โ | Optional callback when connection opens |
const sseClient = createSSEClient({
url: 'https://chatapi.onewise.app/bot-listen',
accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
roomId: 'chat-room-456',
onMessage: (data) => {
try {
const parsed = JSON.parse(data);
console.log('๐ฆ Parsed data:', parsed);
// Handle message content
if (parsed.text) {
console.log('๐ฌ Message:', parsed.text);
}
// Handle attachments
if (parsed.attachments && parsed.attachments.length > 0) {
console.log('๐ Attachments:', parsed.attachments.length);
parsed.attachments.forEach((attachment, index) => {
console.log(` ${index + 1}. ${attachment.name || attachment.url}`);
});
}
} catch (error) {
console.log('๐ Raw message:', data);
}
},
onError: (error) => {
console.error('โ SSE Error:', error);
},
onOpen: () => {
console.log('โ
SSE Connection established');
}
});
Messages are sent and received in JSON format with the following structure:
{
"text": "Hello everyone! ๐",
"attachments": [],
"sender": "user_123",
"timestamp": "2024-01-01T12:00:00Z",
"messageId": "msg_456",
"type": "message"
}
{
"text": "Check out these files!",
"attachments": [
{
"type": "image",
"name": "screenshot.png",
"url": "https://example.com/files/screenshot.png",
"size": 245760
},
{
"type": "document",
"name": "report.pdf",
"url": "https://example.com/files/report.pdf",
"size": 1048576
}
],
"sender": "user_789",
"timestamp": "2024-01-01T12:05:00Z",
"messageId": "msg_789"
}
Type | Description | Example Extensions |
---|---|---|
image |
Image files | .jpg, .png, .gif, .webp |
video |
Video files | .mp4, .avi, .mov, .webm |
audio |
Audio files | .mp3, .wav, .ogg, .m4a |
document |
Document files | .pdf, .doc, .txt, .xlsx |
other |
Other file types | Any other extension |
// User joined
{
"text": "User John joined the conversation",
"attachments": [],
"type": "system",
"timestamp": "2024-01-01T12:10:00Z"
}
// User left
{
"text": "User John left the conversation",
"attachments": [],
"type": "system",
"timestamp": "2024-01-01T12:15:00Z"
}
const groupChatClient = createSSEClient({
url: 'https://chatapi.onewise.app/bot-listen',
accessToken: 'your-access-token',
roomId: 'team-developers',
onMessage: (data) => {
try {
const messageData = JSON.parse(data);
// Handle different message types
switch (messageData.type) {
case 'message':
console.log(`๐ค ${messageData.sender}: ${messageData.text}`);
// Display attachments if any
if (messageData.attachments.length > 0) {
messageData.attachments.forEach(attachment => {
console.log(`๐ ${attachment.type}: ${attachment.name}`);
});
}
break;
case 'user_joined':
console.log(`โ
${messageData.user} joined the group`);
break;
case 'user_left':
console.log(`โ ${messageData.user} left the group`);
break;
}
} catch (error) {
console.error('Failed to parse group message:', error);
}
}
});
const privateChatClient = createSSEClient({
url: 'https://chatapi.onewise.app/bot-listen',
accessToken: 'private-token',
roomId: 'user_123_456', // Private room between users
onMessage: (rawData) => {
try {
const message = JSON.parse(rawData);
const { text, attachments, sender, timestamp } = message;
console.log(`๐ฌ [${new Date(timestamp).toLocaleTimeString()}] ${sender}:`);
console.log(` ${text}`);
// Handle media attachments
if (attachments && attachments.length > 0) {
console.log(`๐ Attachments:`);
attachments.forEach((file, index) => {
const fileType = file.type || 'unknown';
const fileName = file.name || `attachment_${index + 1}`;
const fileSize = file.size ? ` (${(file.size / 1024).toFixed(1)}KB)` : '';
console.log(` ${index + 1}. [${fileType.toUpperCase()}] ${fileName}${fileSize}`);
});
}
} catch (parseError) {
// If not JSON, might be plain text message
console.log(`๐ Raw message: ${rawData}`);
}
},
onError: (error) => {
console.error('โ Private chat connection error:', error);
}
});