Sunday, June 26, 2022
HomeWordPress DevelopmentRevue - Sendy sync: venture setup + Revue calls

Revue – Sendy sync: venture setup + Revue calls


Now that we have now a very good understanding of all of the API calls we have to make, we are able to begin establishing the venture.

I will be constructing this venture as a Node venture just because it is the lowest overhead and straightforward to host someplace.

The aim for right this moment is to have a primary Node venture that we are able to run. On working the code, it ought to record all unsubscribed individuals from Revue and all of the subscribers.



Creating the venture

Let’s get began.

Create a brand new node venture.

# Create folder
mkdir revue-sendy-sync

# Navigate into the folder
cd revue-sendy-sync

# Init new node venture
npm init
Enter fullscreen mode

Exit fullscreen mode

We must always now have our primary venture with a bundle.json file.

The very first thing I did was change it to module kind so we are able to use imports.

{
  "title": "revue-sendy-sync",
  "model": "1.0.0",
  "kind": "module",
  ...
}
Enter fullscreen mode

Exit fullscreen mode

The subsequent factor we need to do is add some packages that we’ll use. To date, we all know we want some surroundings variables and need to make some API calls.

The packages we are able to use for which might be dotenv and node-fetch.

npm i dotenv node-fetch
Enter fullscreen mode

Exit fullscreen mode

With these put in, we are able to outline a .env file. This file can be utilized to retailer your surroundings variables.

Whereas creating this, additionally ensure to exclude it by utilizing your .gitignore file. (You don’t need your secret to being dedicated to git!)

Contained in the .env file, add the next variable.

REVUE_API_TOKEN={YOUR_TOKEN}
Enter fullscreen mode

Exit fullscreen mode

Word: Haven’t got the token? Learn the article on retrieving all of the API keys.

Then the final file we want is an index.js file. This would be the brains of the operation.

Create the file, and begin by importing the packages we put in.

import dotenv from 'dotenv';
import fetch from 'node-fetch';

dotenv.config();

console.log(`I am working!`);
Enter fullscreen mode

Exit fullscreen mode

Now you can attempt to run this by executing node index.js. In return it ought to present you “I am working”.



Calling the Revue API from Node.js

Let’s begin with the primary piece of software program. We would like to have the ability to name the Revue API.

We are able to begin with the unsubscribe name.

To make issues scaleable, I created a customized perform for this objective.

const getRevueUnsubscribers = async () => {
  const response = await fetch(
    'https://www.getrevue.co/api/v2/subscribers/unsubscribed',
    {
      headers: {
        Authorization: `Token ${course of.env.REVUE_API_TOKEN}`,
        'Content material-Sort': 'software/json',
      },
      technique: 'GET',
    }
  ).then((res) => res.json());
  return response;
};
Enter fullscreen mode

Exit fullscreen mode

As you’ll be able to see, we use the node-fetch bundle to request the unsubscribed endpoint. We then go the Authorisation header the place we set the API token.

Word: This token is loaded from our .env file.

As soon as it returns, we convert the response to a sound JSON object and finally return that.

Then we have now to create a perform that runs as soon as our script will get known as.
That is known as an Instantly invoked perform expression (IIFE for brief).

(async () => {
  const revueUnsubscribed = await getRevueUnsubscribers();
  console.log(revueUnsubscribed);
})();
Enter fullscreen mode

Exit fullscreen mode

This creates a perform that invokes itself, so it would now run once we run our script.

In return, it would console log the JSON object of people that unsubscribed on Revue.

Calling Revue API via NodeJS response

Sure, that was extra simple than I assumed. We have already got one name completed.

Let’s additionally add the decision that can get the subscribed individuals.

const getRevueSubscribers = async () => {
  const response = await fetch('https://www.getrevue.co/api/v2/subscribers', {
    headers: {
      Authorization: `Token ${course of.env.REVUE_API_TOKEN}`,
      'Content material-Sort': 'software/json',
    },
    technique: 'GET',
  }).then((res) => res.json());
  return response;
};
Enter fullscreen mode

Exit fullscreen mode

And we are able to 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 strive it out and see what occurs.

Revue subscribed API call

Good, we are able to see each API calls return information.



Cleansing up

For these paying consideration, we created some repeating code. The Revue API calls look the identical, so we are able to change issues round slightly bit.

const callRevueAPI = async (endpoint) => {
  const response = await fetch(`https://www.getrevue.co/api/v2/${endpoint}`, {
    headers: {
      Authorization: `Token ${course of.env.REVUE_API_TOKEN}`,
      'Content material-Sort': 'software/json',
    },
    technique: '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 nonetheless does the identical factor, however now we solely leverage one uniform perform.

It solely limits to GET requests, however for now, that is exactly what we want.



Conclusion

This text taught us methods to name the Revue API from NodeJS.

If you wish to comply with alongside by coding this venture your self, I’ve uploaded this model to GitHub.

We’ll name the Sendy API within the following article, so maintain an eye fixed out.



Thanks for studying, and let’s join!

Thanks for studying my weblog. Be happy to subscribe to my e-mail e-newsletter and join on Fb or Twitter



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments