Back

Gmail API Essential Guide

Jul 19, 20246 minute read

What type of API does Gmail provide?

The Gmail API uses a RESTful architecture.

The Gmail API is a RESTful API that can be used to access Gmail mailboxes and send mail.

It is designed for various applications such as read-only mail extraction, indexing, backup, automated message sending, email account migration, and email organization.

The API provides a mechanism for organizing messages and threads using labels.

Does the Gmail API have webhooks?

Yes, the Gmail API has webhook-like functionality through its push notifications feature.

Push Notifications

The Gmail API uses Google Cloud Pub/Sub to deliver push notifications, allowing notification via webhooks and other methods on a single subscription endpoint.

Events You Can Subscribe To

  • Changes to the user's inbox
  • New messages received
  • Messages sent
  • Labels applied/removed
  • Threads updated

Setting Up Webhooks

  1. Create a Pub/Sub topic
  2. Set up a subscription to that topic
  3. Grant Gmail privileges to publish to your topic
  4. Configure your Gmail account to send notifications to the topic

Key Considerations

  • Complete the Cloud Pub/Sub prerequisites and set up a client before using Gmail webhooks.
  • Webhooks provide real-time notifications, eliminating the need for polling.
  • Use webhooks to trigger actions in your application when certain Gmail events occur.
  • Full email content is not sent in the webhook payload - make a separate API call to retrieve full message details.

Summary

The Gmail API push notifications feature provides webhook-like functionality for real-time updates about Gmail events, enabling developers to build responsive applications that react immediately to changes in a user's Gmail account.

Rate Limits and other limitations

Here are the key points about the API rate limits for the Gmail API:

Daily Quota Limit

  • The Gmail API has a daily quota limit of 1,000,000,000 quota units per day.

Per-User Rate Limit

  • There is a per-user rate limit of 250 quota units per user per second, calculated as a moving average (allows short bursts).

Quota Units

  • Different API operations consume different amounts of quota units. For example:
    • 'labels.list' has a quota of 1 unit
    • 'drafts.send' has a quota of 100 units

Email Sending Limits

  • The Gmail API enforces standard daily email sending limits:
    • 2,000 emails per day for regular accounts
    • 500 emails per day for trial accounts
    • These limits apply to both internal and external recipients

Bandwidth Limits

  • There are per-user upload and download bandwidth limits for the Gmail API, which are separate from but equal to IMAP limits.

Concurrent Request Limit

  • There is a per-user concurrent request limit shared by all Gmail API clients accessing a given user's account.

Handling Rate Limit Errors

  • If limits are exceeded, the API returns a HTTP 429 "Too Many Requests" error with a "User-rate limit exceeded" message.
  • For daily limits being exceeded, these errors may persist for several hours before requests are accepted again.

Best Practices

  1. Monitor your quota usage to avoid hitting limits unexpectedly.
  2. Implement exponential backoff for retrying requests when rate limits are hit.
  3. Optimize your API usage by batching requests where possible.
  4. Consider disabling IMAP/POP access if not needed, as it can contribute to hitting API limits.
  5. Review and remove unnecessary third-party apps with Gmail access to reduce concurrent requests.

By understanding and adhering to these limits, developers can ensure their applications interact smoothly with the Gmail API without disruptions due to quota or rate limit issues.

Latest API Version

Based on the search results, here are the key points regarding the most recent version of the Gmail API:

  1. The Gmail API was first publicly released on June 25, 2014.

  2. The latest version of the Gmail API is v1.

  3. For the Google.Apis.Gmail.v1 NuGet package (for .NET), the most recent version is 1.68.0.3427 as of the search results.

  4. The Gmail API documentation refers to it as "Gmail API v1".

  5. There have been ongoing updates and additions to the API functionality over time, but the core version number has remained at v1.

So in summary, the most recent version of the Gmail API is v1. While there have been updates and revisions to the API over time, it has maintained the v1 version number since its initial release. The specific implementation versions (like NuGet package versions) may have their own numbering schemes, but the core API version remains v1.

How to get a Gmail developer account and API Keys?

Create a Google Cloud Project

  1. Go to the Google Cloud Console (https://console.cloud.google.com/).

  2. Create a new project or select an existing one from the project dropdown menu.

  3. Switch to your preferred project from the dropdown after creation.

Enable the Gmail API

  1. In the Cloud Console, navigate to the "APIs & Services" > "Library" section.

  2. Search for "Gmail API" and click on it.

  3. Click the "Enable" button to enable the Gmail API for your project.

  1. In the Cloud Console, navigate to the "APIs & Services" > "OAuth Consent Screen" section.

  2. Select "External" and create.

  3. Enter App Name.

  4. Select user support email.

  5. Enter the developer contact email.

  6. Save and Continue.

Create OAuth 2.0 Credentials

  1. In the Cloud Console, navigate to the Credentials or "APIs & Services" > "Credentials" section.

  2. Click the "Create credentials" dropdown and select "OAuth client ID".

  3. Choose "Web application" as the application type.

  4. Enter a name for your OAuth client.

  5. Under "Authorized redirect URIs," add the URI where Google will redirect users after they are granted access. For development purposes, you can use https://developers.google.com/oauthplayground.

  6. Click "Create".

  7. Once created, Google will provide you with a client ID and client secret.

Generate an Access Token

  1. Go to https://developers.google.com/oauthplayground.

  2. Select the gear icon on the right.

  3. Check "Use your own OAuth credentials".

  4. Enter your OAuth client ID and secret ID, then close.

  5. From the left form, enter https://mail.google.com and authorize APIs.

  6. Follow the prompts carefully until you're back to the playground.

  7. Click on the "Exchange authorization code for tokens" button.

  8. Copy your refresh token.

Key Points to Consider

  • Ensure you're using a personal Google account, not a GSuite/Google Workspace account, when creating the developer account.

  • The account used to create the developer account will be the owner and have full access.

  • There's a one-time $25 fee to create a Google Developer account.

  • Be cautious with your client ID, client secret, and refresh token. Keep them secure and don't share them publicly.

Best Practices

  • Regularly review and update your OAuth consent screen information.

  • Use appropriate scopes when requesting access to user data.

  • Implement secure storage for tokens and credentials in your application.

  • Follow Google's API usage guidelines and quotas to ensure smooth operation of your integration.

What can you do with the Gmail API?

Here are the main data models you can interact with using the Gmail API, along with what is possible for each:

Messages

  • Read, create, modify, and delete email messages
  • Access message bodies, headers, and attachments
  • Search for messages using query parameters
  • Apply and remove labels on messages
  • Send messages and create drafts
  • Upload attachments

Threads

  • Group related messages into conversation threads
  • Read, modify, and delete entire threads
  • Apply and remove labels on threads
  • Search for threads using query parameters

Labels

  • Create, read, update, and delete labels
  • Apply labels to messages and threads
  • List all labels in a user's mailbox
  • Get label metadata like message counts

Drafts

  • Create, read, update, and delete draft messages
  • Send draft messages
  • Upload attachments to drafts

History

  • Get history of changes to the user's mailbox
  • Sync changes since a specific point in time
  • Track additions, deletions, and modifications

Settings

  • Access and modify user settings like signatures, filters, forwarding, etc.
  • Manage email aliases
  • Configure vacation auto-responders
  • Set up email delegation

Profile

  • Get basic user profile information
  • Access email address and name associated with account

Attachments

  • Upload and download email attachments
  • Get attachment metadata

Push Notifications

  • Set up push notifications for mailbox changes
  • Receive real-time updates when new messages arrive

Batch Operations

  • Execute multiple API calls in a single HTTP request
  • Improve performance for bulk operations