Receive Daily News Emails in Node.js with Twilio SendGrid

February 17, 2022
Written by
Vanshika Rana
Contributor
Opinions expressed by Twilio contributors are their own
Reviewed by

Daily News Updates Node.js Sendgrid Header

Introduction

There is so much going on around the globe every day, sometimes, it’s hard to keep up with it all. As life gets busier and busier, you are left with very little in the daytime to read the news. What if you could have the latest news sent to your inbox every day?

In this article, you will learn how to automate and receive daily news updates in Node.js using Twilio SendGrid and News API .

Prerequisites

Before proceeding, you will need the following:

Configure the Environment

Before you begin writing code, you must first prepare the environment to meet the needs of your project. This ensures that your code executes without issues. So, let's get started!

Set up a Node.js Project

To begin, you will initialize your Node.js project named daily-news-emails which is where you will do all of your project execution. Open your terminal or command prompt, navigate to your preferred directory and run the following commands to create the project:

mkdir daily-news-emails
cd daily-news-emails
npm init  

After you've created your project, open it in your editor.

.env File

After you open your project in the editor, you will need to create a .env file. This file will be used to store all your API Keys safely and in one place. It is important to hide your API Keys so that they do not get misused.

Place the following in the .env file

NEWS_API_KEY=XXXXXXXXXXXXXX
SENDGRID_API_KEY=XXXXXXXXXXXX

The XXXXXXXXXXXXXX placeholders will be replaced with their respective API keys which you will obtain in the next section. In order to use these values inside your application, you’ll need to install the dotenv package. To install the package, head back to your terminal and execute the following command:

npm install dotenv

Get Started with SendGrid

The next step is to generate the SendGrid API key. Log on to SendGrid and you will be brought to the SendGrid dashboard. On the left sidebar, you'll find many categories, where you will click on the Email API dropdown menu and select Integration Guide.

Sidebar on SendGrid dashboard

From there, you’ll be directed to the following page and will be prompted to choose one of the options. You need to choose Web API to integrate with your application. On the next page select Node.js as the language and proceed.

Overview page of SendGrid Setup Guide

After that, you’ll be brought to the page depicted below. Give a name to your API Key and click the Create Key button to generate your API Key.

Integrate page of SendGrid Setup Guide

Head back to your .env file and replace the existing XXXXXXXXX placeholders with the newly generated API key:

SENDGRID_API_KEY=XXXXXXXXX

News API

Go to the News API website. You’ll be directed to this page:

Front page of the News API website

Log in if you already have an account, or click Get API Key, fill out the form, and submit.

You will see a field with the label API Key. Copy the key given and replace it with the existing placeholderand save the file:

NEWS_API_KEY=XXXXXXXXX

Install Dependencies

Now that you have all of the necessary APIs for your project, you can begin utilizing them; however, you must first install a few dependencies.

Normally, when dealing with APIs, you must make external HTTP calls, but thankfully, the SendGrid API and the News API include wrappers for Node.js applications. This allows us to directly use the API functions without making any direct calls.

So, fire up the terminal and execute the following command:

npm install @sendgrid/mail newsapi node-schedule

Finally, because your program must send emails on a daily basis, your node script must run constantly. To do this, you must install the forever package. Forever is an npm package that is used to keep the node server alive and execute Node.js scripts in the background as a daemon indefinitely.

You'll also need to install the node-schedule package to make the most of the Forever package. Node Schedule is a Node.js task scheduler with a lot of flexibility. Node Schedule, unlike other schedulers, is designed for time-based scheduling rather than interval-based scheduling.

Run the following command in your terminal:

npm install forever -g

Build the Application

After successfully configuring your environment, it's time to get to the exciting part: coding!

In your daily-news-emails folder, create an index.js file.

The first step is to import the .env file and API methods into your index.js file. The code for the same is explained below.

require('dotenv').config(); // Load .env file
const schedule = require('node-schedule'); // To schedule jobs using forever package
const NewsAPI = require('newsapi'); // Load News API functions
const newsapi = new NewsAPI(process.env.NEWS_API_KEY); // To access News API 
const sgMail = require('@sendgrid/mail'); // Load Sendgrid API functions 
sgMail.setApiKey(process.env.SENDGRID_API_KEY); // To access SendGrid API

Get the Daily News

After successfully importing all of the necessary files, you will write an async function:  getDailyNews(). This function will retrieve all of the top headlines from the News API between yesterday and today.

