SDKs
Node.js SDK
Use NemoRouter with the OpenAI Node.js SDK
Node.js SDK
NemoRouter is fully compatible with the OpenAI Node.js SDK. Use it with TypeScript or JavaScript — just install openai and point it at NemoRouter.
Installation
npm install openaiOr with yarn/pnpm:
yarn add openai
# or
pnpm add openaiSetup
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.NEMOROUTER_API_KEY,
baseURL: "https://api.nemorouter.ai/v1",
});Set your API key as an environment variable:
export NEMOROUTER_API_KEY="sk-nemo-your-key-here"Or in a .env file (with a package like dotenv):
NEMOROUTER_API_KEY=sk-nemo-your-key-hereChat Completions
Basic Request
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What are the benefits of using an LLM gateway?" },
],
temperature: 0.7,
max_tokens: 512,
});
console.log(response.choices[0].message.content);Multi-Turn Conversation
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.NEMOROUTER_API_KEY,
baseURL: "https://api.nemorouter.ai/v1",
});
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: "system", content: "You are a knowledgeable AI tutor." },
{ role: "user", content: "Explain machine learning in simple terms." },
];
// First turn
const first = await client.chat.completions.create({
model: "gpt-4o",
messages,
});
messages.push({ role: "assistant", content: first.choices[0].message.content! });
// Second turn
messages.push({ role: "user", content: "Now explain neural networks." });
const second = await client.chat.completions.create({
model: "gpt-4o",
messages,
});
console.log(second.choices[0].message.content);Switch Models Instantly
Change providers by changing the model name:
// OpenAI
await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
});
// Anthropic
await client.chat.completions.create({
model: "claude-4-sonnet",
messages: [{ role: "user", content: "Hello!" }],
});
// Google
await client.chat.completions.create({
model: "gemini-2.5-pro",
messages: [{ role: "user", content: "Hello!" }],
});Streaming
Stream responses for real-time output:
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "user", content: "Write a short story about a robot learning to cook." },
],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
}
console.log(); // Final newlineCollecting Streamed Content
let fullResponse = "";
const stream = await client.chat.completions.create({
model: "claude-4-sonnet",
messages: [{ role: "user", content: "List 5 TypeScript best practices." }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
fullResponse += content;
process.stdout.write(content);
}
}
console.log(`\n\nTotal length: ${fullResponse.length} characters`);Embeddings
const response = await client.embeddings.create({
model: "text-embedding-3-small",
input: "NemoRouter is an enterprise LLM gateway.",
});
const embedding = response.data[0].embedding;
console.log(`Dimensions: ${embedding.length}`);Batch Embeddings
const texts = [
"First document about AI gateways.",
"Second document about API management.",
"Third document about cost optimization.",
];
const response = await client.embeddings.create({
model: "text-embedding-3-small",
input: texts,
});
response.data.forEach((item, i) => {
console.log(`Text ${i}: ${item.embedding.length} dimensions`);
});Function Calling
const tools: OpenAI.ChatCompletionTool[] = [
{
type: "function",
function: {
name: "get_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and state, e.g. San Francisco, CA",
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
},
},
required: ["location"],
},
},
},
];
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "What's the weather in Paris?" }],
tools,
tool_choice: "auto",
});
const message = response.choices[0].message;
if (message.tool_calls) {
for (const toolCall of message.tool_calls) {
console.log(`Function: ${toolCall.function.name}`);
console.log(`Arguments: ${toolCall.function.arguments}`);
}
}JSON Mode
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content:
"You output JSON. Return a list of 3 programming languages with name and year_created fields.",
},
{ role: "user", content: "List programming languages." },
],
response_format: { type: "json_object" },
});
const data = JSON.parse(response.choices[0].message.content!);
console.log(JSON.stringify(data, null, 2));Error Handling
import OpenAI from "openai";
try {
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
});
} catch (error) {
if (error instanceof OpenAI.AuthenticationError) {
console.error("Invalid API key. Check your NEMOROUTER_API_KEY.");
} else if (error instanceof OpenAI.RateLimitError) {
console.error("Rate limit exceeded. Implement backoff or upgrade your plan.");
} else if (error instanceof OpenAI.APIConnectionError) {
console.error("Could not connect to NemoRouter. Check your network.");
} else if (error instanceof OpenAI.APIError) {
console.error(`API error: ${error.status} — ${error.message}`);
} else {
throw error;
}
}Automatic Retries
The OpenAI SDK has built-in retry logic for rate limits and transient errors:
const client = new OpenAI({
apiKey: process.env.NEMOROUTER_API_KEY,
baseURL: "https://api.nemorouter.ai/v1",
maxRetries: 3, // Retries on 429 and 5xx errors
timeout: 30000, // 30 second timeout
});Listing Models
const models = await client.models.list();
for (const model of models.data) {
console.log(`${model.id} (owned by ${model.owned_by})`);
}Express.js Integration
Use NemoRouter in an Express API:
import express from "express";
import OpenAI from "openai";
const app = express();
app.use(express.json());
const client = new OpenAI({
apiKey: process.env.NEMOROUTER_API_KEY,
baseURL: "https://api.nemorouter.ai/v1",
});
app.post("/api/chat", async (req, res) => {
try {
const { message } = req.body;
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: message },
],
max_tokens: 512,
});
res.json({ reply: response.choices[0].message.content });
} catch (error) {
res.status(500).json({ error: "Failed to generate response" });
}
});
app.listen(3000, () => console.log("Server running on port 3000"));Complete Example
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.NEMOROUTER_API_KEY,
baseURL: "https://api.nemorouter.ai/v1",
});
async function main() {
// List available models
console.log("Available models:");
const models = await client.models.list();
models.data.slice(0, 5).forEach((m) => console.log(` - ${m.id}`));
// Chat completion
console.log("\nChat completion:");
const chat = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a concise assistant." },
{ role: "user", content: "What is an LLM gateway in one sentence?" },
],
max_tokens: 100,
});
console.log(` ${chat.choices[0].message.content}`);
console.log(` Tokens used: ${chat.usage?.total_tokens}`);
// Embedding
console.log("\nEmbedding:");
const emb = await client.embeddings.create({
model: "text-embedding-3-small",
input: "NemoRouter simplifies LLM access.",
});
console.log(` Dimensions: ${emb.data[0].embedding.length}`);
console.log("\nDone!");
}
main();Next Steps
- Python SDK — Python integration guide
- cURL Examples — Command-line examples
- Chat Completions API — Full API reference
- Authentication — API key best practices