npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@knocklabs/node

v0.6.15

Published

Library for interacting with the Knock API

Downloads

1,592,357

Readme

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");

// The key of the workflow (from Knock dashboard)
await knockClient.workflows.trigger("dinosaurs-loose", {
  // user id of who performed the action
  actor: "dnedry",
  // list of user ids for who should receive the notification
  recipients: ["jhammond", "agrant", "imalcolm", "esattler"],
  // data payload to send through
  data: {
    type: "trex",
    priority: 1,
  },
  // an optional identifier for the tenant that the notifications belong to
  tenant: "jurassic-park",
  // an optional key to provide to cancel a workflow
  cancellationKey: triggerAlert.id,
});

Canceling workflows

import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");

await knockClient.workflows.cancel("dinosaurs-loose", triggerAlert.id, {
  // optionally you can specify recipients here
  recipients: ["jhammond"],
});

Identifying users

import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");

await knockClient.users.identify("jhammond", {
  name: "John Hammond",
  email: "[email protected]",
});

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");

// Set an entire preference set
await knockClient.users.setPreferences("jhammond", {
  channel_types: { email: true, sms: false },
  workflows: {
    "dinosaurs-loose": {
      channel_types: { email: false, in_app_feed: true },
    },
  },
});

// Retrieve a whole preference set
const preferences = await knockClient.users.getPreferences("jhammond");

Getting and setting channel data

import { Knock } from "@knocklabs/node";
const knockClient = new Knock("sk_12345");

// Setting channel data for an APNS
await knockClient.users.setChannelData("jhammond", KNOCK_APNS_CHANNEL_ID, {
  tokens: [apnsToken],
});

// Getting channel data for the APNS channel
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";
// An optional object containing the query options passed to Slack:
// https://api.slack.com/methods/conversations.list#arg_cursor
const queryOptions = {
  cursor: null,
  limit: 200,
  exclude_archived: true,
  team_id: null,
  types: "public_channel",
};

// Retrieve a list of Slack channels
const channelsInput = {
  tenant: tenantId,
  knockChannelId: knockSlackChannelId,
  queryOptions,
};
await knockClient.slack.getChannels(channelsInput);

// Check Slack connection
const authInput = { tenant: tenantId, knockChannelId: knockSlackChannelId };
await knockClient.slack.authCheck(authInput);

// Revoke access token
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";

// When signing user tokens, you do not need to instantiate a Knock client.

// jhammond is the user id for which to sign this token
const token = await Knock.signUserToken("jhammond", {
  // The signing key from the Knock Dashboard in base-64 or PEM-encoded format.
  // If not provided, the key will be read from the KNOCK_SIGNING_KEY environment variable.
  signingKey: "S25vY2sga25vY2sh...",
  // Optional: How long the token should be valid for, in seconds (default 1 hour)
  // For long-lived connections, you will need to refresh the token before it expires.
  expiresInSeconds: 60 * 60,
});

// This token can now be safely passed to your client e.g. in a cookie or API response.