LogDuck Documentation

JavaScript & TypeScript SDK

Markdown: this page · all of the docs · index

The official JavaScript and TypeScript client. Ships ESM and CommonJS builds with bundled type definitions, and runs on Node 18+, Deno, Bun, Cloudflare Workers and the browser — anywhere fetch exists.

Installation

terminal
npm install logduck

Minimal example

The fewest settings that work — an API key and a source.

TypeScriptlogduck.ts
import { LogDuckClient } from 'logduck';const logduck = new LogDuckClient({  apiKey: process.env.LOGDUCK_API_KEY!,  source: 'checkout-api',});await logduck.send({ type: 'order.placed' });

Complete example

Every option, with its default. Only the first two are required; the rest are shown set to the value you get by omitting them.

TypeScriptlogduck.ts
import { LogDuckClient } from 'logduck';const logduck = new LogDuckClient({  apiKey: process.env.LOGDUCK_API_KEY!,   // required - your key, starts with `ld_`  source: 'checkout-api',                 // required - which app sent this, max 200 chars  throwOnError: false,                    // optional, default false - true throws instead of returning null  retryEnabled: true,                     // optional, default true  - retry once on 5xx, network errors and 429  maxRetryDelayMs: 10_000,                // optional, default 10000 - never block your caller longer than this  timeoutMs: 30_000,                      // optional, default 30000 - per-request timeout  baseUrl: 'https://api.logduck.com',     // optional - override only for a self-hosted deployment  logger: console,                        // optional, default console - anything with a .warn() method  fetch: globalThis.fetch,                // optional - inject a fetch implementation, mainly for tests});const orderId = 'order_9127';             // your own identifiersconst sessionId = 'sess_8f21c';const response = await logduck.send({  type: 'order.placed',                   // required - max 100 chars, naming rule below  subject: orderId,                       // optional - what the event is about, max 500 chars  sessionId,                              // optional - groups related events, max 256 chars  message: 'Order placed on the Pro plan', // optional - the push notification body, max 500  time: new Date(),                       // optional - defaults to when the server received it  data: { total: 4999, currency: 'NOK' }, // optional - any JSON-serialisable object  emoji: '🛒',                             // optional - shown next to the event, max 10 chars});// null when the send failed and throwOnError is false (the default).console.log(response?.eventId);

Errors

By default a failed send is logged and returns null, so a logging call can never take down the code it was only meant to observe. Opt into exceptions when you want to handle them:

TypeScripthandler.ts
import { LogDuckClient, LogDuckError } from 'logduck';const logduck = new LogDuckClient({  apiKey: process.env.LOGDUCK_API_KEY!,  source: 'checkout-api',  throwOnError: true,});try {  await logduck.send({ type: 'order.placed' });} catch (error) {  if (error instanceof LogDuckError && error.retryAfterMs) {    // Rate limited for longer than the client will wait.    // Queue it rather than lose it.  }}

Full reference and changelog: github.com/Log-Duck/logduck-js