Node / TypeScript SDK
For Node.js and TypeScript projects, two official packages are recommended:
- OpenAI compatible:
openai(official) - Anthropic native:
@anthropic-ai/sdk(official)
Examples are based on the official READMEs pulled via Context7. Refer to the upstream repos for the latest API: openai/openai-node, anthropics/anthropic-sdk-typescript.
Setup
export SAKRYLLE_API_KEY="sk-xxxxxxxxxxxxxxxx"Node.js 18+ is required (built-in fetch). Both SDKs are written in TypeScript but work fine in plain JavaScript projects.
OpenAI compatible
Install
npm install openai
# or pnpm add openai / yarn add openaiMinimal usage
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.SAKRYLLE_API_KEY,
baseURL: 'https://api.sakrylle.com/v1',
});
const completion = await client.chat.completions.create({
model: 'gpt-5.6-sol',
messages: [
{ role: 'system', content: 'You are a concise assistant.' },
{ role: 'user', content: 'Explain RAG in one sentence.' },
],
});
console.log(completion.choices[0].message.content);OPENAI_API_KEY + OPENAI_BASE_URL env vars also work — new OpenAI() reads them automatically.
Streaming
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.SAKRYLLE_API_KEY,
baseURL: 'https://api.sakrylle.com/v1',
});
const stream = await client.chat.completions.create({
model: 'gpt-5.6-sol',
messages: [{ role: 'user', content: 'Count to 5.' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}Tool calling
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.SAKRYLLE_API_KEY,
baseURL: 'https://api.sakrylle.com/v1',
});
const tools = [
{
type: 'function' as const,
function: {
name: 'get_weather',
description: 'Look up the current weather for a city',
parameters: {
type: 'object',
properties: {
city: { type: 'string', description: 'City name' },
},
required: ['city'],
},
},
},
];
const resp = await client.chat.completions.create({
model: 'gpt-5.6-sol',
messages: [{ role: 'user', content: "What's the weather in Beijing today?" }],
tools,
});
const choice = resp.choices[0];
if (choice.message.tool_calls?.length) {
for (const call of choice.message.tool_calls) {
if (call.type === 'function') {
console.log('call:', call.function.name, JSON.parse(call.function.arguments));
}
}
} else {
console.log(choice.message.content);
}The official SDK also offers
client.chat.completions.runTools(...)for multi-turn tool loops — see openai-node helpers.md.
Anthropic native
Install
npm install @anthropic-ai/sdkMinimal usage
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.SAKRYLLE_API_KEY,
baseURL: 'https://api.sakrylle.com',
});
const message = await client.messages.create({
model: 'claude-haiku-4-5-20251001',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Introduce Claude in one sentence.' }],
});
console.log(message.content);Use
https://api.sakrylle.comasbaseURL; the SDK appends/v1/messagesautomatically.ANTHROPIC_API_KEY/ANTHROPIC_BASE_URLenv vars also work.
Streaming
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.SAKRYLLE_API_KEY,
baseURL: 'https://api.sakrylle.com',
});
const stream = client.messages.stream({
model: 'claude-haiku-4-5-20251001',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Write a four-line poem.' }],
});
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}
const finalMessage = await stream.finalMessage();
console.log('\n[done]', finalMessage.usage);Error handling
import OpenAI, { APIError, AuthenticationError, RateLimitError } from 'openai';
const client = new OpenAI({
apiKey: 'bad-key',
baseURL: 'https://api.sakrylle.com/v1',
});
try {
await client.chat.completions.create({
model: 'gpt-5.6-sol',
messages: [{ role: 'user', content: 'hi' }],
});
} catch (err) {
if (err instanceof AuthenticationError) {
console.error('Key invalid or revoked');
} else if (err instanceof RateLimitError) {
console.error('Rate limited, retry later');
} else if (err instanceof APIError) {
console.error('Upstream error:', err.status, err.message);
} else {
throw err;
}
}For a full error table, see Errors.
