DEV Community

Ashutosh Krishna
Ashutosh Krishna

Posted on • Originally published at ireadblog.com on

How to add subscribers to your Revue newsletter programmatically?

Introduction

Image description

Revue makes it easy for writers and publishers to send editorial newsletters β€” and get paid. If you are looking to manage newsletters for your website, Revue is one of the best options out there, that too for free.

Fact: iRead manages its newsletter using Revue. You can subscribe toΒ iRead Weekly Digests for free.

Get started with Revue here and register for free now.

Why add subscribers programmatically?

You might be thinking why should you add subscribers programmatically when users can themselves visit your newsletter page and subscribe themselves. Well, do you remember signing up for the newsletters on the shopping website you use? I don't think so. Still, when you sign up for an account on the site, you start receiving mails daily, weekly, or monthly. That's the use-case.

Consider a situation where you have a multi-user blogging platform like iRead. You have added an option for users to signup for newsletters at the bottom of the page. But users generally don't scroll till the end. How about adding the same functionality like the shopping website? When a new user registers on your website, you programmatically add the user to your Revue newsletter subscriber list. Isn't that amazing? Let's do it in the next step.

How to add subscribers programmatically?

Revue has made this process quite easy for us. Revue provides us with an API to manage it. Read their official API documentation here. With this, you also get an API Key when you register for a free account on Revue. To find your API Key, log in to your Revue account. Then go to Account Settings and select the Integrations tab. You'll find your API Key at the bottom of this page.

Note: If you don't know how to interact with APIs in Python, read this tutorial.

To continue with the coding part, you must have requests library installed. If not, you can install using the command:

pip install requests
Enter fullscreen mode Exit fullscreen mode

Additionally, you can also install Rich to beautify your terminal output. This is an optional step. To install Rich, use the command:

pip install rich
Enter fullscreen mode Exit fullscreen mode

To learn more about Rich, read this tutorial.

Let's jump into the coding part now.

import requests
from rich import print as rprint # Optional 

BASE_URL = 'https://www.getrevue.co/api'

API_KEY = 'add-your-api-key-here'

headers = {
    'Authorization': f'Token {API_KEY}'
}

data = {
    'email': 'user@domain.com',
    'first_name': 'Ashutosh',
    'last_name': 'Krishna', #Optional
    'double_opt_in': False # True if you wish to notify the user
}

response = requests.post(f'{BASE_URL}/v2/subscribers/',
                         headers=headers, json=data).json()

rprint(response)
# If you don't want to use Rich, comment the above line and uncomment the below line
# print(response)

Enter fullscreen mode Exit fullscreen mode

As per the official API documentation of Revue, the base URL is https://www.getrevue.co/api. To authenticate ourselves with the server, we need to add the API Key in the header of the request as Token <API-KEY>. Further, we need to pass our data in JSON format. We can use Python dictionaries for the same. The email and first_name are mandatory fields. The last_name field is optional. The double_opt_in field means whether we wish to notify the users after they are added to our subscribers' list or not. If we don't wish to notify, we set it to False, else it is set to True, which is the default value too. Then we make a POST request on the /v2/subscribers/ route as per the documentation with the headers and JSON data. We then print the JSON response we receive from the server either using the print function provided by Rich or the built-in print function.

Output:

Conclusion

In this blog, we saw how we can programmatically add subscribers to our Revue newsletter. If you liked this post, you can consider subscribing to my newsletter.

Top comments (0)