Node.js
Express, Fastify, NestJS or plain Node. About five minutes, and nothing changes for your users.
1. Install#
The SDK is one file, kept with your code (it isn't on npm yet). In your server's folder, with your Controxity's address:
curl -fsSL https://controxity.example.com/sdk/node.js -o controxity.js
curl -fsSL https://controxity.example.com/sdk/node.d.ts -o controxity.d.ts # TypeScript onlyIts first line says its version (// controxity-sdk 0.25.6 (Node.js)): fetch it again the same way to update it. Then require("./controxity") (or import controxity from "./controxity.js") where the examples below say "controxity".
Get a key under Where it runs → Add a place and keep it in CONTROXITY_KEY (see Your first error).
2. Start it, as early as you can#
Before your other imports, so it sees errors from the start.
const controxity = require("controxity"); // or: import controxity from "controxity"
controxity.init({
key: process.env.CONTROXITY_KEY,
url: process.env.CONTROXITY_URL,
version: "1.4.2", // or the git commit it runs: "3f9c2a1"
});3. Connect your framework#
app.use(controxity.express()); // after your routescontroxity.fastify(app); // before your routesconst app = await NestFactory.create(AppModule);
controxity.nest(app); // right after create// instrumentation.ts, next to app/ (Next.js 15; 13.4 and 14 need experimental.instrumentationHook)
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") require("./controxity").init({ key: process.env.CONTROXITY_KEY, url: process.env.CONTROXITY_URL });
}
export const onRequestError = (...args) => require("./controxity").nextOnRequestError(...args);
// in the browser, the browser SDK with a browser key: NEXT_PUBLIC_CONTROXITY_BROWSER_KEY// in your own error handler, where it answers with a 500:
controxity.captureError(error, { input: { params: req.params, query: req.query, body: req.body } });Already have your own catch-all error handler or filter? Then the framework line may never run: add controxity.captureError(error, { input }) where your handler deals with a 500, with the request's params, query and body as input (only their shape is sent). Each request keeps its own steps by itself; to start them with the route, call controxity.step("route:POST /carts/:id") first, with the request's method and its route's pattern (never the path asked for, which can hold an id). A 4xx is the visitor's mistake, so it isn't reported.
4. Check it works#
console.log(await controxity.check()); // { ok: true, why: "Connected to controxity.example.com" }It says what's wrong if the key is refused or Controxity can't be reached. Then make one route throw: it's on Problems within a minute.
5. Ship safely (optional)#
// What must always be true. A broken one is a problem, but nothing is thrown:
controxity.expect("total-not-negative", total >= 0, { input: { cart } });
// New or risky code, off until a person turns it on (for everyone or a share), and off again by itself if it hurts:
if (controxity.flag("new-checkout", { id: user.id })) return newCheckout(cart);
// A step worth seeing before an error (routes, fetch calls and events are noted by themselves):
controxity.step("cart.loaded");Expectations · Switches · Steps before an error. In CI, one more step tells Controxity what each release is and sends its source maps: Releases from CI.
TypeScript#
To see your .ts files, not the compiled ones: set "sourceMap": true in tsconfig.json and start with node --enable-source-maps dist/main.js. The flag matters, because your imports load before init() runs.
Also useful#
controxity.captureError(error, { where: "orders" }); // an error you caught yourself
controxity.health("database", false, "high"); // something is down; health("database", true) clears it
controxity.event("payment_failed"); // counted
controxity.shape({ items: undefined }); // what captureError sends for input: { items: "undefined" }
await controxity.report({ title: "Booking failed", what }); // what a person told you
await controxity.shutdown(); // sends what's waiting, when the app stopsEvery function, with what it sends: Node SDK reference.
Is it up?#
Once a minute, when there's nothing else to send, it sends a heartbeat. If they stop for 3 minutes, Controxity marks this server Down, tells the people on the product, and tells them again when it's back. shutdown() says it's stopping on purpose, so a planned stop isn't counted as an outage.
A crash that ends the process is saved to a small file in the system's temp folder and sent when your app starts again.
If nothing shows up#
- Look for a line starting
controxity:in your server's log: it says what's wrong, once. - "The key was refused": nothing more is sent for an hour (an app with an old key doesn't knock on Controxity every minute). Fix the key and restart, or run
check(), and it sends at once. - Run
check(). - A route's error needs step 3.
- Signals are sent every minute, or at
shutdown(). It never throws into your app. - Steps from several requests mixed together? Update the SDK to 0.15.0, which keeps them apart by itself (older ones need
app.use(controxity.trail())before an Express app's routes).
Turning things off#
Option of init() | Off means |
|---|---|
captureInput: false | No input shapes |
breadcrumbs: false | No steps, and fetch is left as it was |
captureUncaught: false | Uncaught exceptions aren't reported |
heartbeat: false | No heartbeat (not a server) |