Node.js SDK
bash
npm install @amlexiahq/nodePackage: @amlexiahq/node (v1.0.2+) · Feature parity
Quick start
typescript
import { AmlexiaClient } from '@amlexiahq/node';
const client = AmlexiaClient.fromEnv();
await client.track({
endpoint: 'GET /health',
method: 'GET',
statusCode: 200,
latencyMs: 12,
environment: process.env.AMLEXIA_ENVIRONMENT,
});
await client.shutdown();Client options
| Option | Default | Description |
|---|---|---|
sdkKey | required | Project SDK key |
ingestUrl | https://ingest.amlexia.com | Ingest base URL |
flushIntervalMs | 5000 | Background flush interval |
maxBatchSize | 50 | Events per request |
maxRetries | 5 | Upload retries |
sampleRate | 1 | Fraction of events to keep (0–1) |
diagnostic | false | Log flush/buffer diagnostics to stderr |
Methods
track(event)— queue an event (respects sampling)flush()— send buffer nowshutdown()— stop timer and flush (call on SIGTERM)getDiagnosticState()— buffer size, last flush, last error
Auto cost estimation
If you send modelName, provider, and token counts without costUsd, ingest enriches estimated cost from the shared pricing table. Explicit costUsd is stored as reported.
typescript
import { estimateEventCostUsd } from '@amlexiahq/node';
const costUsd = estimateEventCostUsd({
modelName: 'gpt-4o-mini',
provider: 'openai',
tokensInput: 100,
tokensOutput: 40,
});HTTP wrapper
Wrap global or custom fetch to auto-track outbound calls:
typescript
import { AmlexiaClient, wrapFetch } from '@amlexiahq/node';
const client = AmlexiaClient.fromEnv();
globalThis.fetch = wrapFetch(client, fetch);LLM helpers
typescript
import { trackOpenAICompletion, trackAnthropicMessage } from '@amlexiahq/node';Pass model, status, latency, and usage object from vendor responses.
CLI
bash
npx amlexia healthVerifies SDK key and ingest reachability.
Framework exports
| Import | Use |
|---|---|
@amlexiahq/node | Client, wrapFetch, cost helpers |
@amlexiahq/node/express | Express |
@amlexiahq/node/fastify | Fastify |
@amlexiahq/node/hono | Hono |
@amlexiahq/node/next | Next.js |
@amlexiahq/node/tracing | createTraceContext, childSpan |
@amlexiahq/node/otel | exportOtelSpans |
Other runtimes
Bun, Deno & Edge — same package where fetch exists.
