Skip to content
Learni
View all tutorials
Intelligence Artificielle

How to Use OpenRouter with Node.js in 2026

Lire en français

Introduction

OpenRouter is a game-changing platform that acts as a smart router for large language models (LLMs). Instead of being locked into one provider like OpenAI or Anthropic, OpenRouter unifies access to over 100 models through an OpenAI-compatible API. Why use it in 2026? Costs are optimized (pay-as-you-go with free starter credits), latency is minimized via automatic routing, and reliability is boosted with multi-provider fallbacks.

This beginner tutorial walks you through integrating OpenRouter into a Node.js app step by step. We'll start from an empty project and build a full chat client with streaming and model selection. Every step is actionable: copy-paste the code and run it. By the end, you'll master the basics to scale to production-ready AI apps. Imagine a virtual assistant that seamlessly switches between GPT-4o and Llama-3—that's OpenRouter!

Prerequisites

  • Node.js 20+ installed
  • Free account on OpenRouter.ai with an API key (create one in 1 minute)
  • Code editor (VS Code recommended)
  • Basic knowledge of JavaScript async/await

Initialize the project

terminal
mkdir openrouter-app
cd openrouter-app
npm init -y

This command creates a project folder and initializes package.json. It sets up the foundation for dependencies without unnecessary manual config. Pro tip: Always run in an empty folder to avoid conflicts.

Get your OpenRouter API key

Head to openrouter.ai/keys, log in, and generate an API key (format sk-or-v1-...). Copy it securely—it'll be used via environment variables for safety. OpenRouter provides $10 in free credits to test without any cost.

Install dependencies

terminal
npm install openai dotenv

openai is the official SDK that's fully compatible with OpenRouter (no custom libs needed). dotenv loads environment variables. Install for production simplicity; in 2026, these packages are stable and optimized for ESM/CJS.

Set up environment variables

.env
OPENROUTER_API_KEY=sk-or-v1-your-real-key-here

# Optionnel : headers pour leaderboard OpenRouter
HTTP_REFERER=https://your-app.com
RERANK_HEADER=your-app

Replace with your real key. Optional headers boost your visibility on the OpenRouter leaderboard (for bonus credits). Never commit .env to Git: add it to .gitignore to prevent secret leaks.

Create the OpenAI client

We configure the OpenAI SDK by pointing baseURL to OpenRouter. This makes the API 100% compatible: same syntax as OpenAI, but with multi-model access.

Simple chat completion

chat-simple.js
require('dotenv').config();
const OpenAI = require('openai');

const openai = new OpenAI({
  apiKey: process.env.OPENROUTER_API_KEY,
  baseURL: 'https://openrouter.ai/api/v1',
});

async function main() {
  try {
    const completion = await openai.chat.completions.create({
      model: 'openai/gpt-4o-mini',
      messages: [
        { role: 'user', content: 'Explique-moi OpenRouter en 2 phrases.' }
      ],
    });
    console.log(completion.choices[0].message.content);
  } catch (error) {
    console.error('Erreur:', error.message);
  }
}

main();

This complete script creates a chat with GPT-4o-mini via OpenRouter. Run with node chat-simple.js. The try/catch handles errors (e.g., invalid key). Pro tip: Check your OpenRouter quotas before heavy usage.

Enable streaming

For a smooth ChatGPT-like UX, enable streaming: responses arrive token by token. OpenRouter supports it natively.

Streaming chat completion

chat-stream.js
require('dotenv').config();
const OpenAI = require('openai');

const openai = new OpenAI({
  apiKey: process.env.OPENROUTER_API_KEY,
  baseURL: 'https://openrouter.ai/api/v1',
});

async function main() {
  try {
    const stream = await openai.chat.completions.create({
      model: 'anthropic/claude-3.5-sonnet',
      messages: [
        { role: 'user', content: 'Rédige un poème sur l\'IA en français.' }
      ],
      stream: true,
    });

    let fullResponse = '';
    for await (const chunk of stream) {
      const content = chunk.choices[0]?.delta?.content || '';
      process.stdout.write(content);
      fullResponse += content;
    }
    console.log('\n\nRéponse complète:', fullResponse);
  } catch (error) {
    console.error('Erreur:', error.message);
  }
}

main();

Switches to Claude 3.5 Sonnet for variety. The for await loop accumulates and displays in real-time. Caution: Always close streams in production to prevent memory leaks.

Switch models and providers

multi-modeles.js
require('dotenv').config();
const OpenAI = require('openai');

const openai = new OpenAI({
  apiKey: process.env.OPENROUTER_API_KEY,
  baseURL: 'https://openrouter.ai/api/v1',
});

const modeles = [
  { nom: 'OpenAI GPT-4o mini (rapide)', model: 'openai/gpt-4o-mini' },
  { nom: 'Meta Llama 3.1 405B (puissant)', model: 'meta-llama/llama-3.1-405b-instruct:free' },
  { nom: 'Google Gemini 1.5 Pro', model: 'google/gemini-2.0-pro-exp-0801' }
];

async function testerModele(modeleInfo) {
  console.log(`\n--- Test ${modeleInfo.nom} ---`);
  const completion = await openai.chat.completions.create({
    model: modeleInfo.model,
    messages: [{ role: 'user', content: 'Quel est ton point fort ?' }],
  });
  console.log(completion.choices[0].message.content);
}

async function main() {
  for (const modele of modeles) {
    await testerModele(modele);
  }
}

main().catch(console.error);

This script loops through 3 popular models (one free!). Check openrouter.ai/models for the full list. Pro tip: Free models have limits; monitor costs via the dashboard.

Multi-turn conversation example

conversation.js
require('dotenv').config();
const readline = require('readline');
const OpenAI = require('openai');

const openai = new OpenAI({
  apiKey: process.env.OPENROUTER_API_KEY,
  baseURL: 'https://openrouter.ai/api/v1',
});

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let messages = [{ role: 'system', content: 'Tu es un assistant utile en français.' }];

async function askQuestion() {
  rl.question('Vous: ', async (input) => {
    if (input.toLowerCase() === 'quit') {
      rl.close();
      return;
    }
    messages.push({ role: 'user', content: input });

    const stream = await openai.chat.completions.create({
      model: 'openai/gpt-4o-mini',
      messages,
      stream: true,
    });

    process.stdout.write('\nAssistant: ');
    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0]?.delta?.content || '');
    }
    console.log('');
    messages.push({ role: 'assistant', content: '' }); // Sera rempli par stream
    askQuestion();
  });
}

askQuestion();

Interactive chat with conversation history (system prompt). Uses Node's built-in readline. In production, persist messages in a DB for long contexts; limit to 10-20 turns to control costs.

Best practices

  • Always use environment variables for API keys: integrate dotenv everywhere.
  • Choose models by use case: 'gpt-4o-mini' for speed, 'llama-3.1-405b' for free precision.
  • Implement retry logic: OpenRouter has auto-fallbacks, but add exponential backoff for 99.9% uptime.
  • Monitor costs: Use the OpenRouter dashboard + usage logs.
  • Secure endpoints: For web APIs, validate inputs and rate-limit.

Common errors to avoid

  • Invalid API key: Check format sk-or-v1-... and ensure credits >0.
  • Missing baseURL: Without 'https://openrouter.ai/api/v1', it calls OpenAI directly (401 error).
  • Unhandled streams: Always close iterators to avoid memory leaks.
  • Unavailable models: Check model docs; free tiers can get saturated.

Next steps