Skip to content

Node.js SDK

The 9n9s Node.js package provides a simple way to integrate Heartbeat Monitoring into your JavaScript or TypeScript applications.

Terminal window
npm install @9n9s/sdk

First, initialize a client with your Heartbeat Monitor’s UUID. It’s best practice to store this in an environment variable.

import { Nines } from "@9n9s/sdk";
const nines = new Nines(process.env.MY_JOB_MONITOR_ID);

To send a simple success pulse:

await nines.pulse();

To track the execution time of a function, you can use the nines.time() wrapper. It will automatically send a /start pulse, execute your function, and then send a completion pulse. If your function throws an error, it will automatically send a /fail pulse with the error details.

async function myImportantJob() {
// ... your job logic ...
console.log("Doing work!");
}
await nines.time(myImportantJob);

You can also do this manually:

await nines.start();
try {
// ... your job logic ...
await nines.pulse(); // or nines.success()
} catch (error) {
await nines.fail(error);
}

You can send logs, error objects, or any other string payload with your pulses.

// Send a simple log message
await nines.pulse("Processed 100 items.");
// Send an error object on failure
try {
throw new Error("Database connection failed");
} catch (error) {
// The SDK will automatically serialize the error object
await nines.fail(error);
}

The SDK is great for monitoring serverless function executions.

// Example with an AWS Lambda handler
import { Nines } from "@9n9s/sdk";
const nines = new Nines(process.env.MY_LAMBDA_MONITOR_ID);
export const handler = async (event) => {
await nines.start();
try {
// ... function logic ...
const result = "OK";
await nines.pulse(`Execution finished with result: ${result}`);
return { statusCode: 200, body: "Success" };
} catch (error) {
await nines.fail(error);
return { statusCode: 500, body: "Error" };
}
};