Prerequisites
Before you begin, ensure you have the following:Python 3.8+
Check your version:
Africa's Talking Account
Sign up at account.africastalking.com
Google Cloud Account
Create an account at cloud.google.com
Tunneling Tool
Install ngrok, Cloudflare Tunnel, or localhost.run
Production vs Sandbox: Voice calls and some advanced features require a production Africa’s Talking account. You can start with the sandbox for SMS testing.
Installation
Configuration
Environment Variables
Create a.env file in the project root directory with your API credentials:
Getting Africa’s Talking Credentials
Create an Africa's Talking account
- Go to account.africastalking.com
- Sign up for a free account
- Create a team
- Create an app within your team
Get your API credentials
- Navigate to your app dashboard
- Go to Settings → API Key
- Generate an API key if you don’t have one
- Copy your username (usually “sandbox” for testing) and API key
Enable SMS (Sandbox)
For sandbox testing:
- The SMS product is enabled by default in sandbox mode
- You can send up to 100 free SMS messages
- You must add recipient phone numbers to your sandbox
Getting Google Cloud Credentials
Create a Google Cloud project
- Go to console.cloud.google.com
- Create a new project or select an existing one
- Note your Project ID (you’ll need this for
PROJECT_IDin.env)
Get Gemini API key
- Go to Google AI Studio
- Click Create API Key
- Copy the API key to your
.envfile asGEMINI_API_KEY
Running the Application
Start your Flask development server:Verify Installation
Test that the server is running by checking the SMS service status:Expose Your Server
To receive webhooks from Africa’s Talking, you need to expose your local server to the internet using a tunneling tool.Option 1: Ngrok (Recommended)
Install ngrok
Download from ngrok.com/download or use a package manager:
Authenticate ngrok
Sign up at ngrok.com to get an auth token, then run:
Start the tunnel
Expose your Flask server (running on port 9000):Or use the Makefile:You’ll see output like this:
Option 2: Cloudflare Tunnel
Option 3: LocalTunnel
Configure Webhooks
Now that your server is exposed to the internet, configure Africa’s Talking to send webhook notifications to your endpoints.SMS Webhooks
Open your Africa's Talking dashboard
Go to account.africastalking.com and select your app.
Set the webhook URLs
Configure the following endpoints (replace
YOUR_NGROK_URL with your actual ngrok URL):- Delivery Reports URL:
https://YOUR_NGROK_URL/api/sms/delivery-reports - Two-Way SMS URL:
https://YOUR_NGROK_URL/api/sms/twoway - Opt-Out URL:
https://YOUR_NGROK_URL/api/sms/opt-out - Subscription URL:
https://YOUR_NGROK_URL/api/sms/subscription
Make sure to use
https:// (not http://) for webhook URLs.Voice Webhooks
Set the webhook URLs
Configure the following endpoints:
- Call Instructions URL:
https://YOUR_NGROK_URL/api/voice/instruct - Event Notification URL:
https://YOUR_NGROK_URL/api/voice/events
Project Structure
Understand the codebase layout:Key Files
app.py - Application Entry Point
app.py - Application Entry Point
The main Flask application file (
app.py:1-19):routes/__init__.py - Route Registration
routes/__init__.py - Route Registration
Automatically discovers and registers all route blueprints:This means any new
.py file you add to the routes/ folder with a Blueprint will be automatically registered!utils/sms_utils.py - SMS Functions
utils/sms_utils.py - SMS Functions
Helper functions for sending SMS (
utils/sms_utils.py:1-76):utils/voice_utils.py - Voice Functions
utils/voice_utils.py - Voice Functions
Helper functions for making calls (
utils/voice_utils.py:1-46):Troubleshooting
Error: Missing Africa's Talking credentials
Error: Missing Africa's Talking credentials
Symptom:Solution:
- Verify your
.envfile exists in the project root - Check that
AT_USERNAMEandAT_API_KEYare set correctly - Restart your Flask server after updating
.env
Port already in use
Port already in use
Symptom:Solution:
- Change the port in your
.envfile:
- Or kill the process using port 9000:
Webhooks not received
Webhooks not received
Symptom: API calls work, but webhooks (delivery reports, call events) are not received.Solution:
- Verify ngrok is running and showing a valid HTTPS URL
- Check that webhook URLs in Africa’s Talking dashboard use
https://(nothttp://) - Test your webhook URL directly:
- Check Flask server logs for incoming requests
- Ensure your firewall allows incoming connections on the Flask port
Module not found errors
Module not found errors
Symptom:Solution:
- Ensure your virtual environment is activated (you should see
(venv)in your prompt) - Reinstall dependencies:
Voice calls not working (sandbox)
Voice calls not working (sandbox)
Symptom: Voice API returns errors or calls are not initiated.Solution:
Voice calls do not work in sandbox mode. You must:
- Upgrade to a production Africa’s Talking account
- Request access to the Voice product
- Get a voice-enabled phone number
- Update
AT_USERNAMEin.envto your production username (not “sandbox”) - Set
AT_VOICE_NUMBERto your voice-enabled number
Google AI/Gemini errors
Google AI/Gemini errors
Symptom: AI features throw authentication errors.Solution:
- Verify your
GEMINI_API_KEYis valid:- Go to Google AI Studio
- Check if your API key is active
- Generate a new key if needed
- For Vertex AI, ensure:
- Your Google Cloud project has Vertex AI API enabled
- Billing is enabled on your project
- You have proper authentication set up
CORS errors (when building a frontend)
CORS errors (when building a frontend)
Solution:
Install Flask-CORS and enable it in
app.py:Next Steps
Now that your environment is set up, proceed to the Quickstart Guide to send your first SMS and make your first call!Quickstart
Send an SMS and make a call in under 5 minutes
SMS API Reference
Explore all SMS endpoints and features
Voice API Reference
Learn about voice calls and IVR
AI Integration
Add AI capabilities to your app
