Node.js SDK Guide
Real200 is fully compatible with the OpenAI Node.js SDK. Just modify the baseURL to integrate.
Installationâ
npm install openai
Requires Node.js 18+.
Basic Usageâ
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-real200-xxxxxxxxxxxxxxxx", // Your Real200 API Key
baseURL: "https://real200.com/v1", // Real200 gateway address
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello, please introduce Real200." },
],
temperature: 0.7,
max_tokens: 200,
});
console.log(response.choices[0].message.content);
Streaming Responseâ
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Write a poem about AI." }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
}
Error Handlingâ
try {
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello" }],
});
} catch (error) {
if (error.status === 401) {
console.error("Authentication failed, please check API Key");
} else if (error.status === 429) {
console.error("Rate limit exceeded, please wait and retry");
} else {
console.error("API error:", error.message);
}
}