# Google ADK

Build a Gmail-powered AI agent using Google ADK that authenticates users via OAuth 2.0 and fetches their last 5 unread emails using Scalekit Agent Auth. Visit the [Google ADK quickstart guide](https://google.github.io/adk-docs/get-started/quickstart/) to set up Google ADK before starting.

[Full code on GitHub](https://github.com/scalekit-inc/google-adk-agent-example)

## Prerequisites

Before you start, ensure you have:

- **Scalekit account and API credentials** — Get your Client ID and Client Secret from the [Scalekit dashboard](https://app.scalekit.com)
- **Google API Key** — Obtain from [Google AI Studio](https://aistudio.google.com)
- **Gmail account** — For testing the OAuth authentication flow

1. ### Set up your environment

   Install the Scalekit Python SDK and Google ADK, then initialize the client. Get your credentials from **Developers → Settings → API Credentials** in the [dashboard](https://app.scalekit.com).

   <p>
     ```sh showLineNumbers=false frame="none"
     pip install scalekit-sdk-python google-adk
     ```
   </p>
   <p>
     ```python showLineNumbers=false frame="none"
     import scalekit.client
     import os

     scalekit_client = scalekit.client.ScalekitClient(
         client_id=os.getenv("SCALEKIT_CLIENT_ID"),
         client_secret=os.getenv("SCALEKIT_CLIENT_SECRET"),
         env_url=os.getenv("SCALEKIT_ENV_URL"),
     )
     actions = scalekit_client.actions

     ```
   </p>

2. ### Create a connected account

   Authorize a user to access their Gmail account by creating a connected account. This represents the user's connection to their Gmail account:

   ```python
   # Create a connected account for user if it doesn't exist already
   response = actions.get_or_create_connected_account(
               connection_name="GMAIL",
               identifier="user_123"
           )
   connected_account = response.connected_account

   print(f'Connected account created: {connected_account.id}')
   ```

3. ### Authenticate the user

   For Scalekit to execute tools on behalf of the user, the user must grant authorization to access their Gmail account. Scalekit automatically handles the entire OAuth workflow, including token refresh.

   ```python
   # Check if the user needs to authorize their Gmail connection
   # This handles both new users and cases where the access token has expired
   if connected_account.status != "ACTIVE":
       # Generate an authorization link for the user to complete OAuth flow
       link_response = actions.get_authorization_link(
           connection_name="GMAIL",
           identifier="user_123"
       )

       # Display the authorization link to the user
       print(f"🔗 Authorize Gmail access: {link_response.link}")
       input("⎆ Press Enter after completing authorization...")

   # In production, redirect users to the authorization URL instead of using input()
   # The user will be redirected back to your app after successful authorization
   ```

4. ### Build a Google ADK agent

   Build a simple agent that fetches the last 5 unread emails from the user's inbox and generates a summary.

   ```python
   from google.adk.agents import Agent

   # Get Gmail tools from Scalekit in Google ADK format
   gmail_tools = actions.google.get_tools(
       providers=["GMAIL"],
       identifier="user_123",
       page_size=100
   )

   # Create a Google ADK agent with Gmail tools
   gmail_agent = Agent(
       name="gmail_assistant",
       model="gemini-2.5-flash",
       description="Gmail assistant that can read and manage emails",
       instruction=(
           "You are a helpful Gmail assistant. You can read, send, and organize emails. "
           "When asked to fetch emails, focus on unread messages and provide clear summaries. "
           "Always be helpful and professional."
       ),
       tools=gmail_tools
   )

   # Use the agent to fetch and summarize unread emails
   response = gmail_agent.process_request(
       "fetch my last 5 unread emails and summarize them"
   )
   print(response)
   ```