Overview
Run a cheap, production‑grade AI assistant on WhatsApp by routing user messages to Fikra API’s OpenAI‑compatible chat endpoint. The service works with any WhatsApp webhook provider and charges only for the tokens you use.
Prerequisites
- Python 3.8+ installed
- Fikra API key (set as the environment variable FIKRA_API_KEY)
- OpenAI Python SDK (
pip install openai) - A WhatsApp webhook that forwards incoming messages to your server (e.g., Twilio, 360dialog, or any provider that can POST JSON to your endpoint)
Step-by-step
- Install the SDK
bash pip install openai flask - Set the API key
bash export FIKRA_API_KEY=your_fikra_api_key - Create a simple Flask webhook that receives a WhatsApp message, calls Fikra, and returns the reply. ```python import os, openai from flask import Flask, request, jsonify
openai.api_key = os.getenv("FIKRA_API_KEY") openai.base_url = "https://api.fikraapi.co.ke/v1"
app = Flask(name)
@app.route("/whatsapp-webhook", methods=["POST"]) def webhook(): data = request.json user_msg = data.get("message") if not user_msg: return jsonify({"error": "no message"}), 400
response = openai.ChatCompletion.create(
model="deepseek-v4-flash-0731",
messages=[{"role": "user", "content": user_msg}],
stream=False,
)
reply = response.choices[0].message.content
# TODO: send `reply` back to WhatsApp via your provider's API
return jsonify({"reply": reply})
if name == "main":
app.run(port=5000)
``
The SDK automatically adds the headerAuthorization: Bearer .
4. **Connect your WhatsApp provider** to POST incoming messages tohttps://yourdomain.com/whatsapp-webhook`.
5. Deploy the Flask app (Heroku, Render, Fly.io, etc.) and ensure it is reachable over HTTPS.
Verify it works
- Send a test message to your WhatsApp number.
- The Flask endpoint should log a request and return a JSON payload containing the AI reply.
- Confirm the reply appears in the WhatsApp conversation.
Costs
- Model:
deepseek-v4-flash-0731 - Pricing: $0.09 USD per 1M input tokens, $0.15 USD per 1M output tokens.
- Example: a 50‑token user message + a 70‑token reply costs roughly $0.000018.
Common errors
- 401 Unauthorized – API key missing, malformed, or inactive.
- 400 Bad Request – Unsupported model name or malformed request payload.
- 429 Too Many Requests – Exceeded rate limits (30 req/min unverified, 100 req/min trusted). Back‑off and retry.
- 503 Service Unavailable – Upstream busy; retry with exponential backoff.