Skip to content

Node.js SDK Guide

Node.js SDK Guide

Installation

Terminal window
npm install openai

Setup

import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'your-api-key',
baseURL: 'https://cryptgpt.co/v1',
});

Basic Usage

const response = await client.chat.completions.create({
model: 'gemma-4-26b',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello!' },
],
});
console.log(response.choices[0].message.content);

Streaming

const stream = await client.chat.completions.create({
model: 'gemma-4-26b',
messages: [{ role: 'user', content: 'Tell me a story' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Error Handling

try {
const response = await client.chat.completions.create(...);
} catch (error) {
if (error.status === 401) {
console.error('Invalid API key');
} else if (error.status === 429) {
console.error('Rate limited');
}
}