Get started with gmail-js CDN

MIT licensed

Gmail-js: Read-only Gmail access for web apps via lightweight library.

Tags:
  • gmail
  • gmail api
  • gmail.js
  • gmail extension
  • gmail chrome extension

Stable version

Copied!

How to start using gmail-js CDN


// Include the Gmail API script from the CDN
const gmailScript = document.createElement('script');
gmailScript.src = 'https://cdn.cdnhub.io/gmail-js/1.1.14/gmail.min.js';
document.head.appendChild(gmailScript);

// Wait for the script to load before initializing the library
gmailScript.onload = () => {
  // Initialize the Gmail API client
  const gmail = new GmailApp();

  // Set up the authorization scopes and callback function
  gmail.setUser('me'); // Replace 'me' with the user email address
  gmail.useDefaultClient();
  gmail.authorize((auth) => {
    if (auth.error) {
      console.log('Error:', auth.error);
      return;
    }

    // Perform actions with the authorized Gmail API client
    const thread = gmail.users.threads.list('me').then((threads) => {
      const firstThread = threads.threads[0];
      return gmail.users.messages.list(firstThread.id, {q: 'is:inbox'});
    });

    thread.then((messages) => {
      const firstMessage = messages.messages[0];
      console.log('First message subject:', firstMessage.snippet);
    });
  });
};
Copied!
Copied!
Copied!

All versions