DEV Community

Cover image for Revue - Sendy sync: project setup + Revue calls
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Revue - Sendy sync: project setup + Revue calls

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.

Creating the project

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
Enter fullscreen mode Exit fullscreen mode

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",
  ...
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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!`);
Enter fullscreen mode Exit fullscreen mode

You can now try to run this by executing node index.js. In return it should show you "I'm working".

Calling the Revue API from Node.js

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;
};
Enter fullscreen mode Exit fullscreen mode

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 .env file.

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);
})();
Enter fullscreen mode Exit fullscreen mode

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.

Calling Revue API via NodeJS response

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;
};
Enter fullscreen mode Exit fullscreen mode

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);
})();
Enter fullscreen mode Exit fullscreen mode

Let's try it out and see what happens.

Revue subscribed API call

Nice, we can see both API calls return data.

Cleaning up

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);
})();
Enter fullscreen mode Exit fullscreen mode

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.

Conclusion

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, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (0)