How to Intermediate · 4 min read

How to use AI in Google Workspace

Quick answer
Use AI in Google Workspace by integrating OpenAI or Anthropic APIs with Google Apps Script to automate tasks in Docs, Sheets, and Gmail. This enables AI-powered content generation, summarization, and workflow automation directly within Workspace apps.

PREREQUISITES

  • Google account with Google Workspace access
  • OpenAI or Anthropic API key (free tier available)
  • Basic knowledge of Google Apps Script
  • Python 3.8+ (optional for external scripts)
  • pip install openai>=1.0 (optional for Python scripts)

Setup Google Apps Script

Start by opening your Google Workspace app (Docs, Sheets, or Gmail), then open the Extensions > Apps Script editor. This environment lets you write JavaScript code to call AI APIs and automate tasks.

Set your API key as a script property or use Google Cloud Secret Manager for secure storage.

javascript
function setApiKey() {
  PropertiesService.getScriptProperties().setProperty('OPENAI_API_KEY', os.environ["OPENAI_API_KEY"]);
}

Step by step: Call OpenAI API from Apps Script

Use the UrlFetchApp service in Apps Script to call the OpenAI gpt-4o model for text generation. This example sends a prompt and inserts the AI response into the active Google Doc.

javascript
function generateTextWithOpenAI() {
  const apiKey = PropertiesService.getScriptProperties().getProperty('OPENAI_API_KEY');
  const url = 'https://api.openai.com/v1/chat/completions';
  const payload = {
    model: 'gpt-4o',
    messages: [{role: 'user', content: 'Write a summary of AI in Google Workspace.'}]
  };

  const options = {
    method: 'post',
    contentType: 'application/json',
    headers: { Authorization: 'Bearer ' + apiKey },
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };

  const response = UrlFetchApp.fetch(url, options);
  const json = JSON.parse(response.getContentText());
  const aiText = json.choices[0].message.content;

  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  body.appendParagraph(aiText);
}

Common variations

  • Use Anthropic API with similar HTTP calls for Claude models.
  • Run AI calls externally in Python and update Google Sheets via Sheets API.
  • Implement async calls and streaming in external apps for real-time interaction.
  • Use different models like gpt-4o-mini for cost-effective tasks.

Troubleshooting common issues

  • If you get 403 Forbidden, verify your API key and permissions.
  • For Timeout errors, increase script execution time or handle calls externally.
  • Check quota limits on your OpenAI or Anthropic account to avoid rate limiting.
  • Use Logger.log() in Apps Script to debug API responses.

Key Takeaways

  • Integrate AI APIs directly into Google Workspace using Apps Script for seamless automation.
  • Securely store API keys using script properties or Google Cloud Secret Manager.
  • Use gpt-4o for powerful text generation within Docs, Sheets, and Gmail.
  • External Python scripts can complement Apps Script for advanced AI workflows.
  • Monitor API usage and handle errors proactively to maintain smooth operation.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