> ## Documentation Index
> Fetch the complete documentation index at: https://docs.magmadeploy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gmail Integration Agent

> Create an AI agent that can send and manage emails through Gmail

<Check icon="clock">Estimated time to complete: 10 minutes</Check>

<img className="block rounded-xl" src="https://mintcdn.com/pompeiilabsinc/INRokfUt_EitWThD/assets/templates/gmail/gmail-header.png?fit=max&auto=format&n=INRokfUt_EitWThD&q=85&s=34138c6c30adf73ceb62e16a32d74088" alt="Gmail Agent Demo" width="2400" height="1350" data-path="assets/templates/gmail/gmail-header.png" />

## 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

<Steps>
  <Step title="Install the CLI">
    If you already have the CLI installed, you can skip this step.

    ```bash theme={null}
    npm install -g @pompeii-labs/cli
    ```
  </Step>

  <Step title="Login to Magma">
    ```bash theme={null}
    magma login
    ```
  </Step>

  <Step title="Create Gmail Agent">
    ```bash theme={null}
    magma init -t gmail <agent-name>

    cd <agent-name>
    ```
  </Step>

  <Step>
    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!
  </Step>
</Steps>

## 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.

<Steps>
  <Step title="Create Google Cloud Project">
    1. Go to [Google Cloud Console](https://console.cloud.google.com)
    2. Create a new project or select an existing one

    <img className="block rounded-lg border border-zinc-200 dark:border-zinc-700 mt-4" src="https://mintcdn.com/pompeiilabsinc/INRokfUt_EitWThD/assets/templates/gmail/create-project-step.png?fit=max&auto=format&n=INRokfUt_EitWThD&q=85&s=3e875f396d02ddb8a77d043933706858" alt="Create Google Cloud Project" width="1571" height="450" data-path="assets/templates/gmail/create-project-step.png" />
  </Step>

  <Step title="Enable Gmail API">
    In the search bar, enter "Gmail API" and click on the "Gmail API" card.

    <img className="block rounded-lg border border-zinc-200 dark:border-zinc-700 mt-4" src="https://mintcdn.com/pompeiilabsinc/INRokfUt_EitWThD/assets/templates/gmail/gmail-api-searchbar.png?fit=max&auto=format&n=INRokfUt_EitWThD&q=85&s=edd35bd11353935ae8af6cf76e7e95db" alt="Enable Gmail API" width="725" height="133" data-path="assets/templates/gmail/gmail-api-searchbar.png" />
  </Step>

  <Step title="Enable Gmail API">
    Click on the "Enable" button.

    <img className="block rounded-lg border border-zinc-200 dark:border-zinc-700 mt-4" src="https://mintcdn.com/pompeiilabsinc/INRokfUt_EitWThD/assets/templates/gmail/gmail-api-enable.png?fit=max&auto=format&n=INRokfUt_EitWThD&q=85&s=4d1817288e85a1e1cf2b722e776b2320" alt="Enable Gmail API" width="442" height="231" data-path="assets/templates/gmail/gmail-api-enable.png" />
  </Step>

  <Step title="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)

    <img className="block rounded-lg border border-zinc-200 dark:border-zinc-700 mt-4" src="https://mintcdn.com/pompeiilabsinc/INRokfUt_EitWThD/assets/templates/gmail/apis-services-credentials.png?fit=max&auto=format&n=INRokfUt_EitWThD&q=85&s=323620472fb59bfee548ce7cc51307c6" alt="OAuth Consent Screen" width="459" height="398" data-path="assets/templates/gmail/apis-services-credentials.png" />
  </Step>

  <Step>
    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"

    <Warning>
      Make sure to save and continue, or your scopes will not be saved!
    </Warning>
  </Step>

  <Step title="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
       ```

    <Info>
      Remember when I said you'd need that agent id? Replace `AGENT_ID` in the redirect URIs with your actual agent id!
    </Info>
  </Step>

  <Step title="Copy Client ID and Secret">
    Copy the `Client ID` and `Client Secret` values. We will need them in the next step.

    <img className="block rounded-lg border border-zinc-200 dark:border-zinc-700 mt-4" src="https://mintcdn.com/pompeiilabsinc/INRokfUt_EitWThD/assets/templates/gmail/copy-id-and-secret.png?fit=max&auto=format&n=INRokfUt_EitWThD&q=85&s=edc223cfbae43a119d77a9923cb372a4" alt="Copy Client ID and Secret" width="523" height="580" data-path="assets/templates/gmail/copy-id-and-secret.png" />
  </Step>

  <Step title="Configure Environment">
    Add these variables to your `.env` file:

    ```bash theme={null}
    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
    ```
  </Step>
</Steps>

## Testing and Deployment

<Steps>
  <Step title="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.

    ```bash theme={null}
    magma run
    ```

    After you authenticate, you should see a `credentials.json` file in the root of your project. This means it's working!
  </Step>

  <Step title="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](https://api.magmadeploy.com) instead of localhost).

    ```bash theme={null}
    magma deploy
    ```
  </Step>
</Steps>

## Troubleshooting

<AccordionGroup>
  <Accordion title="Authentication Issues">
    If authentication fails:

    * Verify credentials in `.env` file
    * Ensure redirect URI matches exactly in Google Cloud Console
    * Confirm Gmail API is enabled
  </Accordion>

  <Accordion title="Email Sending Issues">
    If emails aren't sending:

    * Verify OAuth tokens are valid
    * Check if `credentials.json` exists
    * Ensure Gmail API scope is authorized
  </Accordion>

  <Accordion title="Common Issues">
    * Invalid redirect URI: Double-check exact matches
    * Missing scopes: Verify `https://mail.google.com/` is included
    * Expired tokens: May need re-authentication
  </Accordion>
</AccordionGroup>

<Warning>
  **Security Note**: Never commit `credentials.json` to version control. Add it to your `.gitignore` file!
</Warning>

<CardGroup cols={2}>
  <Card title="Need Help?" icon="question" href="mailto:founders@pompeiilabs.com">
    Contact our support team for assistance
  </Card>

  <Card title="Join Community" icon="slack" href="https://magmacommunity.slack.com">
    Connect with other Magma developers
  </Card>
</CardGroup>
