Skip to main content
CID222Documentation

JavaScript SDK

The official CID222 JavaScript SDK provides a type-safe, easy-to-use interface for integrating with CID222 in Node.js and browser environments.

Coming Soon

The official SDK is currently in development. In the meantime, use the REST API directly as shown in the Integration Examples.

Installation

npm
npm install @cid222/sdk
yarn
yarn add @cid222/sdk
pnpm
pnpm add @cid222/sdk

Quick Start

Basic Usage
1import { CID222 } from '@cid222/sdk';
2
3const client = new CID222({
4 apiKey: process.env.CID222_API_KEY,
5});
6
7// Simple chat completion
8const response = await client.chat.completions.create({
9 model: 'gpt-4',
10 messages: [
11 { role: 'user', content: 'Hello, world!' }
12 ]
13});
14
15console.log(response.choices[0].message.content);

Streaming

Stream Response
1import { CID222 } from '@cid222/sdk';
2
3const client = new CID222({
4 apiKey: process.env.CID222_API_KEY,
5});
6
7const stream = await client.chat.completions.create({
8 model: 'gpt-4',
9 messages: [
10 { role: 'user', content: 'Write a poem about privacy.' }
11 ],
12 stream: true
13});
14
15for await (const chunk of stream) {
16 const content = chunk.choices[0]?.delta?.content;
17 if (content) {
18 process.stdout.write(content);
19 }
20}

Sessions

Session Management
1import { CID222 } from '@cid222/sdk';
2
3const client = new CID222({
4 apiKey: process.env.CID222_API_KEY,
5});
6
7// Create a session
8const session = await client.sessions.create({
9 provider: 'openai',
10 model: 'gpt-4',
11 systemPrompt: 'You are a helpful assistant.',
12 metadata: {
13 userId: 'user-123'
14 }
15});
16
17// Send messages in the session
18const response = await client.sessions.messages.create(session.id, {
19 content: 'What can you help me with?'
20});
21
22// List all sessions
23const sessions = await client.sessions.list({ limit: 10 });
24
25// Delete session
26await client.sessions.delete(session.id);

Error Handling

Type-Safe Errors
1import { CID222, APIError, RateLimitError, ContentBlockedError } from '@cid222/sdk';
2
3const client = new CID222({ apiKey: process.env.CID222_API_KEY });
4
5try {
6 const response = await client.chat.completions.create({
7 model: 'gpt-4',
8 messages: [{ role: 'user', content: userInput }]
9 });
10} catch (error) {
11 if (error instanceof ContentBlockedError) {
12 console.log('Content was blocked:', error.reason);
13 console.log('Category:', error.category);
14 } else if (error instanceof RateLimitError) {
15 console.log('Rate limited. Retry after:', error.retryAfter);
16 } else if (error instanceof APIError) {
17 console.log('API error:', error.message);
18 console.log('Status:', error.status);
19 } else {
20 throw error;
21 }
22}

TypeScript Support

The SDK is written in TypeScript and provides full type definitions:

Type Definitions
import type {
ChatCompletionRequest,
ChatCompletionResponse,
Session,
Detection,
EntityType
} from '@cid222/sdk';
const request: ChatCompletionRequest = {
model: 'gpt-4',
messages: [
{ role: 'system', content: 'You are helpful.' },
{ role: 'user', content: 'Hello!' }
],
temperature: 0.7,
stream: false
};

Configuration Options

Client Configuration
import { CID222 } from '@cid222/sdk';
const client = new CID222({
apiKey: process.env.CID222_API_KEY,
// Optional configuration
baseURL: 'https://api.cid222.ai', // Custom endpoint
timeout: 30000, // Request timeout in ms
maxRetries: 3, // Automatic retries
// Custom headers
defaultHeaders: {
'X-Custom-Header': 'value'
}
});

Next Steps