SDK Reference
TypeScript SDK
Official TypeScript/JavaScript SDK for CogniKin.
Installation
npm install @cognikin/clientInitialisation
import { CogniKin } from '@cognikin/client';
const brain = new CogniKin({
apiKey: process.env.COGNIKIN_API_KEY,
// Optional configuration
baseUrl: 'https://api.cognikin.me/v1', // Default
timeout: 5000, // Request timeout (ms)
retries: 3, // Auto-retry failed requests
debug: false // Enable debug logging
});Methods
getContext()
const context = await brain.getContext({
userId: 'user_123',
task: 'build a feature',
complexity: 'medium', // Optional
taskType: 'coding', // Optional
metadata: { /* ... */ } // Optional
});reportOutcome()
await brain.reportOutcome({
requestId: context.requestId,
started: true,
completed: true,
timeToStart: 45, // Optional
flowState: true, // Optional
satisfaction: 0.9 // Optional
});updateProfile()
const profile = await brain.updateProfile({
userId: 'user_123',
preferredFraming: 'achievement',
communicationStyle: 'brief_directive'
});TypeScript Types
import type {
GetContextParams,
KnowledgeContext,
OutcomeParams,
KnowledgeProfile,
FramingType,
CommunicationStyle
} from '@cognikin/client';
// Fully typed!
const context: KnowledgeContext = await brain.getContext({
userId: 'user_123',
task: 'test'
});Error Handling
import { CogniKinError } from '@cognikin/client';
try {
const context = await brain.getContext({ userId, task });
} catch (error) {
if (error instanceof CogniKinError) {
console.error({
code: error.code,
message: error.message,
statusCode: error.statusCode
});
}
}Event Listeners
// Listen to all requests
brain.on('request', (params) => {
console.log('CogniKin Request:', params);
});
// Listen to responses
brain.on('response', (context, duration) => {
console.log(`CogniKin responded in ${duration}ms`);
});
// Listen to errors
brain.on('error', (error) => {
console.error('CogniKin Error:', error);
});Framework Examples
Next.js
app/api/agent/route.ts
import { CogniKin } from '@cognikin/client';
import { NextRequest, NextResponse } from 'next/server';
const brain = new CogniKin({ apiKey: process.env.COGNIKIN_API_KEY });
export async function POST(request: NextRequest) {
const { userId, query } = await request.json();
const context = await brain.getContext({ userId, task: query });
return NextResponse.json({ context });
}Express
server.ts
import { CogniKin } from '@cognikin/client';
import express from 'express';
const brain = new CogniKin({ apiKey: process.env.COGNIKIN_API_KEY });
const app = express();
app.post('/api/agent', async (req, res) => {
const context = await brain.getContext({
userId: req.body.userId,
task: req.body.query
});
res.json({ context });
});💡 Tip: The SDK automatically handles retries and rate limiting. No extra configuration needed!