How to use the Lists API to send to Slack, Inbox, and Email (NodeJS)

How to use the Lists API to send to Slack, Inbox, and Email (NodeJS)

We are using the '@trycourier/courier' node.js sdk within our Next.js/TypeScript app.

We would like to use the Lists API and Profiles API to send a Slack, Inbox, and Email message when a project status has changed in our app.

  1. How can I get the b) code below to send a Slack, Inbox, and Email to the Profiles in a List?
  2. Should I be using courier.lists.send() or courier.send() when working with Lists?
  3. Would it be better to use the Audience Object instead of the Lists API for what I am trying to do?

Example Code

Example of a List ID: 'organizationId.project.status_changed'

I created a list and profile with the following code:

const userId = req.body.user.id;
const { status } = await courier.mergeProfile({
  recipientId: userId,
  profile: {
    email: req.body.user.email,
    given_name: req.body.user.given_name,
    courier: { channel: userId },
    custom: {},
  },
});

// listId = 'organizationId.project.status_changed'
await courier.lists.subscribe(req.body.listId, userId);

a) This code works to send a direct message via Slack, Inbox, and users email:

import { CourierClient } from '@trycourier/courier';
import { NextApiRequest, NextApiResponse } from 'next';

const slackBotUserOAuthToken = '---';
const courierTestKey = '---';
const courier = CourierClient({ authorizationToken: courierTestKey });

export default async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
  const courierResponse = await courier.send({
    message: {
      to: [
        { slack: { access_token: slackBotUserOAuthToken, email: req.body.email } },
        { courier: { channel: req.body.userId } },
        { email: req.body.email },
      ],
      content: { title: 'Project Status Change', body: 'Some text.' },
      routing: { method: 'all', channels: ['direct_message', 'push', 'email'] },
    },
  });

  res.status(201).json(courierResponse);
};

b) This code only sends an email to the Profile in a List and not to Slack or the Inbox:

// listId = 'organizationId.project.status_changed'

const courierTestKey = '---';
const courier = CourierClient({ authorizationToken: courierTestKey });

export default async (req: INextApiRequest<IListEvent>, res: NextApiResponse<IResponseData>): Promise<void> => {
  // This code only sends to the profile email:
  const response = await courier.send({
    message: {
      to: [{ list_id: req.body.listId }],
      content: { title: 'Project Status Change', body: 'Some text.' },
    },
    routing: { method: 'all', channels: ['direct_message', 'push', 'email'] },
  });

  // This code doesn't do anything:
  // const response = await courier.lists.send({
  //   event: 'COURIER_LIVE_ALERT',
  //   list: req.body.listId,
  //   data: { title: 'Project Status Change', body: 'Some text.' },
  // });

  res.status(201).json({});
};

Hey @robert :wave:

For any members that make part of your list, each Courier profile that is inside that list needs to have the profile configured so that the /send request can be successfully made to both Slack and Courier Inbox.

The first example you shared works because you specified the profile in the to/profile object to send to slack, courier, and email. So, if you wanted each member of your list_id to get a successful message, each user_id in the profile would need a configuration like this:

I update the json for the profile and I still only get an email and not the slack or inbox message.

I used --- in the screenshot to hide personal information.

I have my “CONFIGURED PROVIDERS” but do I need to do anything with “Set your inline call defaults”?

I could see that the requests to your specific user were successful when you sent them to Slack and Courier. With that configured Slack profile you’re sending a DM to a user with their email associated. Please make sure that your Slack app has the required scopes.

The scopes I have for the Slack app are:

Bot Token Scopes
calls:read, calls:write, channels:read, chat:write, dnd:read, files:read, groups:read, im:history, im:read, im:write, mpim:history, mpim:read, mpim:write, pins:write, reactions:read, reactions:write, remote_files:read, remote_files:share, remote_files:write, team:read, users:read, users:read.email, users:write

User Token Scopes
chat:write, im:history, team:read, users:read, users:read.email


If I set the default routing “Always send to” to include Courier and Slack then it sends to all three providers but it seems I shouldn’t have to do that?

That routing strategy is correct. When you select the method “always send to” Courier will send the notification to every configured channel in the template. When you select “send to best of”, Courier will send to the first channel following a top-down hierarchy meaning that if the first channel failed (bc of an incomplete profile, or another reason) Courier will send to the following channel in the hierarchy.

Looks like my problem was I needed to create a Template and set the channels to “Always send notification to”. I was trying to do everything without using a Template.