docs/.mintlify/skills/trigger-notification/references/topic-trigger-examples.md
Topics let you send notifications to groups of subscribers. Create a topic, add subscribers, then trigger to the topic.
import { Novu } from "@novu/api";
const novu = new Novu({
secretKey: process.env.NOVU_SECRET_KEY,
});
// Create a topic
await novu.topics.create({
key: "project-alpha-watchers",
name: "Project Alpha Watchers",
});
// Add subscribers to the topic
await novu.topics.subscriptions.create(
{ subscriptions: ["user-1", "user-2", "user-3"] },
"project-alpha-watchers"
);
All subscribers in the topic receive the notification:
const result = await novu.trigger({
workflowId: "project-update",
to: {
type: "Topic",
topicKey: "project-alpha-watchers",
},
payload: {
projectName: "Alpha",
update: "New release v2.0 deployed",
},
});
const result = await novu.trigger({
workflowId: "company-announcement",
to: [
{ type: "Topic", topicKey: "engineering-team" },
{ type: "Topic", topicKey: "product-team" },
],
payload: {
announcement: "Company all-hands at 3pm",
},
});
curl -X POST https://api.novu.co/v1/events/trigger \
-H "Authorization: ApiKey $NOVU_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "project-update",
"to": {
"type": "Topic",
"topicKey": "project-alpha-watchers"
},
"payload": {
"projectName": "Alpha",
"update": "New release v2.0 deployed"
}
}'