How to Use the Gmail API in Node.js – A Step-by-Step Tutorial

Gmail has become the go-to emailing tool for almost everyone on the internet. But your Gmail account can do much more than just send and receive emails. People can even use it as verification for opening accounts on other platforms. 

The utility of your Gmail might make you wonder what other ways you can use your Gmail account. Thanks to Gmail APIs, which even allow you to use your Gmail account without actually opening it. You can create and update email drafts, manage threads, send emails, and even look for specific messages. 

Using Gmail API in one of the most popular web development frameworks – Node.js, has many such benefits for you. But before you can benefit from these perks, you must learn to use Gmail API in Node.js. So, here is the ultimate guide to help you use the Gmail API in Node.js.    

Prerequisites For Using Gmail API with Node.js

Before you can start using Gmail API in Node.js, your system must be equipped with several things. So, let’s check out some of the prerequisites you need for using Gmail API in Node.js. 

  • Install Node.js and npm in your system. 
  • Make sure you have the Google Cloud Platform with enabled API. 
  • Ready your credentials for authorization for a given desktop application. 
  • The final thing you need here is a Google Account with enabled Gmail.   

Steps For Using Gmail API With Node.js

Once you have sorted out all the prerequisites mentioned above, it is time to get started with the steps to use Gmail API with Node.js. So, be mindful of these steps mentioned below to help you send Emails using Gmail API with Node.js. 


Step 1: Start By Creating a Google Cloud Project

The first thing you need to do is create a Google Cloud Project. So, go to and get started with creating a new cloud project. This is where you need to give your project a nice name. You must also change the project ID and click the “Create” button. Now, you will have a brand new Google Cloud Console project ready for use. 


Step 2: Move Forward With Enabling Google APIs

Once you have created your Google Cloud Project, you must enable all necessary Google APIs. For this, you can start by choosing the “API & Services” function available on the left menu. Click on the “Enable APIs and Services” tab to enable the Gmail API. The API will allow you to view and manage all data in the Gmail mailbox. It means managing your messages, labels, and email threads.           


Step 3: Configuring The OAuth Consent Screen 

After enabling Google APIs, it is time to move on to the next step and configure the OAuth consent screen. The “OAuth Consent Screen” tab is under the “APIs and Services” section. The consent screen will give you two choices in User Type, and you need to choose the Internal option. 

Choosing the Internal option will eliminate the need to go through the complex process of verifying OAuth, which can take weeks. Instead, you can get access to your Gmail API without any hassle. In the end, click on “Save & Continue” to complete this step. 

Step 4: Creating OAuth Client ID

Before leaving the OAuth Consent Screen, there is another thing you need to do. Click on the “Credentials” tab and create a client ID for OAuth. You will now be directed to the “Credentials” tab, where you can create your credentials. Click on the “Create Credentials” tab, which will open a drop-down menu where you must select the OAuth client ID option. 

The fields will ask you to choose an application type, and you can choose a web application. Name the application whatever you want but keeping it Web client 1 will also do the job. Add a redirect URL, which will be available as you scroll down. In this field, you can enter this You should use this link because your URL should not have any slash (/) at the end of the URL. 

Finally, click on ‘Create’ to create the OAuth client. You will get an OAuth client for the project, some client secret keys, and a client ID.      

Step 5: Getting The Code of Authorization 

When the application redirects to any Google URL with the necessary scopes and client ID for OAuth, you can consider the sequence started. Google will manage the user’s authentication and give you a code for authorization accordingly. The application can exchange the code for a refresh token and an access token. Provide necessary commands in the command prompt, and the screen will direct you to the Google authorization page.  


Step 6: Time for Creating Authorized OAuth2 Client

The best thing about this step is the browser is programmed to generate an authorization code automatically. You can paste the same on token.js and generate a refresh and access token. Remember, the validity of the access code lasts for just an hour, and the application can use the refresh code to get a new access code upon expiry. After this, you need to run the necessary command to generate both the tokens, adding a new token.js file to the project’s directory. 


Step 7: Work On Your Email Sender Library  

Most people use the Nodemailer library for this purpose. It will allow you to generate the RFC822 formatted email messages, which you can stream to SMTP. You also have the option to do this manually by creating a Mime message, so choose the approach that best suits your requirements.    


Step 8: Send Out The Email Using Gmail API

The final step is to start creating objects of mailOptions. Make sure the object can define the fields of the message. These fields include recipients, sender’s name, HTML body, attachments, and the subject. Many even recommend adding headers to the message to help add message tracking information. 

You can further attach any localized file system to attach the file directly to the Gmail message. But if this does not work for you, there is also the option for pulling an attachment from the remote URL. You are all set to send personalized emails via Gmail API in Node.js.        


Bottom Line 

As you can see, using Gmail API in Node.js is not as challenging as you might have imagined. All you need to do is follow these eight simple steps mentioned above, and you will be all set to reap the benefits of Gmail API in Node.js. It will allow you not just to send and receive emails, but you can even make necessary customizations to suit your preferences. 


So, be mindful of this information and use it to your advantage. But in case this appears too complex, you can always connect to a Node js web development company to help you out with the intricacies of the entire process. No matter how you choose to do it, you can always be sure the results are rewarding and worth all the effort. So why adhere to complications when you can follow these steps and get the most out of your Gmail API in Node.js. 


BY: Harikrishna Kundariya, a marketer, developer, IoT, ChatBot & Blockchain savvy, designer, co-founder, Director of eSparkBiz Technologies, A Software Development Company. His 10+ experience enables him to provide digital solutions to new start-ups based on IoT and ChatBot.

About the Author Shamsher

Live Instructor-Led online digital marketing trainer, a consultant, and an affiliate marketer with over 8+ years of experience View Course Details if you want to grow your business online or want to learn digital marketing online from anywhere.

follow me on:

Leave a Comment: