Google Integration
The @cf-agents/google package provides a powerful set of tools to let your AI agent interact with Google Workspace.
Features
Section titled “Features”- 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.
Installation
Section titled “Installation”npm install @cf-agents/google# orpnpm add @cf-agents/googleConfiguration
Section titled “Configuration”Google Cloud Console
Section titled “Google Cloud Console”- Create a project in the Google Cloud Console.
- Enable the Gmail API, Google Calendar API, and Google Sheets API.
- Configure the OAuth Consent Screen (add test users if in testing mode).
- Create OAuth Client ID credentials (Web Application).
- Authorized Redirect URIs:
https://your-worker.workers.dev/auth/google/callback(orhttp://localhost:8787/auth/google/callbackfor local dev).
- Authorized Redirect URIs:
Environment Variables
Section titled “Environment Variables”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:
npx wrangler secret put GOOGLE_CLIENT_IDnpx wrangler secret put GOOGLE_CLIENT_SECRET[!CAUTION] Never store sensitive credentials directly in your
wrangler.jsoncfile. Use.dev.varsfor local dev and Wrangler secrets for production.
1. Initialize Tools
Section titled “1. Initialize Tools”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 }});2. Authentication Flow
Section titled “2. Authentication Flow”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.