> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-opensw-1782332329-96d87c7.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Gmail toolkit integration

> Integrate with the Gmail toolkit using LangChain Python.

This will help you get started with the Gmail [toolkit](/oss/python/integrations/tools/google_gmail). This toolkit interacts with the Gmail API to read messages, draft and send messages, and more. For detailed documentation of all `GmailToolkit` features and configurations head to the [API reference](https://reference.langchain.com/python/langchain-google-community/gmail/toolkit/GmailToolkit).

## Setup

To use this toolkit, you will need to set up your credentials explained in the [Gmail API docs](https://developers.google.com/gmail/api/quickstart/python#authorize_credentials_for_a_desktop_application). Once you've downloaded the `credentials.json` file, you can start using the Gmail API.

### Installation

This toolkit lives in the `langchain-google-community` package. We'll need the `gmail` extra:

```python theme={null}
pip install -qU langchain-google-community\[gmail\]
```

To enable automated tracing of individual tools, set your [LangSmith](/langsmith/observability) API key:

```python theme={null}
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
```

## Instantiation

By default the toolkit reads the local `credentials.json` file. You can also manually provide a `Credentials` object.

```python theme={null}
from langchain_google_community import GmailToolkit

toolkit = GmailToolkit()
```

### Customizing authentication

Behind the scenes, a `googleapi` resource is created using the following methods.
you can manually build a `googleapi` resource for more auth control.

```python theme={null}
from langchain_google_community.gmail.utils import (
    build_resource_service,
    get_gmail_credentials,
)

# Can review scopes here https://developers.google.com/gmail/api/auth/scopes
# For instance, readonly scope is 'https://www.googleapis.com/auth/gmail.readonly'
credentials = get_gmail_credentials(
    token_file="token.json",
    scopes=["https://mail.google.com/"],
    client_secrets_file="credentials.json",
)
api_resource = build_resource_service(credentials=credentials)
toolkit = GmailToolkit(api_resource=api_resource)
```

## Tools

View available tools:

```python theme={null}
tools = toolkit.get_tools()
tools
```

```text theme={null}
[GmailCreateDraft(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailSendMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailSearch(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailGetMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailGetThread(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>)]
```

* [GmailCreateDraft](https://reference.langchain.com/python/langchain-google-community/gmail/create_draft/GmailCreateDraft)
* [GmailSendMessage](https://reference.langchain.com/python/langchain-google-community/gmail/send_message/GmailSendMessage)
* [GmailSearch](https://reference.langchain.com/python/langchain-google-community/gmail/search/GmailSearch)
* [GmailGetMessage](https://reference.langchain.com/python/langchain-google-community/gmail/get_message/GmailGetMessage)
* [GmailGetThread](https://reference.langchain.com/python/langchain-google-community/gmail/get_thread/GmailGetThread)

## Use within an agent

Below we show how to incorporate the toolkit into an [agent](/oss/python/langchain/agents).

We will need a LLM or chat model:

<ChatModelTabs customVarName="llm" />

```python theme={null}
# | output: false
# | echo: false

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-5.4-mini", temperature=0)
```

```python theme={null}
from langchain.agents import create_agent


agent_executor = create_agent(llm, tools)
```

```python theme={null}
example_query = "Draft an email to fake@fake.com thanking them for coffee."

stream = agent_executor.stream_events(
    {"messages": [("user", example_query)]},
    version="v3",
)
for snapshot in stream.values:
    snapshot["messages"][-1].pretty_print()
```

```text theme={null}
================================ Human Message =================================

Draft an email to fake@fake.com thanking them for coffee.
================================== Ai Message ==================================
Tool Calls:
  create_gmail_draft (call_slGkYKZKA6h3Mf1CraUBzs6M)
 Call ID: call_slGkYKZKA6h3Mf1CraUBzs6M
  Args:
    message: Dear Fake,

I wanted to take a moment to thank you for the coffee yesterday. It was a pleasure catching up with you. Let's do it again soon!

Best regards,
[Your Name]
    to: ['fake@fake.com']
    subject: Thank You for the Coffee
================================= Tool Message =================================
Name: create_gmail_draft

Draft created. Draft Id: r-7233782721440261513
================================== Ai Message ==================================

I have drafted an email to fake@fake.com thanking them for the coffee. You can review and send it from your email draft with the subject "Thank You for the Coffee".
```

***

## API reference

For detailed documentation of all `GmailToolkit` features and configurations head to the [API reference](https://reference.langchain.com/python/langchain-google-community/gmail/toolkit/GmailToolkit).

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/google_gmail.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
