Back to Guides

OpenAI Node.js

Official OpenAI SDK for Node.js and TypeScript

TypeScripteasy

The OpenAI Node.js SDK is the official JavaScript client for OpenAI's API. Route your requests through Decyra's proxy to capture all API calls, monitor usage, and implement rate limiting.

Prerequisites

  • Decyra account with API key
  • Node.js 18+ installed
  • OpenAI package installed

Installation

npm install openai @decyra/sdk

Integration

Configure OpenAI client with Decyra's proxy:

import OpenAI from 'openai';
import { wrapOpenAI } from '@decyra/sdk';

const openai = new OpenAI({
  baseURL: 'https://proxy.decyra.com/v1',
  apiKey: process.env.DECYRA_API_KEY,
  defaultHeaders: {
    'X-Decyra-API-Key': process.env.DECYRA_API_KEY!,
  },
});

// Wrap for enhanced tracking
const decyraClient = wrapOpenAI(openai);

Use the client for chat completions:

const completion = await decyraClient.chat.completions.create({
  model: 'gpt-4',
  messages: [
    { role: 'user', content: 'Explain quantum computing' }
  ],
  temperature: 0.7,
});

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

What Gets Captured

FieldDescription
ModelThe AI model identifier (e.g., gpt-4)
TemperatureSampling temperature parameter
Max TokensMaximum tokens in response
MessagesArray of conversation messages
Prompt HashSHA-256 hash of the prompt
Response TimeTime taken for API call
Token UsageInput and output token counts
CostEstimated cost based on model pricing

Verify

Navigate to your Decyra dashboard and check the traces page. Each API call will appear with complete details including model, tokens, and cost.

Next Steps