Copy the function below in the index.js file:

const getDailyNews = async () => {
  // Calculate Today's date and Yesterday's date
  const today = new Date();
  const yesterday = new Date(today.getTime() - (24 * 60 * 60 * 1000));
  const yesterdayDate = yesterday.toISOString().split('T')[0];
  const todayDate = today.toISOString().split('T')[0];
 
  // Get Top Headlines from News API
  const response = await newsapi.v2.topHeadlines({
    language: 'en', // language to display the news in 
    country: 'us', // country to display the news from
    pageSize: 100, 
    page: 1,
    from: yesterdayDate,
    to: todayDate,
  });

  return response; // response will be returned in a JSON format
};

If you'd like to understand the request parameters in detail, click here to take a look at News API's documentation on it.

Generate HTML Template for Email

A JSON response sent as an email is really unpleasant to read. So, to make your email more understandable, you will construct a basic HTML Template for the JSON response data.

Append the following in your index.js file:

const generateTemplate = (news) => {
  const newsArticles = news.articles; // An array of articles
  let htmlTemplate = '<h1>Daily News</h1>'; // Will be initialized with the heading
  newsArticles.forEach((article, index) => { // Loop to iterate through the array of articles
    htmlTemplate += `<h2>${index + 1}</h2>`; // A the index of the article
    htmlTemplate += `<h3>${article.title}</h3>`; // A the title of the article
    htmlTemplate += `<p>${article.description}</p>`; // Then with the description of the article
    htmlTemplate += `<a href="${article.url}">Read More</a>`; // At last, it will be appended with the url of the source.
  }); 
  return htmlTemplate; // Formatted HTML will be returned
};

Use SendGrid to Send the Email

Now you'll write the message that will be delivered by email. This message includes the recipient's address, the sender's name and address, the subject, and the body. The body will be the htmlTemplate that you made in the last section.

Add the following to the the index.js file:

const sendEmail = async (htmlTemplate) => {
  const msg = {
    to: 'test@example.com', // Email address of the recipient
    from: {
      name: 'Your Name', // Name of the sender
      email:'test@example.com' // Email address of the sender
    },
    subject: 'Daily News', // Subject of the email
    html: htmlTemplate, // htmlTemplate will be passed as the html of the email
  };
  await sgMail.send(msg); // SendGrid API will be used to send the email
}

Once copied into your file, don’t forget to replace the test@example.com placeholders with the actual emails you will use to test out the project.

When using SendGrid, you’ll need to verify your Sender Identity by completing Single Sender Verification or Domain Authentication. For a step-by-step tutorial on this check out: How to set up domain authentication for Twilio SendGrid

Schedule the Main Function to Send Updates

You’ll need to create a main function that calls your previously written functions in a proper sequence:

const main = async () => {
  const news = await getDailyNews();
  const htmlTemplate = generateTemplate(news);
  await sendEmail(htmlTemplate);
  console.log('Daily News Sent!');
};

Now that all of the functions are pieced together, let’s set up the program to send out an email update every minute. To do that add the following line and save the file:

schedule.scheduleJob('* * * * *', main);

The function above uses a cron expression as a parameter. The cron expression used is * * * * * and instructs the function to have it run every minute. To understand how cron expressions work, click here.

Test the Application

Now that you are done with the coding part let’s put it to the test. Open your terminal and run the following command:

node index.js

Wait for a minute and you should see a terminal log that says “Daily News Sent!”. Once you see the terminal log, check the recipient email address you used and you should see your Daily News Email!

To stop the execution, use ctrl+c on your terminal.

Schedule to get Daily Updates

Now that you have successfully tested your application, let’s modify it to send daily updates.

 To do that update the following changes:

schedule.scheduleJob('0 0 * * *', main);

This will send you emails at 00:00 every day.

To run the node continuously, open your terminal and execute:

forever start index.js

This will keep your node server running indefinitely unless you stop it.

To stop the server, run:

forever stop

Let’s Summarize!

Great! You made it; now begin your day with a good cup of coffee/tea served with the latest news from around the world. In this tutorial, you learned:

  • How to build a program that will send regular news updates directly to your inbox.
  • How to handle various APIs.
  • How to retrieve data from News API.
  • How to schedule a job in node.js.

If you want to keep exploring the code, try to either modify the HTML template or schedule updates to be sent to you multiple times a day so you can be constantly in the know!