Welcome to Highlight
Getting Started
Overview
Backend / Server
Client / Frontend
Fullstack Frameworks
Tips
Content-Security-Policy
Local Development
Monkey Patches
Performance Impact
Proxying Highlight
Session Search Deep Linking
Troubleshooting
Upgrading Highlight
Session Replay
Console Messages
HTML iframe Recording
Identifying Users
Live Mode
Network DevTools
Privacy
Rage Clicks
Recording Network Requests and Responses
Session Sharing
Session Shortcut
Tracking Events
Versioning Sessions
Error Monitoring
Grouping Errors
Sourcemaps
Versioning Errors
Product Features
Alerts
Analytics
Canvas
Comments
Environments
Frontend Observability
Keyboard Shortcuts
Performance Data
Segments
Session Search
Team Management
User Feedback
Web Vitals
WebGL
Integrations
Amplitude Integration
Clearbit Integration
Electron Integration
Front Plugin
Intercom Integration
Linear Integration
Mixpanel Integration
React.js Integration
Segment Integration
Sentry Integration
Slack Integration
Vercel Integration
highlight.run Changelog
5.0.0
5.0.1
5.1.0
5.1.1
5.1.2
5.1.3
5.1.4
5.1.5
Menu

Node.JS SDK API Reference

<section className="section"> <div className="left"> <h3>Node.js SDK</h3> <p> Highlight's Node.js SDK makes it easy to monitor errors and metrics on your Node.js backend. </p> </div> <div className="right"> <h6>Just getting started?</h6> <p>Check out our [getting started guide](/getting-started/backend-sdk/nodejs) to get up and running quickly.</p> </div> </section> <section className="section"> <div className="left"> <h3>H.init</h3> <p>H.init() initializes the Highlight backend SDK. It is required to call this method before recording backend errors or metrics.</p> <h6>Method Parameters</h6> <aside className="parameter"> <h5>options<code>NodeOptions</code> <code>required</code></h5> <p>The configuration for Highlight backend monitoring.</p> <article className="innerParameterContainer"> <aside className="innerParameterHeading">options properties</aside> <aside className="parameter"> <h5>disableErrorSourceContext <code>boolean</code> <code>optional</code></h5> <p>Disables source code context lines for error reporting. This may be useful for performance if your source files are particularly large or memory is limited.</p> </aside> <aside className="parameter"> <h5>errorSourceContextCacheSizeMB <code>number</code> <code>optional</code></h5> <p>Source files are cached in memory to speed up error reporting and avoid costly disk access. The default cache size is 10MB, but this can be overridden. Specifying a value <= 0 removes all cache size limits.</p> </aside> </article> </aside> </div> <div className="right"> <code> import { H } from "@highlight-run/node";
const highlightOptions = {}; if (!H.isInitialized()) { H.init(highlightOptions); } </code>
Copy
</div> </section> <section className="section"> <div className="left"> <h3>H.isInitialized</h3> <p>H.isInitialized() returns true if the Highlight backend SDK has been initialized. This may be handy if your initialization code could be called multiple times, e.g. if it is called conditionally from a request handler when a backend error or metric needs to be recorded.</p> </div> <div className="right"> <code> import { H } from "@highlight-run/node";
const highlightOptions = {}; if (!H.isInitialized()) { H.init(highlightOptions); } </code>
Copy
</div> </section> <section className="section"> <div className="left"> <h3>H.consumeError</h3> <p>H.consumeError() reports an error and its corresponding stack trace to Highlight. The secureSessionId and requestId properties are Highlight ids used to link an error to the session in which the error was thrown. These properties are sent via a header and included in every request to your backend once the Highlight client is initialized. They can be parsed using the H.parseHeaders() helper method.</p> <h6>Method Parameters</h6> <aside className="parameter"> <h5>error<code>Error</code> <code>required</code></h5> <p>The error being reported to Highlight.</p> </aside> <aside className="parameter"> <h5>secureSessionId<code>string</code> <code>required</code></h5> <p>A randomized id representing the Highlight session in which an error was thrown. This can be parsed from the network request's headers using H.parseHeaders().</p> </aside> <aside className="parameter"> <h5>requestId<code>string</code> <code>required</code></h5> <p>A randomized id generated by the Highlight client representing the request for which an error was thrown. This can be parsed from the network request's headers using H.parseHeaders().</p> </aside> </div> <div className="right"> <code> import * as http from 'http'; import { H } from "@highlight-run/node";
const onError = (request: http.IncomingMessage, error: Error): void => { const parsed = H.parseHeaders(request.headers); if (parsed !== undefined) { H.consumeError(error, parsed.secureSessionId, parsed.requestId) } }; </code>
Copy
</div> </section> <section className="section"> <div className="left"> <h3>H.recordMetric</h3> <p>H.recordMetric() reports a metric to Highlight. Backend metrics can be used just like frontend metrics for creating custom dashboards. See [Frontend Observability](/product-features/frontend-observability) for more details.</p> <h6>Method Parameters</h6> <aside className="parameter"> <h5>secureSessionId<code>string</code> <code>required</code></h5> <p>A randomized id representing the Highlight session making the network request. This can be parsed from the network request's headers using H.parseHeaders().</p> </aside> <aside className="parameter"> <h5>name<code>string</code> <code>required</code></h5> <p>The name of the metric being reported.</p> </aside> <aside className="parameter"> <h5>value<code>number</code> <code>required</code></h5> <p>The numeric value of the metric being reported.</p> </aside> <aside className="parameter"> <h5>requestId<code>string</code> <code>optional</code></h5> <p>A randomized id generated by the Highlight client representing the request for this metric. This can be parsed from the network request's headers using H.parseHeaders(). If the metric is not request-specific, this argument can be omitted.</p> </aside> <aside className="parameter"> <h5>tags<code>{ name: string; value: string }[]</code> <code>optional</code></h5> <p>Tags are arbitrary name-value pairs you can associate to your metrics. This is helpful for categorizing data in dashboards, e.g. for grouping or filtering metrics by particular tags.</p> </aside> </div> <div className="right"> <code> import { H } from "@highlight-run/node";
const handler = (request) => { const parsed = H.parseHeaders(request.headers); const start = Date.now(); doInterestingWork(); const elapsed = Date.now() - start; H.recordMetric(parsed.secureSessionId, "elapsedTimeMs", elapsed, parsed.requestId, ["user": "Zane"]); }; </code>
Copy
</div> </section> <section className="section"> <div className="left"> <h3>H.parseHeaders</h3> <p>H.parseHeaders() is a helper function for extracting the Highlight secureSessionId and requestId from network requests. These fields are sent with network requests as the 'x-highlight-request' header, encoded as a slash-separated string: "{secureSessionId}/{requestId}"</p> <h6>Method Parameters</h6> <aside className="parameter"> <h5>headers<code>IncomingHttpHeaders</code> <code>required</code></h5> <p>The headers sent as part of your network request.</p> </aside> </div> <div className="right"> <code> import * as http from 'http'; import { H } from "@highlight-run/node";
const onError = (request: http.IncomingMessage, error: Error): void => { const parsed = H.parseHeaders(request.headers); if (parsed !== undefined) { H.consumeError(error, parsed.secureSessionId, parsed.requestId) } }; </code>
Copy
</div> </section>