Overview
This guide walks you through setting up your development environment for the Africa’s Talking AI Integration Course. Proper environment configuration is crucial for secure API key management and smooth development workflow.Prerequisites
Before you begin, ensure you have:- Python 3.8 or higher installed
- An Africa’s Talking account with API credentials
- A Google Cloud account for AI services (Gemini/Vertex AI)
- A text editor or IDE for editing configuration files
Creating the .env File
Create .env File
Create a new
.env file in the root directory:The
.env file should never be committed to version control. Ensure it’s listed in your .gitignore file.Environment Variables Explained
Flask Configuration
| Variable | Purpose | Example Value | Required |
|---|---|---|---|
FLASK_ENV | Sets the Flask environment mode | development or production | Yes |
PORT | Port number for the Flask server | 9000 (default: 5000) | No |
Tunneling Configuration
| Variable | Purpose | Example Value | Required |
|---|---|---|---|
NGROK_AUTHTOKEN | Authentication token for ngrok | 2abc...xyz | Yes (if using ngrok) |
You’ll need this to expose your local server to the internet for receiving webhooks from Africa’s Talking.
Africa’s Talking Credentials
| Variable | Purpose | Example Value | Required |
|---|---|---|---|
AT_USERNAME | Your Africa’s Talking username | sandbox or your app name | Yes |
AT_API_KEY | Your Africa’s Talking API key | atsk_abc123... | Yes |
AT_VOICE_NUMBER | Your voice-enabled phone number | +254711000000 | For Voice API |
AT_SHORTCODE | Your SMS shortcode | 8995 | For two-way SMS |
Getting Africa’s Talking Credentials
Create an Account
Visit Africa’s Talking and sign up for an account.
Create a Team and App
- After logging in, create a new team
- Inside the team, create a new app
- Choose whether to use the sandbox (for testing) or production
Get API Credentials
- Navigate to your app settings
- Find your Username (usually “sandbox” for sandbox apps)
- Generate or copy your API Key
- Store these securely in your
.envfile
Google Cloud AI Configuration
| Variable | Purpose | Example Value | Required |
|---|---|---|---|
PROJECT_ID | Your Google Cloud project ID | my-project-123 | For Vertex AI |
GEMINI_API_KEY | API key for Gemini AI | AIza... | For Gemini API |
VERTEX_API_KEY | API key for Vertex AI | AIza... | For Vertex AI |
Getting Google Cloud Credentials
Create Google Cloud Account
Visit Google Cloud Console and create an account if you don’t have one.
Get Gemini API Key
- Go to Google AI Studio
- Create a new API key
- Copy the key to your
.envfile asGEMINI_API_KEY
Environment Best Practices
Security Guidelines
File Permissions
On Unix-based systems (Linux/macOS), restrict access to your.env file:
Using .env.example
Create a.env.example file to document required variables without exposing secrets:
Verifying Environment Variables
You can verify your environment variables are loaded correctly:The project uses
python-dotenv to automatically load environment variables from the .env file when the Flask app starts.Environment-Specific Configuration
Development Environment
- Uses sandbox credentials
- Debug mode enabled
- Detailed error messages
- Auto-reload on code changes
Production Environment
- Uses production credentials
- Debug mode disabled
- Minimal logging
- Optimized for performance
Troubleshooting
Environment Variables Not Loading
Problem: Your application can’t find environment variables. Solutions:- Verify the
.envfile is in the project root directory - Check that
dotenvis installed:pip install python-dotenv - Ensure
load_dotenv()is called before accessing variables - Check for typos in variable names
API Authentication Failures
Problem: Getting authentication errors from Africa’s Talking or Google Cloud. Solutions:- Verify your API keys are correct (no extra spaces)
- Check if you’re using sandbox vs. production credentials correctly
- Ensure API keys haven’t expired or been revoked
- Verify you have access to the requested services
Port Already in Use
Problem: Error message: “Address already in use” Solutions:- Change the
PORTvariable in.envto an unused port - Stop any other applications using the same port
- On Unix systems, find and kill the process:
Next Steps
Now that your environment is configured:- Set up tunneling to expose your local server
- Configure webhooks in the Africa’s Talking dashboard
- Test your endpoints to ensure everything works
