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

@revenium/openai

v1.0.16

Published

Transparent TypeScript middleware for automatic Revenium usage tracking with OpenAI

Downloads

118

Readme

Revenium OpenAI Middleware for Node.js

npm version Node.js Documentation Website License: MIT

Transparent TypeScript middleware for automatic Revenium usage tracking with OpenAI

A professional-grade Node.js middleware that seamlessly integrates with OpenAI and Azure OpenAI to provide automatic usage tracking, billing analytics, and comprehensive metadata collection. Features native TypeScript support with zero type casting required and supports both Chat Completions API, Embeddings API, and Responses API.

Go-aligned API for consistent cross-language development!

Features

  • Go-Aligned API - Same Initialize()/GetClient() pattern as Go implementation
  • Seamless Integration - Native TypeScript support, no type casting required
  • Optional Metadata - Track users, organizations, and business context (all fields optional)
  • Multiple API Support - Chat Completions, Embeddings, and Responses API
  • Azure OpenAI Support - Full Azure OpenAI integration with automatic detection
  • Type Safety - Complete TypeScript support with IntelliSense
  • Streaming Support - Handles regular and streaming requests seamlessly
  • Fire-and-Forget - Never blocks your application flow
  • Automatic .env Loading - Loads environment variables automatically

Getting Started

1. Create Project Directory

# Create project directory and navigate to it
mkdir my-openai-project
cd my-openai-project

# Initialize npm project
npm init -y

# Install packages
npm install @revenium/openai openai dotenv tsx
npm install --save-dev typescript @types/node

2. Configure Environment Variables

Create a .env file in your project root. See .env.example for all available configuration options.

Minimum required configuration:

REVENIUM_METERING_API_KEY=hak_your_revenium_api_key_here
REVENIUM_METERING_BASE_URL=https://api.revenium.ai
OPENAI_API_KEY=sk_your_openai_api_key_here

NOTE: Replace the placeholder values with your actual API keys.

3. Run Your First Example

For complete examples and usage patterns, see examples/README.md.


Requirements

  • Node.js 16+
  • OpenAI package v5.0.0 or later
  • TypeScript 5.0+ (for TypeScript projects)

What Gets Tracked

The middleware automatically captures comprehensive usage data:

Usage Metrics

  • Token Counts - Input tokens, output tokens, total tokens
  • Model Information - Model name, provider (OpenAI/Azure), API version
  • Request Timing - Request duration, response time
  • Cost Calculation - Estimated costs based on current pricing

Business Context (Optional)

  • User Tracking - Subscriber ID, email, credentials
  • Organization Data - Organization ID, subscription ID, product ID
  • Task Classification - Task type, agent identifier, trace ID
  • Quality Metrics - Response quality scores, task identifiers

Technical Details

  • API Endpoints - Chat completions, embeddings, responses API
  • Request Types - Streaming vs non-streaming
  • Error Tracking - Failed requests, error types, retry attempts
  • Environment Info - Development vs production usage

API Overview

The middleware provides a Go-aligned API with the following main functions:

  • Initialize(config?) - Initialize the middleware (from environment or explicit config)
  • GetClient() - Get the global Revenium client instance
  • Configure(config) - Alias for Initialize() for programmatic configuration
  • IsInitialized() - Check if the middleware is initialized
  • Reset() - Reset the global client (useful for testing)

For complete API documentation and usage examples, see examples/README.md.

Trace Visualization Fields

The middleware automatically captures trace visualization fields for distributed tracing and analytics:

| Field | Type | Description | Environment Variable | | --------------------- | ------ | ------------------------------------------------------------------------------- | ---------------------------------- | | environment | string | Deployment environment (production, staging, development) | REVENIUM_ENVIRONMENT, NODE_ENV | | operationType | string | Operation classification (CHAT, EMBED, IMAGE, AUDIO) - automatically detected | N/A (auto-detected) | | operationSubtype | string | Additional detail (function_call, etc.) - automatically detected | N/A (auto-detected) | | retryNumber | number | Retry attempt number (0 for first attempt, 1+ for retries) | REVENIUM_RETRY_NUMBER | | parentTransactionId | string | Parent transaction reference for distributed tracing | REVENIUM_PARENT_TRANSACTION_ID | | transactionName | string | Human-friendly operation label | REVENIUM_TRANSACTION_NAME | | region | string | Cloud region (us-east-1, etc.) - auto-detected from AWS/Azure/GCP | AWS_REGION, REVENIUM_REGION | | credentialAlias | string | Human-readable credential name | REVENIUM_CREDENTIAL_ALIAS | | traceType | string | Categorical identifier (alphanumeric, hyphens, underscores only, max 128 chars) | REVENIUM_TRACE_TYPE | | traceName | string | Human-readable label for trace instances (max 256 chars) | REVENIUM_TRACE_NAME |

All trace visualization fields are optional. The middleware will automatically detect and populate these fields when possible.

Example Configuration

