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';23const client = new CID222({4 apiKey: process.env.CID222_API_KEY,5});67// Simple chat completion8const response = await client.chat.completions.create({9 model: 'gpt-4',10 messages: [11 { role: 'user', content: 'Hello, world!' }12 ]13});1415console.log(response.choices[0].message.content);
Streaming
Stream Response
1import { CID222 } from '@cid222/sdk';23const client = new CID222({4 apiKey: process.env.CID222_API_KEY,5});67const stream = await client.chat.completions.create({8 model: 'gpt-4',9 messages: [10 { role: 'user', content: 'Write a poem about privacy.' }11 ],12 stream: true13});1415for 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';23const client = new CID222({4 apiKey: process.env.CID222_API_KEY,5});67// Create a session8const 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});1617// Send messages in the session18const response = await client.sessions.messages.create(session.id, {19 content: 'What can you help me with?'20});2122// List all sessions23const sessions = await client.sessions.list({ limit: 10 });2425// Delete session26await client.sessions.delete(session.id);
Error Handling
Type-Safe Errors
1import { CID222, APIError, RateLimitError, ContentBlockedError } from '@cid222/sdk';23const client = new CID222({ apiKey: process.env.CID222_API_KEY });45try {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 configurationbaseURL: 'https://api.cid222.ai', // Custom endpointtimeout: 30000, // Request timeout in msmaxRetries: 3, // Automatic retries// Custom headersdefaultHeaders: {'X-Custom-Header': 'value'}});
Next Steps
- API Reference — Full API documentation
- Integration Examples — More code examples
- Error Handling — Handle errors gracefully