Skip to content

Google Integration

The @cf-agents/google package provides a powerful set of tools to let your AI agent interact with Google Workspace.

  • Gmail: Read emails, search inboxes, and send emails.
  • Calendar: Manage events, check availability, and schedule meetings.
  • Sheets: Read and write data to spreadsheets.
  • Authentication: Built-in OAuth 2.0 flow helpers for Cloudflare Workers.
Terminal window
npm install @cf-agents/google
# or
pnpm add @cf-agents/google
  1. Create a project in the Google Cloud Console.
  2. Enable the Gmail API, Google Calendar API, and Google Sheets API.
  3. Configure the OAuth Consent Screen (add test users if in testing mode).
  4. Create OAuth Client ID credentials (Web Application).
    • Authorized Redirect URIs: https://your-worker.workers.dev/auth/google/callback (or http://localhost:8787/auth/google/callback for local dev).

For local development, create a .dev.vars file in your root directory:

GOOGLE_CLIENT_ID="your-client-id"
GOOGLE_CLIENT_SECRET="your-client-secret"
GOOGLE_REDIRECT_URI="https://your-worker.workers.dev/auth/google/callback"

For production, use the Wrangler CLI to set your secrets securely:

Terminal window
npx wrangler secret put GOOGLE_CLIENT_ID
npx wrangler secret put GOOGLE_CLIENT_SECRET

[!CAUTION] Never store sensitive credentials directly in your wrangler.jsonc file. Use .dev.vars for local dev and Wrangler secrets for production.

In your agent’s onChatMessage handler:

import { createGoogleTools } from "@cf-agents/google";
const googleTools = createGoogleTools({
getToken: async () => {
// Return your stored valid access token here
// See the 'Authentication' section below for full implementation
}
});

Since Google requires OAuth 2.0, you need to handle the token exchange. This package provides helpers to make this easy in a Cloudflare Worker.

See the Google Package README for the full code example on implementing handleGoogleAuth and handleGoogleCallback.