npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

openrouter-client

v1.8.0

Published

An API wrapper for OpenRouter

Downloads

289

Readme

OpenRouter-client

Info

This is a simple API wrapper for OpenRouter.

Documentation

Example use case

import { OpenRouter } from "openrouter-client";
const OpenRouterClient = new OpenRouter("API KEY HERE");

let nl = await OpenRouterClient.chat([{
    role: "system",
    content:
        "ONLY reply with 'minecraft'. Do not reply with anything else under any circumstances",
}, {
    role: "user",
    content: [
        { type: "text", text: "Hello World" },
        // Images can also be added
        // {
        //     type: "image_url",
        //     image_url: {
        //         url: `data:image/png;base64,${base64Image}`
        //     }
        // }
    ],
}], {
    // General configuration goes here

    model: "meta-llama/llama3.2-3b-instruct", // Required. This is the name of the model on OpenRouter
    temperature: 0.7, // Optional
    min_p: 0.05, // Optional

    // OpenRouter only configuration option. Read OpenRouter documentation for more information
    provider: {
        order: [
            "DeepInfra",
            "Hyperbolic",
        ],
    },
});

A global configuration can also be set. Any options set here will be applied to EVERY chat.

import { OpenRouter } from "openrouter-client";
const OpenRouterClient = new OpenRouter("API KEY HERE", {
    //global configuration can be set here

    model: "meta-llama/llama3.2-3b-instruct",
    min_p: 0.05, // Optional

    // OpenRouter only configuration option. Read OpenRouter documentation for more information
    provider: {
        order: [
            "DeepInfra",
            "Hyperbolic",
        ],
    },
});

Retrive information of a chat with getGenerationStats()

import { OpenRouter } from "openrouter-client";
const OpenRouterClient = new OpenRouter("API KEY HERE");
// You get the ID from what `OpenRouterClient.chat()` returns
let nl = await OpenRouterClient.chat([{
    role: "user",
    content: [
        { type: "text", text: "Hello World" },
    ],
}], {
    model: "meta-llama/llama3.2-3b-instruct",
});

if (nl.success == false) {
    console.log(nl.error);
    process.exit(0);
}

const gen = OpenRouterClient.getGenerationStats(nl.data.id);
console.log(gen);

Streaming

The streaming version also supports a global configuration.

import { OpenRouterStream } from "openrouter-client";
const OpenRouterClient = new OpenRouterStream("API KEY HERE", {
    model: "meta-llama/llama3.2-3b-instruct",
    min_p: 0.05,
});

There are 2 ways you can stream. The first way is by chunks. Every new "data" message will only contain the token that was generated.

import { OpenRouterStream } from "openrouter-client";
const OpenRouterClient = new OpenRouterStream("API KEY HERE");

OpenRouterClient.on("error", (e) => {
    console.log(`Error: ${e}`);
});

OpenRouterClient.on("end", (data) => {
    console.log("Stream end");
});

OpenRouterClient.on("data", (data) => {
    // Type of the `data` variable

    // {
    //   id: string
    //   provider: string,
    //   model: string,
    //   object: 'chat.completion.chunk',
    //   created: number,
    //   choices: [
    //     { index: 0, delta: { role: "assistant", content: string }, finish_reason: null | string, logprobs: null | number[] }
    //   ],
    //   usage?: { prompt_tokens: number, completion_tokens: number, total_tokens: number }
    // }
    console.log(data);
});

let nl = await OpenRouterClient.chatStreamChunk([{
    role: "user",
    content: [
        { type: "text", text: "Hello World" },
    ],
}], {
    model: "meta-llama/llama3.2-3b-instruct",
});

The second way is the "whole" way. This function passes back the entire object. So the first message might be { content: "hello" } and the second message might be { content: "hello world" }. This differs from chatStreamChunk, which only sends the new token that was generated instead of the whole object.

import { OpenRouterStream } from "openrouter-client";
const OpenRouterClient = new OpenRouterStream("API KEY HERE");

OpenRouterClient.on("error", (e) => {
    console.log(`Error: ${e}`);
});

OpenRouterClient.on("end", (data) => {
    console.log("Stream end");
});

OpenRouterClient.on("data", (data) => {
    // Type of the `data` variable

    // {
    //   id: string
    //   provider: string,
    //   model: string,
    //   object: 'chat.completion.chunk',
    //   created: number,
    //   finish_reason: string,
    //   choices: { role: "system" | "assistant" | "user", content: string, reasoning: string | null }[],
    //   usage?: { prompt_tokens: number, completion_tokens: number, total_tokens: number }
    // }
    console.log(data);
});

let nl = await OpenRouterClient.chatStreamWhole([{
    role: "user",
    content: [
        { type: "text", text: "Hello World" },
    ],
}], {
    model: "meta-llama/llama3.2-3b-instruct",
});

How to use reasoning

Non-streaming example

import { OpenRouter } from "openrouter-client";
const OpenRouterClient = new OpenRouter("API key here");

let nl = await OpenRouterClient.chat([{
    role: "user",
    content: [
        { type: "text", text: "Hello World" },
    ],
}], {
    model: "deepseek/deepseek-r1-0528-qwen3-8b",

    // Reasoning options can be added here
    reasoning: {
        enabled: true,
        // exclude?: boolean,
        // effort?: "high" | "medium" | "low"
        // max_tokens?: number
    },
});

console.dir(nl);

For streaming, just import and use OpenRouterStream instead of OpenRouter. All the other code stays the same