iToverDose/Software· 8 JUNE 2026 · 16:02

Simplify LLM Integration: Build with OpenRouter in Node.js

Discover how OpenRouter’s unified API streamlines access to multiple LLMs through a single SDK or existing OpenAI-compatible tools in Node.js, cutting setup time and vendor lock-in.

DEV Community4 min read0 Comments

Integrating large language models (LLMs) into applications often means juggling multiple APIs, keys, and billing systems. OpenRouter changes that by offering a single gateway to dozens of LLMs from providers like OpenAI, Anthropic, Google, and Meta. With one API key and a configurable model slug, developers can switch between models without rewriting core logic.

Why OpenRouter Stands Out for LLM Integration

OpenRouter acts as an abstraction layer above diverse LLM providers, consolidating their APIs into a unified interface. This eliminates the need to manage separate accounts, SDKs, or rate limits for each service. Instead, developers configure a single endpoint and toggle models by adjusting a provider/model string—for example, openai/gpt-5.5 or anthropic/claude-opus-4.8.

The platform’s Chat Completions-compatible HTTP API ensures compatibility with existing OpenAI SDKs and tools, reducing the learning curve. Whether you're prototyping a chatbot or scaling an AI-driven feature, OpenRouter simplifies deployment while preserving flexibility.

Three Proven Ways to Integrate OpenRouter in Node.js

Developers have multiple paths to integrate OpenRouter, each suited to different workflows. The three most common approaches are:

  • Using OpenRouter’s official TypeScript SDK (@openrouter/sdk)
  • Leveraging the OpenAI SDK with a custom baseURL
  • Integrating with the Vercel AI SDK via the @openrouter/ai-sdk-provider package

Each method requires Node.js 26+ and an OpenRouter account with an API key. Billing must be enabled if using paid models, and environment variables should store sensitive credentials like OPENROUTER_API_KEY.

Method 1: OpenRouter’s Official SDK

The @openrouter/sdk package provides type-safe, auto-generated bindings from OpenRouter’s OpenAPI specification. It’s ideal for new projects or teams prioritizing maintainability and IDE support.

Setting Up the Client

import { OpenRouter } from '@openrouter/sdk';

const client = new OpenRouter({
  apiKey: process.env.OPENROUTER_API_KEY,
  httpReferer: process.env.OPENROUTER_SITE_URL,
  appTitle: process.env.OPENROUTER_SITE_TITLE,
});

The httpReferer and appTitle headers help track usage and attribution on OpenRouter’s platform.

Making Your First Request

const response = await client.chat.send({
  chatRequest: {
    model: process.env.OPENROUTER_MODEL ?? 'openai/gpt-5.5',
    messages: [
      {
        role: 'user',
        content: 'Write a one-sentence bedtime story about a unicorn.',
      },
    ],
  },
});

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

This returns a concise completion from the specified model.

Adding System Prompts and Streaming

System prompts guide the model’s tone and format. To enforce brevity:

const response = await client.chat.send({
  chatRequest: {
    model: process.env.OPENROUTER_MODEL ?? 'openai/gpt-5.5',
    messages: [
      {
        role: 'system',
        content: 'Reply in one short sentence. Use plain language.',
      },
      {
        role: 'user',
        content: 'Explain what an LLM is.',
      },
    ],
  },
});

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

For real-time applications, enable streaming to process responses incrementally:

const stream = await client.chat.send({
  chatRequest: {
    model: process.env.OPENROUTER_MODEL ?? 'openai/gpt-5.5',
    messages: [
      {
        role: 'user',
        content: 'List three colors.',
      },
    ],
    stream: true,
  },
});

process.stdout.write('[stream] ');
for await (const chunk of stream) {
  const delta = chunk.choices[0]?.delta?.content;
  if (delta) {
    process.stdout.write(delta);
  }
}
process.stdout.write('\n');

This streams text as it’s generated, reducing latency for user-facing features.

Method 2: OpenAI SDK with OpenRouter

Teams already using the OpenAI SDK can redirect it to OpenRouter with minimal changes. Set the baseURL to OpenRouter’s endpoint and pass the same request shape as the Chat Completions API.

Configuring the Client

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.OPENROUTER_API_KEY,
  baseURL: '
  defaultHeaders: {
    'HTTP-Referer': process.env.OPENROUTER_SITE_URL,
    'X-OpenRouter-Title': process.env.OPENROUTER_SITE_TITLE,
  },
});

This approach reuses existing code while switching to OpenRouter’s backend.

Sending a Completion Request

const completion = await client.chat.completions.create({
  model: process.env.OPENROUTER_MODEL ?? 'openai/gpt-5.5',
  messages: [
    {
      role: 'user',
      content: 'Write a one-sentence bedtime story about a unicorn.',
    },
  ],
});

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

The response structure mirrors OpenAI’s API, ensuring compatibility with existing integrations.

Method 3: Vercel AI SDK Integration

For projects built with the Vercel AI SDK, the @openrouter/ai-sdk-provider package bridges OpenRouter’s models with its generateText, streamText, and other helpers. This is useful for frameworks like Next.js that rely on the AI SDK.

Installing Dependencies

npm install ai @openrouter/ai-sdk-provider

Then configure the provider in your application.

Choosing the Right Integration Path

The best method depends on your project’s stack and goals:

  • Startups or new projects may prefer OpenRouter’s SDK for its type safety and first-party support.
  • Legacy systems using OpenAI’s SDK can migrate seamlessly by updating the baseURL.
  • Next.js or AI SDK users benefit from the @openrouter/ai-sdk-provider for streamlined development.

Regardless of the path, OpenRouter’s model-agnostic design future-proofs your application against vendor lock-in.

Looking Ahead: The Future of Multi-Model LLMs

As LLMs evolve, the ability to switch models without refactoring code becomes a competitive advantage. OpenRouter’s unified approach lowers the barrier to experimentation, enabling developers to test new models quickly and scale based on performance.

The next wave of AI applications will demand flexibility—OpenRouter positions itself as the connective tissue between innovation and practical deployment.

AI summary

Learn three ways to integrate OpenRouter’s unified LLM API in Node.js using SDKs, OpenAI-compatible tools, or Vercel AI SDK for seamless model switching and cost efficiency.

Comments

00
LEAVE A COMMENT
ID #EDQ8FA

0 / 1200 CHARACTERS

Human check

5 + 7 = ?

Will appear after editor review

Moderation · Spam protection active

No approved comments yet. Be first.