Welcome to FullStack. We use cookies to enable better features on our website. Cookies help us tailor content to your interests and locations and provide other benefits on the site. For more information, please see our Cookies Policy and Privacy Policy.
The Gmail API grants secure access to messages, threads, and labels and allows developers to create a variety of different applications for mail indexing, label management, and message sending.
In this tutorial, you will learn how to create a simple app that can grant access to a Gmail account, filter the list of messages, and assign them a specific label. The code shown here is written in Node.js but you can easily adapt it to your preferred language.
Create a Project in Google API Console
First, create a project in the Google Developers Console. Visit https://console.developers.google.com/ and log in, then click on Select a project > NEW PROJECT.
Enable the Gmail API Library
Navigate to the Library, search for the Gmail API and enable it.
Configure OAuth Consent Screen
Before creating the credentials, configure the OAuth consent screen by clicking on the button CONFIGURE CONSENT SCREEN.
Select the User Type depending on the users who will be able to use the application.
Type the application name and click Save.
Create Credentials
In the left menu, click on Credentials > CREATE CREDENTIALS, and select OAuth client ID.
Select the application type and enter a name for the credentials.
You should be able to see the new credentials under the OAuth 2.0 Client IDs section. Click on the download icon to download the credentials file. Then rename it to credentials.json.
Set Up the QuickStart Example
Navigate to the Node.js Quickstart page, copy the quickstart example provided by Google, and save it in a file named index.js.
In the Terminal window, go to the Working Directory where you created the file index.js, and run the command yarn add googleapis@39 to install the Gmail API library.
Then run the command node . to execute the sample code. The first time you will be asked to authorize the application by visiting a URL like the example below. Open that URL in a browser and follow the steps. At the end, you will get a code that you have to paste in the terminal window right where it says “Enter the code from that page here.”
Authorize this app by visiting this url: https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&response_type=code&client_id=479559853488-050lms0ffusprdhh938s954q0s36kg4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3AoobEnter the code from that page here:
After the app is successfully authorized, a list of the current labels from the user’s mailbox is shown.
The authorize function in the quickstart example is in charge of validating the credentials and generating the token. You have to send the constant oAuth2Client as a parameter for the functions listed in the next sections.
Finally, run the application by using the command node . to authorize the app again and update the access permissions. You should be able to see the label list but now each label with its corresponding id.
Loop through the previous messages. Call the modifyLabels functions with the right parameters.
This call will remove the INBOX label and will add the PENDING label to all the messages. Label_11 is the id of the label PENDING that we created using the Gmail site.
For sake of simplicity, we created and retrieved the id of the PENDING label manually. However, the Gmail API provides the methods that will let you do that programmatically. We encourage you to check the API Reference by visiting Gmail API Documentation.
Let us know how you did!
Conclusion
Using techniques like what is listed above, we have had the opportunity to address our clients’ concerns and they love it! If you are interested in joining our team, please visit our Careers page.
--- At FullStack Labs, we are consistently asked for ways to speed up time-to-market and improve project maintainability. We pride ourselves on our ability to push the capabilities of these cutting-edge libraries. Interested in learning more about speeding up development time on your next form project, or improving an existing codebase with forms? Contact us.
Learn more
Frequently Asked Questions
What is the Gmail API, and why use it with Node.js?
The Gmail API lets developers securely access, organize, and manage Gmail messages, threads, and labels. Using it with Node.js is a common choice because Google provides an official library that makes it easier to integrate email functionality into applications without manually handling complex authentication or data parsing.
How do I enable the Gmail API for my project?
First, create a project in the Google Developers Console and enable the Gmail API from the Library section. Then, configure your OAuth consent screen and create the necessary credentials. After downloading your credentials file, you can authorize your app and connect it to your Gmail account for secure access.
What can I do with the Gmail API?
The Gmail API allows you to:
View and filter messages based on specific queries
Retrieve and manage labels
Organize your mailbox by adding or removing labels
Automate common email workflows, like categorizing or prioritizing messages
It gives you more control and flexibility than standard Gmail integrations.
How do labels work with the Gmail API?
Labels are a way to organize and categorize emails within your mailbox. With the Gmail API, you can retrieve a list of existing labels, create new ones, and assign or remove them from messages. This helps developers build apps that automatically sort, track, or prioritize emails based on custom rules.
What permissions are needed to manage Gmail messages and labels?
When connecting your app to Gmail, you choose an access scope. A read-only scope allows you to view messages and labels but not change them. To manage labels or update messages, you need to enable modify permissions during setup. These permissions ensure secure and controlled access to your account.
AI is changing software development.
The Engineer's AI-Enabled Development Handbook is your guide to incorporating AI into development processes for smoother, faster, and smarter development.
Enjoyed the article? Get new content delivered to your inbox.
Subscribe below and stay updated with the latest developer guides and industry insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.