Novu offers a simple API providing an easy interface for triggering notifications to multiple subscribers at once. It is called Topics and allows the users to manage their bulk notifications without having to do complex loop implementations. A topic is identified by a custom key that is provided by the user. It will be the identifier used in the Topics API.

The topic key should be unique and can’t be changed once chosen. Novu also safe guards for key uniqueness behind the scenes.

Users can also assign a name to a topic. This name doesn’t need to be unique and it can be changed using the API. So far, it is for descriptive goals.

A topic would get assigned different subscribers that will receive a notification every time a notification is sent to the topic.

Create A Topic

In order to be able to send a notification to a topic, first the user needs to create one. It can be done like this:

import { Novu } from '@novu/node';

const novu = new Novu("<NOVU_API_KEY>");

const result = await novu.topics.create({
  key: 'unique-topic-identifier',
  name: 'descriptive-topic-name',
});

If successful this will return the internal ID generated by Novu and the topic key given by the user.

That topic key can be used to retrieve the whole Topic entity:

import { Novu } from '@novu/node';

const novu = new Novu("<NOVU_API_KEY>");

const key = 'unique-topic-identifier';

const result = await novu.topics.get(key);

Rename a Topic

To rename a topic, this call can be used:

import { Novu } from '@novu/node';

const novu = new Novu("<NOVU_API_KEY>");

const topicKey = 'posts:comment:12345';
const topicName = 'Post Comments';

const result = await novu.topics.rename(topicKey, topicName);

This will return the whole Topic information, including the subscribers if having any.

Add subscribers to a Topic

Novu Topics API allows to assign the subscribers to the topic like this:

import { Novu } from '@novu/node';

const novu = new Novu("<NOVU_API_KEY>");

const topicKey = 'posts:comment:12345';

const response = await novu.topics.addSubscribers(topicKey, {
  subscribers: ['subscriber-id-1', 'subscriber-id-2', ...],
});

This call will return a response in the shape of:

{
    succeeded: ['subscriber-id-1', 'subscriber-id-2', ...],
    failed: {
      notFound: ['subscriber-id-1', 'subscriber-id-2', ...],
    },
}

The field succeeded will return the array of subscriber ids that have been correctly assigned to the topic. The field failed will show up only if there has been any subscriber that couldn’t been assigned to the topic. This probably will happen if there is any typo in the subscriber ID given, if the subscriber ID give doesn’t belong to the environment and the organization where the topic has been created.

To facilitate flows we allow to create a topic on the fly when adding subscribers.

If the topic key used does not exist for the selected environment and organization, Novu will create a new topic based on that key, with name Autogenerated-<TOPIC_KEY> and the subscribers will be assigned to this topic created.

After that, that topic created on the fly can be managed in the same ways as the ones created through the creation endpoint.

Verify a subscriber belongs to a topic

There would be times when it would be needed to verify if a certain subscriber belongs to a topic in order to decide what to do with that subscriber. The API call to achieve that is the following:

import { Novu } from '@novu/node';

const novu = new Novu("<NOVU_API_KEY>");

const externalSubscriberId = 'external-subscriber-id-1';
const topicKey = 'posts:comment:12345';

const response = await novu.topics.getSubscriber(topicKey, externalSubscriberId);

The subscriber ID to use in this API call is the one used as subscriber identifier. The choice was done to make easier for the user to use this endpoint without extra calls to Novu.

Remove a subscriber from a Topic

can remove subscribers assigned to a topic. For that this API call can be used:

import { Novu } from '@novu/node';

const novu = new Novu("<NOVU_API_KEY>");

const topicKey = 'posts:comment:12345';

const response = await novu.topics.removeSubscribers(topicKey, {
  subscribers: ['subscriber-id-1', 'subscriber-id-2', ...],
});

Where subscribers will be an array of the subscriber identifiers we want to unassign from the topic. If successful an empty response will be returned.

Delete a topic

A topic can be deleted by its key by doing:

import { Novu } from '@novu/node';

const novu = new Novu("<NOVU_API_KEY>");

const key = 'unique-topic-identifier';

const result = await novu.topics.delete(key);

A topic can only be deleted if it doesn’t have any subscriber added. When trying to delete a topic with subscribers a conflict error will be returned. This is done to prevent accidental topic deletions.