Skip to content

Node.js SDK Integration ​

When using the official OpenAI Node.js SDK, simply replace apiKey, baseURL, and model with CoreRouter information.

Installation ​

bash
npm install openai

Or with Bun:

bash
bun add openai

To directly read from a .env file, also install dotenv:

bash
npm install dotenv

Or:

bash
bun add dotenv

Basic Usage ​

typescript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.COREROUTER_API_KEY,
  baseURL: "https://api.corerouter.cloud/v1",
});

const completion = await client.chat.completions.create({
  model: "claude-sonnet-4-5",
  messages: [
    { role: "user", content: "Hello, please introduce yourself." },
  ],
});

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

Streaming Output ​

typescript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.COREROUTER_API_KEY,
  baseURL: "https://api.corerouter.cloud/v1",
});

const stream = await client.chat.completions.create({
  model: "claude-sonnet-4-5",
  messages: [
    { role: "user", content: "Introduce CoreRouter in three sentences." },
  ],
  stream: true,
});

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

Multi-Turn Conversation ​

typescript
import type { ChatCompletionMessageParam } from "openai/resources/chat/completions";

const messages: ChatCompletionMessageParam[] = [
  { role: "user", content: "Hello" },
];

let completion = await client.chat.completions.create({
  model: "claude-sonnet-4-5",
  messages,
});

messages.push({
  role: "assistant",
  content: completion.choices[0].message.content ?? "",
});
messages.push({ role: "user", content: "Please write a TypeScript hello function." });

completion = await client.chat.completions.create({
  model: "claude-sonnet-4-5",
  messages,
});

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

Tool Calling ​

typescript
const completion = await client.chat.completions.create({
  model: "claude-sonnet-4-5",
  messages: [{ role: "user", content: "What's the weather like in Beijing?" }],
  tools: [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "Get weather information for a specified city",
        parameters: {
          type: "object",
          properties: {
            city: { type: "string", description: "City name" },
          },
          required: ["city"],
        },
      },
    },
  ],
});

const toolCall = completion.choices[0].message.tool_calls?.[0];
if (toolCall) {
  console.log(toolCall.function.name);
  console.log(toolCall.function.arguments);
}

Responses API ​

This example requires the Model ID to support Responses. Regular chat models may not work. If client.responses doesn't exist, upgrade the openai package first, or call directly following Responses API curl examples.

typescript
const response = await client.responses.create({
  model: "responses-model-id",
  input: "Introduce CoreRouter in three sentences.",
});

console.log(response.output_text);

Embeddings ​

typescript
const result = await client.embeddings.create({
  model: "embedding-model-id",
  input: ["First text segment", "Second text segment"],
});

console.log(result.data[0].embedding.length);

Using .env ​

bash
COREROUTER_API_KEY=sk-xxxxxxxxxxxxxxxx
COREROUTER_BASE_URL=https://api.corerouter.cloud/v1
COREROUTER_MODEL=claude-sonnet-4-5
typescript
import "dotenv/config";
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.COREROUTER_API_KEY,
  baseURL: process.env.COREROUTER_BASE_URL,
});

const completion = await client.chat.completions.create({
  model: process.env.COREROUTER_MODEL ?? "claude-sonnet-4-5",
  messages: [{ role: "user", content: "Hello" }],
});

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

Add .env to .gitignore; don't commit API Keys to repositories.

Common Issues ​

  • COREROUTER_API_KEY is empty: Confirm the program starts from the same terminal, or the deployment platform has environment variables configured.
  • Returns model not found: Confirm model is the Model ID from the console.
  • Regular conversation works but tool calling fails: Switch to a model that supports Tool Calling.

Released under the MIT License.