init()

init(config: InitConfig): void

Initialize the SDK. Must be called before any other method.

Parameters
NameTypeDescription
apiKeystringRequiredYour CostCanary API key. Must start with cck_live_ or cck_test_.
endpointstringOptionalOverride ingestion endpoint. Defaults to production URL. Also settable via COSTCANARY_ENDPOINT env var.
autoCapturebooleanOptional, default: falseAutomatically intercept all LLM API calls via fetch and http.request patching.
contextProvider() => CostCanaryContextOptionalFallback context provider for each LLM event when no AsyncLocalStorage context is set.
inferFeatureFromStackbooleanOptional, default: trueEnable stack-based feature inference from call site file paths.
debugbooleanOptionalEnable debug logging. Also enabled via COSTCANARY_DEBUG=true env var.
batchSizenumberOptional, default: 25Events per batch flush. Range: 1–100.
flushIntervalnumberOptional, default: 5000Ms between batch flushes. Range: 500–30000.
Throws
Error if apiKey is missing
Error if batchSize is outside 1–100 range
Error if flushInterval is outside 500–30000ms range
Warnings
Warns if apiKey doesn't start with cck_live_ or cck_test_
Example
javascript
costcanary.init({
  apiKey: process.env.COSTCANARY_KEY,
  autoCapture: true,
  debug: process.env.NODE_ENV !== "production",
  batchSize: 50,
  flushInterval: 3000,
});