|
| 1 | +--- |
| 2 | +title: 'Trigger workflows to topics' |
| 3 | +description: "Learn how to trigger workflows to one or more topics and how to exclude specific subscribers from a trigger." |
| 4 | +icon: 'Share2' |
| 5 | +--- |
| 6 | + |
| 7 | +Once you've created your topics and added subscribers, you're ready to send them notifications. Triggering a workflow to a topic is the core mechanism for broadcasting a message to a large audience with a single API call. |
| 8 | + |
| 9 | +When you trigger a workflow using a topic key, Novu performs a **fan-out**: it identifies all subscribers within that topic and creates an individual workflow run for each one. |
| 10 | + |
| 11 | +This process is handled entirely by Novu, saving you from writing complex loops or sending thousands of separate API requests. The trigger syntax is the same as for individual subscribers; you only need to specify the `type` as `Topic` in the `to` field. |
| 12 | + |
| 13 | + |
| 14 | +### Trigger to a single topic |
| 15 | + |
| 16 | +The most common use case is broadcasting a notification to every subscriber within a single topic. This is ideal for product announcements, incident updates, or other targeted mass communications. |
| 17 | + |
| 18 | +To trigger a workflow to a topic, use the `to` field with `type: "Topic"` and the corresponding `topicKey`. |
| 19 | + |
| 20 | +<Tabs items={["Node.js", "cURL"]}> |
| 21 | + <Tab value="Node.js"> |
| 22 | +```ts |
| 23 | +import { Novu } from "@novu/api" |
| 24 | + |
| 25 | +const novu = new Novu({ secretKey: "<YOUR_SECRET_KEY_HERE>", }); |
| 26 | + |
| 27 | +await novu.trigger({ |
| 28 | + to: { type: "Topic", topicKey: "topic_key" }, |
| 29 | + workflowId: "workflow_identifier", |
| 30 | +}); |
| 31 | +``` |
| 32 | + </Tab> |
| 33 | + <Tab value="cURL"> |
| 34 | +```bash |
| 35 | +curl -L -g -X POST 'https://api.novu.co/v1/events/trigger' \ |
| 36 | +-H 'Content-Type: application/json' \ |
| 37 | +-H 'Accept: application/json' \ |
| 38 | +-H 'Authorization: ApiKey <NOVU_SECRET_KEY>' \ |
| 39 | +-d '{ |
| 40 | + "name": "workflow_identifier", |
| 41 | + "to": { |
| 42 | + "type": "Topic", |
| 43 | + "topicKey": "topic_key" |
| 44 | + } |
| 45 | +}' |
| 46 | +``` |
| 47 | + </Tab> |
| 48 | +</Tabs> |
| 49 | + |
| 50 | +### Trigger to multiple topics |
| 51 | + |
| 52 | +You can also broadcast a notification to several topics at once by passing an array in the to field. This is useful when your audience spans multiple groups, such as notifying both "paying-customers" and "beta-testers" about an important update. |
| 53 | + |
| 54 | +Novu automatically handles de-duplication, so if a subscriber belongs to more than one of the specified topics, they will only receive the notification once. |
| 55 | + |
| 56 | +<Tabs items={["Node.js", "cURL"]}> |
| 57 | + <Tab value="Node.js"> |
| 58 | +```ts |
| 59 | +import { Novu } from "@novu/api" |
| 60 | + |
| 61 | +const novu = new Novu({ secretKey: "<YOUR_SECRET_KEY_HERE>", }); |
| 62 | + |
| 63 | +await novu.trigger({ |
| 64 | + to: [ |
| 65 | + { type: "Topic", topicKey: "topic_key_1" }, |
| 66 | + { type: "Topic", topicKey: "topic_key_2" } |
| 67 | + ], |
| 68 | + workflowId: "workflow_identifier", |
| 69 | +}); |
| 70 | +``` |
| 71 | + </Tab> |
| 72 | + <Tab value="cURL"> |
| 73 | +```bash |
| 74 | +curl -L -g -X POST 'https://api.novu.co/v1/events/trigger' \ |
| 75 | +-H 'Content-Type: application/json' \ |
| 76 | +-H 'Accept: application/json' \ |
| 77 | +-H 'Authorization: ApiKey <NOVU_SECRET_KEY>' \ |
| 78 | +-d '{ |
| 79 | + "name": "workflow_identifier", |
| 80 | + "to": [ |
| 81 | + { |
| 82 | + "type": "Topic", |
| 83 | + "topicKey": "topic_key_1" |
| 84 | + }, |
| 85 | + { |
| 86 | + "type": "Topic", |
| 87 | + "topicKey": "topic_key_2" |
| 88 | + } |
| 89 | + ] |
| 90 | +}' |
| 91 | +``` |
| 92 | + </Tab> |
| 93 | +</Tabs> |
| 94 | + |
| 95 | +### Exclude an actor for a notification |
| 96 | + |
| 97 | +When a workflow is triggered to a topic, notification is sent to all subscribers present in the topic. However, you can exclude a specific subscriber (for example, the user who performed the action) by passing their subscriberId in the actor field. |
| 98 | + |
| 99 | +You can achieve this by passing the `subscriberId` of the initiator in the `actor` field. Novu will then exclude this subscriber from that specific trigger event. |
| 100 | + |
| 101 | + |
| 102 | +<Tabs items={["Node.js", "cURL"]}> |
| 103 | + <Tab value="Node.js"> |
| 104 | +```ts |
| 105 | +import { Novu } from "@novu/api" |
| 106 | + |
| 107 | +const novu = new Novu({ secretKey: "<YOUR_SECRET_KEY_HERE>", }); |
| 108 | + |
| 109 | +await novu.trigger({ |
| 110 | + to: [{ type: "Topic", topicKey: "topic_key_1" }], |
| 111 | + workflowId: "workflow_identifier", |
| 112 | + actor: "actor_subscriber_Id" |
| 113 | +}); |
| 114 | +``` |
| 115 | + </Tab> |
| 116 | + <Tab value="cURL"> |
| 117 | +```bash |
| 118 | +curl -L -g -X POST 'https://api.novu.co/v1/events/trigger' \ |
| 119 | +-H 'Content-Type: application/json' \ |
| 120 | +-H 'Accept: application/json' \ |
| 121 | +-H 'Authorization: ApiKey <NOVU_SECRET_KEY>' \ |
| 122 | +-d '{ |
| 123 | + "name": "workflow_identifier", |
| 124 | + "to": [ |
| 125 | + { |
| 126 | + "type": "Topic", |
| 127 | + "topicKey": "topic_key_1" |
| 128 | + } |
| 129 | + ], |
| 130 | + "actor": "actor_subscriber_Id" |
| 131 | +}' |
| 132 | +``` |
| 133 | + </Tab> |
| 134 | +</Tabs> |
0 commit comments