Revue - Sendy sync: project setup + Revue calls

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Now that we have a good understanding of all the API calls we need to make, we can start setting up the project.
I'll be building this project as a Node project simply because it's the lowest overhead and easy to host somewhere.
The goal for today is to have a basic Node project that we can run. On running the code, it should list all unsubscribed people from Revue and all the subscribers.
Let's get started.
Create a new node project.
# Create folder
mkdir revue-sendy-sync
# Navigate into the folder
cd revue-sendy-sync
# Init new node project
npm init
We should now have our basic project with a package.json file.
The first thing I did was change it to module type so we can use imports.
{
"name": "revue-sendy-sync",
"version": "1.0.0",
"type": "module",
...
}
The next thing we want to do is add some packages that we'll use. So far, we know we need some environment variables and want to make some API calls.
The packages we can use for that are dotenv and node-fetch.
npm i dotenv node-fetch
With those installed, we can define a .env file. This file can be used to store your environment variables.
While creating this, also make sure to exclude it by using your .gitignore file. (You don't want your secret to being committed to git!)
Inside the .env file, add the following variable.
REVUE_API_TOKEN={YOUR_TOKEN}
Note: Don't have the token? Read the article on retrieving all the API keys.
Then the last file we need is an index.js file. This will be the brains of the operation.
Create the file, and start by importing the packages we installed.
import dotenv from 'dotenv';
import fetch from 'node-fetch';
dotenv.config();
console.log(`I'm working!`);
You can now try to run this by executing node index.js. In return it should show you "I'm working".
Let's start with the first piece of software. We want to be able to call the Revue API.
We can start with the unsubscribe call.
To make things scaleable, I created a custom function for this purpose.
const getRevueUnsubscribers = async () => {
const response = await fetch(
'https://www.getrevue.co/api/v2/subscribers/unsubscribed',
{
headers: {
Authorization: `Token ${process.env.REVUE_API_TOKEN}`,
'Content-Type': 'application/json',
},
method: 'GET',
}
).then((res) => res.json());
return response;
};
As you can see, we use the node-fetch package to request the unsubscribed endpoint. We then pass the Authorisation header where we set the API token.
Note: This token is loaded from our
.envfile.
Once it returns, we convert the response to a valid JSON object and eventually return that.
Then we have to create a function that runs once our script gets called. This is called an Immediately invoked function expression (IIFE for short).
(async () => {
const revueUnsubscribed = await getRevueUnsubscribers();
console.log(revueUnsubscribed);
})();
This creates a function that invokes itself, so it will now run when we run our script.
In return, it will console log the JSON object of people who unsubscribed on Revue.

Yes, that was more straightforward than I thought. We already have one call done.
Let's also add the call that will get the subscribed people.
const getRevueSubscribers = async () => {
const response = await fetch('https://www.getrevue.co/api/v2/subscribers', {
headers: {
Authorization: `Token ${process.env.REVUE_API_TOKEN}`,
'Content-Type': 'application/json',
},
method: 'GET',
}).then((res) => res.json());
return response;
};
And we can add this to our IIFE like this.
(async () => {
const revueUnsubscribed = await getRevueUnsubscribers();
console.log(revueUnsubscribed);
const revueSubscribed = await getRevueSubscribers();
console.log(revueSubscribed);
})();
Let's try it out and see what happens.

Nice, we can see both API calls return data.
For those paying attention, we created some repeating code. The Revue API calls look the same, so we can change things around a little bit.
const callRevueAPI = async (endpoint) => {
const response = await fetch(`https://www.getrevue.co/api/v2/${endpoint}`, {
headers: {
Authorization: `Token ${process.env.REVUE_API_TOKEN}`,
'Content-Type': 'application/json',
},
method: 'GET',
}).then((res) => res.json());
return response;
};
(async () => {
const revueUnsubscribed = await callRevueAPI('subscribers/unsubscribed');
console.log(revueUnsubscribed);
const revueSubscribed = await callRevueAPI('subscribers');
console.log(revueSubscribed);
})();
The code still does the same thing, but now we only leverage one uniform function.
It only limits to GET requests, but for now, that's precisely what we need.
This article taught us how to call the Revue API from NodeJS.
If you want to follow along by coding this project yourself, I've uploaded this version to GitHub.
We'll call the Sendy API in the following article, so keep an eye out.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter