Python
Learn how to use Novu to quickly send multi-channel (SMS, Email, Chat, Push) notifications with Python.
In this guide, you’ll learn how to use Novu in a Python application. Let’s see how you can seamlessly integrate Novu into your Python project!
Prerequisites
Before diving into the Quickstart, make sure you have the following:
- Python3 installed on your development machine.
- A Novu account. If you don’t have one, sign up for free at web.novu.co
Install and Set Up Novu in your Python Project
First, you must install the Novu package in your Python project. From your terminal, you can install the Novu package in your project by running either of the following two commands:
Via pip
Via poetry
Once installed, you can import Novu into your project and initialize it using your Novu account credentials. This step establishes a connection between your app and the Novu notification service.
Replace the <NOVU_API_KEY>
value with the authentic key from the API Key section of your Novu Dashboard.
Set Up A Channel Provider
A channel provider is a service that lets you use one or more notification channels such as sending an email, SMS, push notification etc. Our integration store currently supports four channels: Email, SMS, Chat, and Push. Each of these channels have multiple providers associated with them.
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 |
Only one provider can be active per channel at a time. Connect anyone of your preferred providers to get started. The email channel comes with Novu’s email provider, which is active by default and includes 300 credits.
Create a Notification Workflow
A notification workflow is the blueprint for the notifications that will be sent. It holds the entire flow of messages sent to the subscriber.
The workflow includes the following:
- Notification workflow name and Identifier
- Channel tailored content:
Channel | Content Style | Custom Variables{{handlebars}} format |
---|---|---|
HTML | ✅ | |
Visual Editor | ✅ | |
SMS | Text | ✅ |
Chat | Text | ✅ |
In-App | Text | ✅ |
Push | Text | ✅ |
To create a notification workflow, please follow the following steps:
-
Click Workflows on the left sidebar of your Novu dashboard.
-
Click the Add a Workflow button on the top left.
-
Select blank workflow from the dropdown.
-
The name of a new workflow is currently “Untitled.” Rename it to a suitable title.
-
Select any channel you want to use in your app. For the sake of this guide, we’ll be using the ‘Email’ channel.
-
Click on the recently added channel, fill the email subject and click “Update”.
-
Click on the “Test” tab and send a test email to verify your notification workflow.
You should get an email within seconds. If you didn’t, please check your ‘spam’ folder as sometimes test emails can end up there. Yaaay, you have successfully sent your first notification via the Novu dashboard!
Now, let’s take it a step further to trigger notifications via code.
Create A Subscriber
The recipients of a triggered notification are called subscribers.
Click Subscribers on the left sidebar of the Novu dashboard to see all subscribers. By default, the dashboard will display only one subscriber, as you were added automatically during sign-up. If default subscriber is not created, checkout how to create new subscriber.
Now, let’s create a subscriber on Novu. Copy and paste the following code to do so:
Run the code in your terminal like so:
You should see a new subscriber (that you created above) on your Novu dashboard.
Update A Subscriber
To update the Subscriber details you can call the put method from SubcriberApi. Here is an example:
Trigger A Notification
Copy and paste the following code into your app to trigger a notification:
Before running the code, make sure you understand the following:
- The value of
name
should be the notification workflow’s trigger ID/slug.
- The value of
payload
is an array of the data that you want to be dynamically injected into the notification template content. - The value of
recipients
is the id of the subscriber on Novu. Replace123
with your subscriber ID.
Run the code to trigger a notification!
Next, we’ll learn how to send notifications to different groups of subscribers easily via Topics.
Topics
Novu provides a simple API that offers an easy interface for triggering notifications to multiple subscribers at once. This API is called Topics and allows users to manage their bulk notifications without having to implement complex loops.
A topic is identified by a custom key and this key will be the identifier used when triggering notifications. You can assign a name to a topic for descriptive purposes. This name does not need to be unique and can be changed programmatically.
A topic can have multiple subscribers who will receive a notification whenever a message is sent to the topic.
Create a Topic
Copy and paste the following code into your app to create a topic:
Before running the code, make sure you understand the following:
- When creating a
key
, ensure it is unique and accurately identifies the topic. Document naming conventions and communicate them to team members to avoid confusion and ensure a smooth workflow. - The value of
name
should be a descriptive topic name.
Add subscribers to a Topic
Copy and paste the following code into your app to add subscribers a topic:
On the other hand, if you want to remove subscribers from a topic, do the following:
Sending a notification to a Topic
Thanks to the topics feature, it is possible to trigger a notification to all subscribers assigned to a topic. This helps avoid listing all subscriber identifiers.
To trigger a notification to all subscribers of a topic, copy and paste the code below:
Next Steps
Great job! If you’ve reached this point, you should now have successfully set up a channel provider, created a notification workflow, created a subscriber, updated a subscriber and triggered a notification in your application.
To learn more about notifications and explore Novu’s features and capabilities, check out:
- Novu Digest Engine - Learn how to aggregate multiple trigger events into a single message and deliver it to the subscriber.
- Novu Python SDK - Delve deeper into the SDK and explore a lot of features.
- Novu Python Cookbook - Learn how to use the SDK features.