SDK Reference
The @bettermeter/node SDK is zero-dependency (uses built-in fetch and crypto). Requires Node.js 18+.
Constructor
const bm = new BetterMeter(config);siteIdrequiredapiKeyrequiredapiUrlallowCustomApiUrldisabledbatchbatchIntervalhashUserIdredactPropertiesSecurity defaults are conservative: API keys are required for SDK ingestion, flag values are stripped, custom properties cannot override trusted SDK fields, user IDs are hashed by default, and sensitive property keys are redacted before events are sent.
trackCommand(options)
commandrequiredsubcommandflagsversiondurationMsexitCodeisCiuserIdpropertiestrackTool(options)
toolrequiredclientprotocolVersiondurationMssuccesserrorTypeinputTokensoutputTokensuserIdpropertiestrackApi(options)
methodrequiredendpointrequiredstatusCodedurationMsuserIdpropertiesAuto-wrappers
wrapCommander(program, options?)
Hooks into Commander.js postAction to auto-track all commands.
wrapMcpServer(server)
Monkey-patches server.tool() to wrap all handlers with timing and error tracking.
expressMiddleware()
Returns an Express/Connect middleware that tracks every request on res.end.
Server-Side Bot Detection
Most bots don't execute JavaScript, so the browser tracker never fires for them. Use reportBotVisit() in your server middleware to detect bots at the request level.
// Next.js middleware
import { reportBotVisit } from "@bettermeter/node/middleware";
export function middleware(request) {
reportBotVisit(request, "my-site.com", { apiKey: "bm_..." });
// ... rest of your middleware
}requestrequiredsiteIdrequiredoptions.apiUrloptions.apiKeyoptions.allowCustomApiUrlEdge-compatible (no Node.js dependencies). Non-blocking -- adds zero latency to responses. Automatically skips static assets, API routes, and Next.js internals.
Lifecycle
flush(): Promise<void>
Send all queued events immediately.
shutdown(): Promise<void>
Stop batch timer and flush remaining events. Call before process exit.