Learn how to use Novu to quickly send multi-channel (SMS, Email, Chat, Push) notifications with NextJS.
pages
directory.
By creating a new file in the pages
directory, such as about.js
, we automatically create a route /about
in our application. Next.js handles the routing logic behind the scenes, ensuring that when a user accesses /about
, the corresponding page component is rendered.
In our app, we’ll create a route that will send an email notification to a specified email id. To do this, we need two things:
Obtain your API key from the settings menu
Channel | Providers | |
---|---|---|
MailGun, Mandrill, MailJet, Amazon SES, Sendgrid, Postmark, Netcore | ||
SMS | Twilio, Amazon SNS, Plivo, SMS, SMSCentral, Kannel, Infobip, Termii | |
Chat | Mattermost, Slack, Microsoft Teams, Discord | |
Push | FCM, APNS, Expo |
Click the “Create Workflow” button on the top right to create a workflow
Rename the workflow to a more appropriate name
Select Email as the channel you want to add, by dragging it from the right sidebar.
Edit the email node
Add variables and send a test email to verify the workflow
subscriber id
would be a unique id generated automatically by the database. So for our demo purpose, we’re using a simple subscriber id of ‘1234567890’ in our app.
We’ll first create a subscriber using the following code:
1234567890
and now we’ll send a notification to the subscriber with this very subscriber id by triggering an email notification like follows:
pages
directory in the root of the project and create a file in it. Give it a name and remember that it will automatically become a route.
In our case, we’re creating a directory called api
in our pages directory, and inside api
we’re creating a file called sub.js
. In this case, our path will be: http://localhost:3000/api/sub
In this file, we simply need to define a function that will handle a POST request to our API. It’ll extract the ‘description’ and ‘email’ variables from the ‘request’ body that will be generated every time we make a POST request and call the function to send an email notification with those plugged in.
We’d used these variables in the workflow we had created in the Novu dashboard and also specified the same in the trigger function above.
The function in our route is quite simple and looks like this:
http://localhost:3000/api/sub
Also, make sure that in the body, you’re passing the two variables we’re extracting above, namely - email and description, as follows:
Testing on Postman
In place of youremail@gmail.com, use your actual email and once you send it, you should see the ‘message: email working’ on the bottom as in the image above.This means that the email notification was sent successfully. Now go to your inbox and you should see an email notification like the following:
The triggered email notification in the inbox
Make sure that you use a unique key for a Topic. Keys once used, can’t be changed later!You have the flexibility to assign a descriptive name to a topic. Unlike the topic key, this name does not require uniqueness and can be modified using the provided API. A topic can have multiple subscribers associated with it. These subscribers will receive notifications whenever a notification is dispatched to the respective topic.
key
and name
. Keys are unique identifiers for topics and a name is just something you assign to a topic for convenience.
create
method on the topics
property of the **novu
**instance. This method creates a new topic in the Novu system using the provided key
and name
values.
If you test this on your local machine, you should get something like this:
Topics created successfully
Note how the return object contains the key I sent from my request body. That signals successful creation!
subscriberID
and topicKey from the request body and call the addSubscribers
method on the topics
property of the novu
instance, passing the topic key and an object with an array of subscribers.
This adds the subscriber with the subscriberID
we’d passed to the array of subscribers, which in the above case contains just one subscriber.
If you check this on Postman, the returned array will contain the subscriberID
that we had passed in the request body, signalling that it was added to the topic successfully.
If, on the other hand, you find the passed subscriberId
in the notFound
array inside the failed
object, it means the subscriber wasn’t added to the topic.
You can read more about it here.
The image below shows the case where the subscriber has been added successfully:
Subscriber added to the topic
Notification successfully sent
Notification in my mailbox