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 🙏

© 2025 – Pkg Stats / Ryan Hefner

npmai-client

v1.0.2

Published

A lightweight Node.js client for the OpenRouter API

Readme

NPM AI Client for OpenRouter

A lightweight, easy-to-use Node.js client for the OpenRouter API that simplifies sending chat completion requests to various AI models and handling structured JSON responses.

Installation

```bash npm install npmai-client ```

Basic Usage

```javascript const NpmAiClient = require('npmai-client'); const ai = new NpmAiClient(process.env.OPENROUTER_API_KEY);

// Define chat messages const messages = [ { role: "system", content: "You are a helpful assistant. Output your response in JSON format: { "answer": "string" }" }, { role: "user", content: "What is JavaScript?" } ];

// Send a request to a specific model ai.ask("google/gemini-pro", messages) .then(response => { // Access the parsed JSON response const answer = response.get("answer"); console.log("Answer:", answer); }) .catch(error => { console.error("Error:", error); }); ```

Features

  • Simple API for sending chat completion requests
  • Automatic JSON parsing of AI responses
  • Helper methods for accessing structured data
  • Support for all OpenRouter models
  • Fallback model routing
  • Comprehensive error handling
  • Structured Outputs - Enforce JSON Schema validation on model responses

Structured Outputs

For models that support it (like OpenAI's GPT-4o), you can enforce a specific JSON schema:

```javascript // Define your JSON schema const schema = { name: "weather_info", strict: true, schema: { type: "object", properties: { location: { type: "string", description: "City name" }, temperature: { type: "number", description: "Temperature in Celsius" } }, required: ["location", "temperature"] } };

// Use the askForJson method ai.askForJson("openai/gpt-4o", messages, schema) .then(response => { const data = response.getRawJson(); console.log(It's ${data.temperature}°C in ${data.location}); }); ```

API Reference

NpmAiClient

```javascript // Initialize with API key const ai = new NpmAiClient(apiKey, options);

// Send a request const response = await ai.ask(modelName, messages, options); // or const response = await ai.chat(modelName, messages, options); // or with structured output const response = await ai.askForJson(modelName, messages, jsonSchema, options); ```

ApiResponse

```javascript // Get a value by key const value = response.get("key");

// Get an array by key const list = response.getList("items");

// Get the full parsed JSON const json = response.getRawJson();

// Get the raw API response const rawResponse = response.getRawResponse();

// Check if JSON was parsed successfully const isParsed = response.isJsonParsed();

// Get the model used const model = response.getModel();

// Get token usage const usage = response.getUsage(); ```

For full documentation, see doc.md.

License

MIT