@cored-im/sdk
v0.28.108
Published
Official Cored OpenAPI SDK for Node.js and Browser
Downloads
449
Maintainers
Readme
Cored IM OpenAPI SDK - JavaScript
English | 中文
Cored is a secure, self-hosted productivity platform for teams, integrating instant messaging, organizational structures, video conferencing, and file storage.
This is the official JavaScript SDK for Cored server, used to interact with the Cored server via OpenAPI. You need to deploy the Cored server before using this SDK. See the Quick Deploy Guide for setup instructions.
Installation
npm install @cored-im/sdkWebSocket is natively supported in browsers and Node.js 22+. For Node.js < 22, install ws:
npm install wsQuick Start
import { CoredClient, MessageType_TEXT } from '@cored-im/sdk';
const client = await CoredClient.create(
'https://your-backend-url.com',
'your-app-id',
'your-app-secret',
);
// Optional: preheat fetches access token and syncs server time upfront,
// reducing latency on the first API call
await client.preheat();
// Call API
const resp = await client.Im.Message.sendMessage({
chat_id: 'chat-id',
message_type: MessageType_TEXT,
message_content: { text: { content: 'Cored new version released!' } },
});
console.log(resp);
// Close when done
await client.close();Configuration
CoredClient.create() accepts an optional options object to configure client behavior:
import { CoredClient, LoggerLevel } from '@cored-im/sdk';
const client = await CoredClient.create(
'https://your-backend-url.com',
'your-app-id',
'your-app-secret',
{
logLevel: LoggerLevel.Debug, // Log level (default: Info)
requestTimeout: 30_000, // Request timeout in ms (default: 60000)
enableEncryption: false, // Enable request encryption (default: true)
},
);Event Subscription
Receive real-time events via WebSocket:
// Register event handler
const handlerId = client.Im.Message.Event.onMessageReceive((event) => {
console.log('Message received:', event);
});
// Unsubscribe
client.Im.Message.Event.offMessageReceive(handlerId);Error Handling
API responses include code and msg fields. A code of 0 indicates success:
const resp = await client.Im.Message.sendMessage({
chat_id: 'chat-id',
message_type: MessageType_TEXT,
message_content: { text: { content: 'Cored new version released!' } },
});
if (resp.code !== 0) {
console.error(`Request failed: code=${resp.code}, msg=${resp.msg}`);
}Requirements
- Node.js 18+ (requires
fetch,crypto.subtle,CompressionStream) - Browsers: Modern browsers with Web Crypto API and CompressionStream support
- TypeScript 5.0+ (optional, type declarations are included)
