Overview
This guide walks you through setting up Google’s Gemini API for AI-powered features in your Africa’s Talking applications.Getting a Gemini API Key
- Visit Google AI Studio
- Sign in with your Google account
- Click Get API Key or Create API Key
- Copy your API key - you’ll need it for configuration
Installing the SDK
Install the Google Generative AI SDK using pip:python-dotenv package is used to manage environment variables securely.
Configuring Credentials
1. Create a .env file
In your project root, create a .env file:
2. Add .env to .gitignore
Ensure your .env file is not tracked by Git:
3. Load Environment Variables
The AI utilities automatically load your credentials:utils/ai_utils.py
Configuration Options
DEFAULT_MODEL
TheDEFAULT_MODEL is set to gemini-2.5-flash by default, which provides:
- Fast response times (ideal for real-time applications)
- Cost-effective API usage
- Good balance of quality and speed
.env file or when calling AI functions:
Available Models
gemini-2.5-flash- Fast, optimized for speed (default)gemini-2.0-pro- More powerful, better for complex tasksgemini-1.5-pro- Previous generation, stable
Verifying Your Setup
Test your configuration with a simple script:Troubleshooting
”Missing GEMINI_API_KEY in environment”
- Ensure your
.envfile exists in the project root - Check that the variable name is exactly
GEMINI_API_KEY - Verify you’ve called
load_dotenv()before accessing the key
”Invalid API key” or authentication errors
- Verify your API key is correct (no extra spaces)
- Check that your API key is active in Google AI Studio
- Ensure you have API access enabled for your Google account
Next Steps
AI Utilities
Learn about the three main AI utility functions
Examples
See real-world examples with SMS, Voice, and USSD
