Node.js Examples

Panda World is compatible with the official OpenAI Node.js SDK. Install it and switch the baseURL.

Installation

npm install openai

Basic Chat Completion

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.relay-station.com/v1",
  apiKey: "sk-your-key-here",
});

const response = await client.chat.completions.create({
  model: "deepseek-chat",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Explain quantum computing in 3 sentences." },
  ],
  temperature: 0.7,
  max_tokens: 200,
});

console.log(response.choices[0].message.content);

Streaming

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.relay-station.com/v1",
  apiKey: "sk-your-key-here",
});

const stream = await client.chat.completions.create({
  model: "deepseek-chat",
  messages: [{ role: "user", content: "Write a short poem." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || "");
}

Environment Variable

export RELAY_API_KEY="sk-your-key-here"
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.relay-station.com/v1",
  apiKey: process.env.RELAY_API_KEY,
});

Error Handling

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.relay-station.com/v1",
  apiKey: "sk-your-key-here",
});

try {
  const response = await client.chat.completions.create({
    model: "deepseek-chat",
    messages: [{ role: "user", content: "Hello!" }],
  });
} catch (error) {
  if (error instanceof OpenAI.AuthenticationError) {
    console.error("Invalid API key");
  } else if (error instanceof OpenAI.RateLimitError) {
    console.error("Rate limit exceeded");
  } else {
    console.error("API error:", error.message);
  }
}

Multi-turn Conversation

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.relay-station.com/v1",
  apiKey: "sk-your-key-here",
});

const messages = [
  { role: "system", content: "You speak like a pirate." },
  { role: "user", content: "What's the weather like?" },
];

// First turn
const res1 = await client.chat.completions.create({
  model: "deepseek-chat",
  messages,
});
messages.push(res1.choices[0].message);
messages.push({ role: "user", content: "Recommend a good ship." });

// Second turn with history
const res2 = await client.chat.completions.create({
  model: "deepseek-chat",
  messages,
});
console.log(res2.choices[0].message.content);

TypeScript

The OpenAI SDK includes full TypeScript types:

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.relay-station.com/v1",
  apiKey: process.env.RELAY_API_KEY!,
});

// Response is fully typed
const response: OpenAI.Chat.Completions.ChatCompletion =
  await client.chat.completions.create({
    model: "deepseek-chat",
    messages: [{ role: "user", content: "Hello!" }],
  });

console.log(response.usage?.total_tokens);