Documentation

Installation

Install the MarginDash SDK for your language.

npm
npm install margindash

Usage

Track events using the MarginDash TypeScript SDK.

TypeScript
MarginDash SDK Your code
import { MarginDash } from "margindash";
import OpenAI from "openai";

const md = new MarginDash({ apiKey: "YOUR_API_KEY_HERE" }); // Get from Settings page
const openai = new OpenAI();

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Hello!" }],
});

// No prompts or responses ever leave your servers
md.addUsage({
  vendor: "openai",
  model: response.model, // "gpt-4o"
  inputTokens: response.usage!.prompt_tokens, // 1200
  outputTokens: response.usage!.completion_tokens, // 340
});

md.track({
  customerId: user.id, // 8291
  eventType: "summarize",
  revenueAmountInCents: 500,
});

// Events are flushed automatically in the background.
// Before your process exits:
await md.shutdown();

Privacy first: Only the model name and token counts are sent to MarginDash — no request or response content ever leaves your infrastructure. Pass the vendor name and usage via addUsage(), and MarginDash calculates cost from your configured vendor rates. For agent sessions with multiple AI calls, call addUsage() once per call, then track() once to attach them all.

Field Reference

All fields accepted by track() and addUsage().

track() / Event

Field Type Required Description
customerId string yes Your customer identifier. Auto-creates the customer if it doesn’t exist.
eventType string no Category for this event, max 255 characters (default: ai_request)
revenueAmountInCents number no Revenue for this event in cents, 0–10,000,000, e.g. 1500 = $15.00 (default: 0)
occurredAt string no ISO 8601 timestamp, e.g. 2025-06-15T14:30:00Z. Must be within the last 90 days and no more than 1 hour in the future. (default: current time)

addUsage() / Usage

Field Type Required Description
vendor string yes AI vendor name, e.g. openai, anthropic, google. See all vendors and model slugs below.
model string yes AI model slug, e.g. gpt-4o, claude-4-opus. See all vendors and model slugs below.
inputTokens number yes Input/prompt token count, 0–100,000,000. At least one of inputTokens or outputTokens must be > 0.
outputTokens number yes Output/completion token count, 0–100,000,000

Supported Models

Click a vendor to see valid slugs for the vendor and model fields. You can also fetch this list programmatically via GET /api/v1/models.