Skip to content

Node.js SDK

bash
npm install @amlexiahq/node

Package: @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

OptionDefaultDescription
sdkKeyrequiredProject SDK key
ingestUrlhttps://ingest.amlexia.comIngest base URL
flushIntervalMs5000Background flush interval
maxBatchSize50Events per request
maxRetries5Upload retries
sampleRate1Fraction of events to keep (0–1)
diagnosticfalseLog flush/buffer diagnostics to stderr

Methods

  • track(event) — queue an event (respects sampling)
  • flush() — send buffer now
  • shutdown() — 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 health

Verifies SDK key and ingest reachability.

Framework exports

ImportUse
@amlexiahq/nodeClient, wrapFetch, cost helpers
@amlexiahq/node/expressExpress
@amlexiahq/node/fastifyFastify
@amlexiahq/node/honoHono
@amlexiahq/node/nextNext.js
@amlexiahq/node/tracingcreateTraceContext, childSpan
@amlexiahq/node/otelexportOtelSpans

Other runtimes

Bun, Deno & Edge — same package where fetch exists.

Event fields

Event fields · Manual instrumentation