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
| Field | Description |
|---|---|
| Model | The AI model identifier (e.g., gpt-4) |
| Temperature | Sampling temperature parameter |
| Max Tokens | Maximum tokens in response |
| Messages | Array of conversation messages |
| Prompt Hash | SHA-256 hash of the prompt |
| Response Time | Time taken for API call |
| Token Usage | Input and output token counts |
| Cost | Estimated 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.