This guide covers how to get and set up your SerpApi API key for AI coding agents.
- Sign up for an account at serpapi.com.
- Go to the Dashboard to find your key.
- Copy the API key for use in the configurations below.
- Environment Variables: Always store your key in an environment variable named
SERPAPI_KEY. - Never Commit Keys: Do not commit your API key to any version control system.
- Rotate Keys: If you suspect exposure, rotate your key immediately in the SerpApi dashboard.
For local development, add the key to your shell profile (.zshrc or .bashrc):
export SERPAPI_KEY=your_key_hereReload your profile:
source ~/.zshrc # or source ~/.bashrcUse the serpapi-mcp server for Model Context Protocol integration.
Option A — CLI (recommended):
claude mcp add serpapi -- npx -y @serpapi/serpapi-mcpThen set your API key in the environment (see Shell Environment above) or pass it via SERPAPI_KEY=your_key_here claude mcp add ....
Option B — Manual config:
Add to ~/.claude/settings.json (global) or .claude/settings.json (project-scoped):
{
"mcpServers": {
"serpapi": {
"command": "npx",
"args": ["-y", "@serpapi/serpapi-mcp"],
"env": {
"SERPAPI_KEY": "your_key_here"
}
}
}
}Add to .cursor/mcp.json (project) or ~/.cursor/mcp.json (global):
{
"mcpServers": {
"serpapi": {
"command": "npx",
"args": ["-y", "@serpapi/serpapi-mcp"],
"env": {
"SERPAPI_KEY": "your_key_here"
}
}
}
}Add to .windsurf/mcp.json:
{
"mcpServers": {
"serpapi": {
"command": "npx",
"args": ["-y", "@serpapi/serpapi-mcp"],
"env": {
"SERPAPI_KEY": "your_key_here"
}
}
}
}Add to .opencode/config.json or rely on the shell environment variable above.
Set the environment variable before launching the agent:
export SERPAPI_KEY=your_key_hereStore your key as a repository secret named SERPAPI_KEY, then reference it in your workflow:
jobs:
search:
runs-on: ubuntu-latest
steps:
- name: Run agent with SerpApi
env:
SERPAPI_KEY: ${{ secrets.SERPAPI_KEY }}
run: |
echo "Agent will pick up SERPAPI_KEY from the environment"Store the key as a CI/CD variable in your project settings:
search-job:
script:
- echo "Agent will pick up SERPAPI_KEY from CI environment"
variables:
SERPAPI_KEY: $SERPAPI_KEYPass the key at runtime:
docker run -e SERPAPI_KEY=your_key_here your-agent-image