This guide provides step-by-step instructions for setting up the Google Cloud Platform (GCP) SDK and integrating the Gemini Live API into the DAO Copilot project.
- Prerequisites
- Installation
- Authentication Setup
- Project Configuration
- Basic Usage
- Testing & Validation
- Troubleshooting
- Best Practices
- API Reference
Before starting, ensure you have:
- Node.js (v18 or higher)
- npm or yarn package manager
- TypeScript configured in your project
- A Google Cloud Platform account
- Access to Gemini AI API (API key or service account)
The GCP SDK setup requires two main packages:
# Install Google Generative AI SDK
npm install @google/genai
# Install Google Auth Library (for advanced authentication)
npm install google-auth-library
# Install dotenv for environment variable management
npm install dotenvCheck that the packages are installed correctly:
npm list @google/genai google-auth-libraryExpected output:
├── @google/genai@1.12.0
└── google-auth-library@9.x.x
-
Get your API Key:
- Go to Google AI Studio
- Create a new API key or use an existing one
- Copy the API key (starts with
AIzaSy...)
-
Set Environment Variables: Create or update your
.envfile:# Primary API key (recommended) GEMINI_API_KEY=AIzaSyYourApiKeyHere # Alternative names (for compatibility) GOOGLE_AI_API_KEY=AIzaSyYourApiKeyHere GOOGLE_API_KEY=AIzaSyYourApiKeyHere
-
Load Environment Variables: Ensure your application loads the
.envfile:import dotenv from 'dotenv' dotenv.config()
-
Create Service Account:
- Go to Google Cloud Console
- Navigate to IAM & Admin > Service Accounts
- Create a new service account
- Download the JSON key file
-
Set Environment Variables:
# Option A: File path GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json # Option B: JSON content (for deployment) GCP_SERVICE_ACCOUNT_KEY={"type":"service_account",...} # Project configuration GCP_PROJECT_ID=your-project-id
When running on Google Cloud Platform (Cloud Run, GKE, etc.), authentication can use the default service account:
# No additional configuration needed
# The SDK will automatically use metadata serverEnsure your tsconfig.json supports the GCP SDK:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}The project includes a comprehensive SDK manager at src/services/gcp-sdk-manager.ts. Here's how to use it:
import {initializeGCPSDK, getGCPSDK} from '@/services/gcp-sdk-manager'
// Initialize SDK with environment configuration
const sdkInstance = await initializeGCPSDK()
// Or with custom configuration
const sdkInstance = await initializeGCPSDK({
authMethod: 'api-key',
debug: true,
geminiLive: {
model: 'gemini-2.5-flash-preview-native-audio-dialog',
enableNativeAudio: true
}
})Run the included test to verify your setup:
# Run the simple API test
node simple-api-test.mjs
# Or run comprehensive tests
npx tsx src/tests/basic-gemini-api-test.tsExpected output:
🎉 All tests passed!
✅ Basic API functionality is working correctly
import {getGCPSDK} from '@/services/gcp-sdk-manager'
const sdk = getGCPSDK()
if (!sdk) throw new Error('SDK not initialized')
const response = await sdk.genAI.models.generateContent({
model: 'gemini-2.5-flash',
contents: [
{
parts: [{text: 'Hello, world!'}]
}
]
})
console.log(response.candidates[0].content.parts[0].text)const stream = await sdk.genAI.models.generateContentStream({
model: 'gemini-2.5-flash',
contents: [
{
parts: [{text: 'Tell me a story'}]
}
]
})
for await (const chunk of stream) {
const text = chunk.candidates?.[0]?.content?.parts?.[0]?.text
if (text) {
process.stdout.write(text)
}
}import {createGeminiLiveSession} from '@/services/gcp-sdk-manager'
const session = await createGeminiLiveSession({
model: 'gemini-2.5-flash-preview-native-audio-dialog',
onMessage: message => {
console.log('Received:', message)
},
onError: error => {
console.error('Session error:', error)
}
})
// Send audio data
await session.send({
type: 'audio',
data: audioBuffer
})The project includes comprehensive test suites:
-
Authentication Tests:
node auth-test.mjs
-
Basic API Tests:
node simple-api-test.mjs
-
TypeScript Compilation:
npx tsc --noEmit
-
Check Environment Variables:
echo $GEMINI_API_KEY # Should output your API key
-
Test API Connection:
curl -H "Content-Type: application/json" \ -d '{"contents":[{"parts":[{"text":"Hello"}]}]}' \ "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GEMINI_API_KEY"
-
Verify Live API Support: Check that the Live API interface is available in your SDK version.
Problem: Environment variables not loaded properly.
Solutions:
- Verify
.envfile exists and containsGEMINI_API_KEY - Check that
dotenv.config()is called before using the SDK - Ensure
.envis not in.gitignoreif needed for deployment
Problem: TypeScript import/export issues.
Solutions:
- Verify
@google/genaiis installed:npm list @google/genai - Check TypeScript configuration supports ES modules
- Use dynamic imports if static imports fail
Problem: Invalid API key or expired credentials.
Solutions:
- Verify API key is correct and active
- Check Google AI Studio for API key status
- Regenerate API key if necessary
Problem: Live API interface not found.
Solutions:
- Update to latest
@google/genaiversion - Verify model name is correct for Live API
- Check Google AI documentation for availability
Problem: Too many API requests.
Solutions:
- Implement exponential backoff retry logic
- Monitor API usage in Google Cloud Console
- Consider upgrading to paid plan for higher limits
Enable debug logging to troubleshoot issues:
const sdkInstance = await initializeGCPSDK({
debug: true,
logLevel: 'debug'
})This will output detailed information about:
- Authentication attempts
- API requests and responses
- Error details and stack traces
- Never commit API keys to version control
- Use service accounts in production environments
- Rotate API keys regularly
- Restrict API keys to specific services/domains when possible
- Monitor API usage for suspicious activity
- Reuse SDK instances - use the singleton pattern
- Implement connection pooling for high-volume applications
- Cache authentication tokens to reduce overhead
- Use streaming for long responses
- Implement retry logic with exponential backoff
- Use environment-specific configurations
- Implement comprehensive error handling
- Add logging and monitoring
- Write unit tests for SDK integration
- Document API usage and rate limits
- Use service accounts instead of API keys
- Set up proper logging and monitoring
- Implement health checks for SDK connectivity
- Configure auto-scaling based on API usage
- Set up alerts for error rates and quotas
Main SDK management class with singleton pattern.
getInstance(): Get singleton instanceinitialize(config?): Initialize SDK with configurationcreateLiveSession(options): Create Live API sessionrefreshCredentials(): Refresh authentication credentials
interface GCPSDKConfig {
authMethod?: 'api-key' | 'service-account' | 'default' | 'auto'
apiKey?: string
serviceAccount?: {
keyFile?: string
credentials?: object
}
project?: {
id?: string
region?: string
}
geminiLive?: {
model?: string
enableNativeAudio?: boolean
websocketTimeout?: number
}
debug?: boolean
logLevel?: 'error' | 'warn' | 'info' | 'debug'
}| Variable | Description | Example |
|---|---|---|
GEMINI_API_KEY |
Primary API key for Gemini AI | AIzaSy... |
GOOGLE_AI_API_KEY |
Alternative API key name | AIzaSy... |
GOOGLE_API_KEY |
Alternative API key name | AIzaSy... |
GOOGLE_APPLICATION_CREDENTIALS |
Service account file path | /path/to/key.json |
GCP_SERVICE_ACCOUNT_KEY |
Service account JSON content | {"type":"service_account"...} |
GCP_PROJECT_ID |
Google Cloud project ID | my-project-123 |
gemini-2.5-flash- Fast text generationgemini-2.5-pro- Advanced reasoning and analysis
gemini-2.5-flash-preview-native-audio-dialog- Real-time audio processinggemini-live-2.5-flash-preview- Half-cascade processing
Last Updated: August 4, 2025
Version: 1.0.0
Compatibility: @google/genai v1.12.0+