Knock Node.js library
Knock API access for applications written in server-side Javascript. This package is compatible with the Vercel Edge runtime and with Cloudflare Workers.
Documentation
See the documentation for Node.js usage examples.
Installation
npm install @knocklabs/node
Configuration
To use the library you must provide a secret API key, provided in the Knock dashboard.
If you are using enhanced security mode you will also need to provide your signing key.
You can set both as environment variables:
KNOCK_API_KEY="sk_12345"
KNOCK_SIGNING_KEY="S25vY2sga25vY2sh..."
You can also pass the Knock API key in the constructor. The signing key is passed separately to the signUserToken
method (see below):
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
Usage
Sending notifications (triggering workflows)
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
await knockClient.workflows.trigger("dinosaurs-loose", {
actor: "dnedry",
recipients: ["jhammond", "agrant", "imalcolm", "esattler"],
data: {
type: "trex",
priority: 1,
},
tenant: "jurassic-park",
cancellationKey: triggerAlert.id,
});
Canceling workflows
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
await knockClient.workflows.cancel("dinosaurs-loose", triggerAlert.id, {
recipients: ["jhammond"],
});
Identifying users
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
await knockClient.users.identify("jhammond", {
name: "John Hammond",
email: "jhammond@ingen.net",
});
Retrieving users
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
await knockClient.users.get("jhammond");
Deleting users
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
await knockClient.users.delete("jhammond");
Preferences
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
await knockClient.users.setPreferences("jhammond", {
channel_types: { email: true, sms: false },
workflows: {
"dinosaurs-loose": {
channel_types: { email: false, in_app_feed: true },
},
},
});
const preferences = await knockClient.users.getPreferences("jhammond");
Getting and setting channel data
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
await knockClient.users.setChannelData("jhammond", KNOCK_APNS_CHANNEL_ID, {
tokens: [apnsToken],
});
await knockClient.users.getChannelData("jhammond", KNOCK_APNS_CHANNEL_ID);
Slack
import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");
const tenantId = "tenant-123";
const knockSlackChannelId = "7c1e0042-5ef2-411a-a43b-e541acb139ed";
const queryOptions = {
cursor: null,
limit: 200,
exclude_archived: true,
team_id: null,
types: "public_channel",
};
const channelsInput = {
tenant: tenantId,
knockChannelId: knockSlackChannelId,
queryOptions,
};
await knockClient.slack.getChannels(channelsInput);
const authInput = { tenant: tenantId, knockChannelId: knockSlackChannelId };
await knockClient.slack.authCheck(authInput);
const revokeInput = { tenant: tenantId, knockChannelId: knockSlackChannelId };
await knockClient.slack.revokeAccessToken(revokeInput);
Signing JWTs
When using enhanced security mode (recommended in production), you will need to sign JWTs server-side to authenticate your users.
You will need to generate an environment specific signing key in the Knock dashboard under "API Keys", and then enable enhanced security mode for your environment.
import { Knock } from "@knocklabs/node";
const token = await Knock.signUserToken("jhammond", {
signingKey: "S25vY2sga25vY2sh...",
expiresInSeconds: 60 * 60,
});