Estimated time to complete: 10 minutes

Overview

Welcome to Magma! 🌋

This template will help you create an agent that can send and manage emails through your Gmail account.

It involves:

  • Creating a Google Cloud Project
  • Enabling the Gmail API
  • Creating OAuth credentials
  • Using the credentials to authenticate your agent with Gmail

Getting Started

1

Install the CLI

If you already have the CLI installed, you can skip this step.

npm install -g @pompeii-labs/cli
2

Login to Magma

magma login
3

Create Gmail Agent

magma init -t gmail <agent-name>

cd <agent-name>
4

After the agent is created, you should notice Agent ID: XXXXX in the output. Copy this agent id, we will need it in the next section!

Google Cloud Setup

In order to create an agent that can interact with Gmail, we need to create a Google Cloud Project, enable the Gmail API, and create OAuth credentials.

1

Create Google Cloud Project

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
2

Enable Gmail API

In the search bar, enter “Gmail API” and click on the “Gmail API” card.

3

Enable Gmail API

Click on the “Enable” button.

4

Configure OAuth Consent

  1. Go to “APIs & Services” > “OAuth consent screen”
  2. For “User type”, select “Internal” if you want people in your organization to use the agent. Otherwise, select “External” (you will have to add your email to test users later in order to use the agent)
  3. Fill in the required app information (this can be changed later, don’t worry about it for now)
5

  1. On the Scopes screen, click “Add or Remove Scopes” in the middle of the screen.
  2. Under “Manually Add Scopes”, add the following scope: https://mail.google.com/
  3. Click “Add to Table”
  4. Click “Update”
  5. At the bottom of the screen, click “Save and Continue”

Make sure to save and continue, or your scopes will not be saved!

6

Create OAuth Credentials

  1. Go to “APIs & Services” > “Credentials”
  2. At the top of the screen, click “Create Credentials” > “OAuth Client ID”
  3. Select “Web application” for your application type
  4. For “Authorized redirect URIs”, add the following two values
    http://localhost:3000/v1/agents/AGENT_ID/hooks/google-oauth  # For local testing
    
    https://api.magmadeploy.com/v1/agents/AGENT_ID/hooks/google-oauth  # For production
    

Remember when I said you’d need that agent id? Replace AGENT_ID in the redirect URIs with your actual agent id!

7

Copy Client ID and Secret

Copy the Client ID and Client Secret values. We will need them in the next step.

8

Configure Environment

Add these variables to your .env file:

GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_REDIRECT_URI=http://localhost:3000/v1/agents/AGENT_ID/hooks/google-oauth

Testing and Deployment

1

Local Testing

Run the agent locally to test the OAuth flow. When you ask your agent to authenticate you with Google, it should print out a google oauth url.

magma run

After you authenticate, you should see a credentials.json file in the root of your project. This means it’s working!

2

Deploy

Once you have verified your agent is working locally, you can deploy it to magma and start interacting with your Gmail!

Before you deploy, make sure to update the GOOGLE_REDIRECT_URI in your .env file to the production redirect uri (https://api.magmadeploy.com instead of localhost).

magma deploy

Troubleshooting

Security Note: Never commit credentials.json to version control. Add it to your .gitignore file!