REVENIUM_ENVIRONMENT=production
REVENIUM_REGION=us-east-1
REVENIUM_CREDENTIAL_ALIAS=OpenAI Production Key
REVENIUM_TRACE_TYPE=customer_support
REVENIUM_TRACE_NAME=Support Ticket #12345
REVENIUM_PARENT_TRANSACTION_ID=parent-txn-123
REVENIUM_TRANSACTION_NAME=Answer Customer Question
REVENIUM_RETRY_NUMBER=0

Metadata Fields

The middleware supports the following optional metadata fields for tracking:

| Field | Type | Description | | ----------------------- | ------ | ----------------------------------------------------------- | | traceId | string | Unique identifier for session or conversation tracking | | taskType | string | Type of AI task being performed (e.g., "chat", "embedding") | | agent | string | AI agent or bot identifier | | organizationId | string | Organization or company identifier | | productId | string | Your product or feature identifier | | subscriptionId | string | Subscription plan identifier | | responseQualityScore | number | Custom quality rating (0.0-1.0) | | subscriber.id | string | Unique user identifier | | subscriber.email | string | User email address | | subscriber.credential | object | Authentication credential (name and value fields) |

All metadata fields are optional. For complete metadata documentation and usage examples, see:

Configuration Options

Environment Variables

For a complete list of all available environment variables with examples, see .env.example.

Examples

The package includes comprehensive examples in the examples/ directory.

Getting Started

npm run example:getting-started

OpenAI Examples

| Example | Command | Description | | ------------------------------- | ----------------------------------- | --------------------------------- | | openai/basic.ts | npm run example:openai-basic | Chat completions and embeddings | | openai/metadata.ts | npm run example:openai-metadata | All metadata fields demonstration | | openai/streaming.ts | npm run example:openai-stream | Streaming chat completions | | openai/responses-basic.ts | npm run example:openai-res-basic | Responses API usage | | openai/responses-embed.ts | npm run example:openai-res-embed | Embeddings with Responses API | | openai/responses-streaming.ts | npm run example:openai-res-stream | Streaming Responses API |

Azure OpenAI Examples

| Example | Command | Description | | --------------------------- | ---------------------------------- | ----------------------------- | | azure/basic.ts | npm run example:azure-basic | Azure chat completions | | azure/stream.ts | npm run example:azure-stream | Azure streaming | | azure/responses-basic.ts | npm run example:azure-res-basic | Azure Responses API | | azure/responses-stream.ts | npm run example:azure-res-stream | Azure Responses API streaming |

For complete example documentation, setup instructions, and usage patterns, see examples/README.md.

How It Works

  1. Initialize: Call Initialize() to set up the middleware with your configuration
  2. Get Client: Call GetClient() to get a wrapped OpenAI client instance
  3. Make Requests: Use the client normally - all requests are automatically tracked
  4. Async Tracking: Usage data is sent to Revenium in the background (fire-and-forget)
  5. Transparent Response: Original OpenAI responses are returned unchanged

The middleware never blocks your application - if Revenium tracking fails, your OpenAI requests continue normally.

Supported APIs:

  • Chat Completions API (client.chat().completions().create())
  • Embeddings API (client.embeddings().create())
  • Responses API (client.responses().create() and client.responses().createStreaming())

Troubleshooting

Common Issues

No tracking data appears:

  1. Verify environment variables are set correctly in .env
  2. Enable debug logging by setting REVENIUM_DEBUG=true in .env
  3. Check console for [Revenium] log messages
  4. Verify your REVENIUM_METERING_API_KEY is valid

Client not initialized error:

  • Make sure you call Initialize() before GetClient()
  • Check that your .env file is in the project root
  • Verify REVENIUM_METERING_API_KEY is set

Azure OpenAI not working:

  • Verify all Azure environment variables are set (see .env.example)
  • Check that AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_API_KEY are correct
  • Ensure you're using a valid deployment name in the model parameter

Debug Mode

Enable detailed logging by adding to your .env:

REVENIUM_DEBUG=true

Getting Help

If issues persist:

  1. Enable debug logging (REVENIUM_DEBUG=true)
  2. Check the examples/ directory for working examples
  3. Review examples/README.md for detailed setup instructions
  4. Contact [email protected] with debug logs

Supported Models

This middleware works with any OpenAI model. For the complete model list, see the OpenAI Models Documentation.

API Support Matrix

The following table shows what has been tested and verified with working examples:

| Feature | Chat Completions | Embeddings | Responses API | | --------------------- | ---------------- | ---------- | ------------- | | OpenAI Basic | Yes | Yes | Yes | | OpenAI Streaming | Yes | No | Yes | | Azure Basic | Yes | No | Yes | | Azure Streaming | Yes | No | Yes | | Metadata Tracking | Yes | Yes | Yes | | Token Counting | Yes | Yes | Yes |

Note: "Yes" = Tested with working examples in examples/ directory

Documentation

For detailed documentation, visit docs.revenium.io

Contributing

See CONTRIBUTING.md

Code of Conduct

See CODE_OF_CONDUCT.md

Security

See SECURITY.md

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues, feature requests, or contributions:


Built by Revenium