Generating API Keys for OpenAI, Google Gemini, and Anthropic for usage in Apps Script
This tutorial will guide you through generating API keys for major AI platforms and adding them as Script Properties in Google Apps Script. Setting up these keys properly allows you to integrate multiple AI services into your Apps Script projects.
Important Note
Only generate API keys for the services you plan to use. Unused API keys can pose unnecessary security risks and may incur charges if your account information is compromised.
Create API KeyOpenAI API Key
Log in to your OpenAI account
Open the Settings page by clicking on the gear icon in the top-right corner
Select "View API Keys"
Click "Create new secret key"
Copy your key immediately (it won't be shown again)
Store it securely (you'll need it later). Use a password manager or other secure storage.
Google Gemini API KeyLog in with your Google account
Click "Create API key"
Create a new project or use an existing one
Name your key and click "Create"
Copy your key immediately
Store it securely (you'll need it later). Use a password manager or other secure storage.
Anthropic API KeyLog in to your Anthropic account
Click "Create Key"
Name your key and set permissions
Copy your key immediately
Store it securely (you'll need it later). Use a password manager or other secure storage.
Adding API Keys as Script Properties in Apps Script
Log in with your Google account
Click "Create API key"
Create a new project or use an existing one
Name your key and click "Create"
Copy your key immediately
Store it securely (you'll need it later). Use a password manager or other secure storage.
Log in to your Anthropic account
Click "Create Key"
Name your key and set permissions
Copy your key immediately
Store it securely (you'll need it later). Use a password manager or other secure storage.
Adding API Keys as Script Properties in Apps Script
Using Script Properties is the recommended way to store information like API keys in Apps Script, rather than pasting them directly into your code.
Open your Google Apps Script project.
Click on "Project Settings" (the gear icon ⚙️) in the left sidebar menu.
Scroll down within the Project Settings until you find the "Script Properties" section.
Click "Add script property" (note: You may have to click "Edit script properties" first if you've previously added script properties)
Enter the property name: Use a clear, descriptive name that you will reference in your code. Conventionally, these are uppercase with underscores. Examples:
OPENAI_API_KEY
(for OpenAI models)GOOGLE_API_KEY
(for Google Gemini models)ANTHROPIC_API_KEY
(for Anthropic models)Paste your secret API key into the "Value" field next to the property name you just entered.
Repeat steps 4-6 for each different API key (e.g., one for OpenAI, one for Google) that you plan to use in your project.
Click "Save script properties" to save the new property (or properties).
In the screenshot below, I have configured API Keys for all three AI model providers. It is a best practice to only add API Keys for model providers you plan to use in your Apps Script project.

Accessing API Keys in Your Apps Script Code
Once you configure API Keys as script properties, you can access them in your Apps Script code using the PropertiesService
.
function callAIService() {
// Get API keys from Script Properties
const openAiKey = PropertiesService.getScriptProperties().getProperty('OPENAI_API_KEY');
const googleKey = PropertiesService.getScriptProperties().getProperty('GOOGLE_API_KEY');
const anthropicKey = PropertiesService.getScriptProperties().getProperty('ANTHROPIC_API_KEY');
// Use keys in your API calls
// ...
}
⚠️ Important Warnings and Security ConsiderationsAPI Key Security
The API keys required by these functions provide access to paid services. Keep your API keys strictly confidential and never share them publicly. Only generate keys for AI model providers you actively need.
Important Note on Sharing
If you share your script or spreadsheet with others, they may be able to access your embedded API keys. You will be billed for any model usage incurred through your keys, regardless of who initiated the usage.
Usage Costs
Using these functions will incur charges based on the API keys you provide:
Each API call costs money according to your provider's pricing structure.
Batch processing many cells or running frequent operations can quickly increase costs.
We recommend testing with smaller data ranges first to estimate potential costs.
Importantly, set up billing alerts or usage quotas with your API provider to avoid unexpected charges.
Data Privacy and AI LimitationsData Privacy
Any data you send to these AI providers through these functions:
Is processed on their servers according to their privacy policies
May be stored or used to improve their models (depending on their terms)
Should not include confidential, sensitive, or personally identifiable information
AI LimitationsAI models can make mistakes, hallucinate facts, or provide incorrect information
Always review AI-generated content before using it for important decisions
Use appropriate checks and balances for critical applications
Consider adding human review steps for important workflows
AI models can make mistakes, hallucinate facts, or provide incorrect information
Always review AI-generated content before using it for important decisions
Use appropriate checks and balances for critical applications
Consider adding human review steps for important workflows
DISCLAIMER: This content is provided for educational purposes only. All code, templates, and information should be thoroughly reviewed and tested before use. Use at your own risk. Full Terms of Service apply.
Small Scripts, Big Impact
Join 1,500+ professionals who are supercharging their productivity with Google Sheets automation
By subscribing, you agree to our Privacy Policy and Terms of Service