fetch API (no polyfills needed), and handles all the reliability concerns — batching, retries, and graceful shutdown — so you can focus on recording the events that matter.
Installation
Initialization
ImportAuditRails and create a single client instance. The only required option is your API key — everything else has a sensible default.
Configuration options
The internal flush timer calls
.unref() on itself, which means it will not prevent your Node.js process from exiting naturally when it is otherwise idle.
Logging events
Buffered logging (recommended)
audit.log() adds an event to an in-memory buffer and returns immediately. The buffer is flushed to the API in the background every second (or when it reaches batchSize events). This method never throws — any network or API errors are handled internally.
Direct (immediate) logging
audit.logDirect() bypasses the buffer and sends the event immediately. It returns a promise that resolves with the API response — including the logId assigned to the event — and throws on any error. Use this when you need a confirmed receipt before continuing.
Direct batch logging
audit.logBatchDirect() sends multiple events in a single HTTP request, bypassing the buffer. Like logDirect, it throws on error.
Manual flush
Callaudit.flush() to immediately drain the buffer without shutting down the client.
Framework integration
Express.js
You can wrapaudit.log() in Express middleware to capture every request automatically. Logging inside the res.on('finish', ...) callback ensures you capture the final HTTP status code.
Next.js (App Router)
In serverless environments like Next.js, you should disableautoShutdown (the process is long-lived and managed by the framework) and export a singleton from a shared module.
Error handling
log() never throws. For logDirect() and logBatchDirect(), catch AuditRailsError to inspect the failure. AuditRailsTimeoutError is a subclass specifically for request timeouts.
Graceful shutdown
WhenautoShutdown is true (the default), the SDK automatically registers process-exit handlers (beforeExit, SIGTERM, SIGINT) and flushes any buffered events before the process terminates. You do not need to add anything extra in most applications.
If you need manual control — for example in a worker thread, a test suite, or a framework that manages its own lifecycle — call audit.close() explicitly:
In serverless and edge environments that reuse process instances (Next.js, Vercel, AWS Lambda with warm starts), set
autoShutdown: false and call audit.flush() at the end of each handler invocation to avoid losing buffered events between requests.