Python OpenAI SDK base_url override
Prerequisites
- Python 3.8 or later.
- The
openaiPython package (v1.x or later). - A Fikra API key (
fk_live_...) generated from the Fikra dashboard. - Access to the Fikra API base URL:
https://api.fikraapi.co.ke/v1.
Environment Variable Setup
Store your API key in an environment variable to avoid hardcoding secrets. For example, in your shell or .env file:
export FIKRA_API_KEY="fk_live_your_key_here"
Basic Integration Example
The Fikra API is OpenAI-compatible for chat-completions requests. You can use the standard openai Python SDK by overriding the base_url.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["FIKRA_API_KEY"],
base_url="https://api.fikraapi.co.ke/v1"
)
response = client.chat.completions.create(
model="fikra-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, Fikra!"}
]
)
print(response.choices[0].message.content)
Streaming Guidance
Fikra supports streaming via Server-Sent Events (SSE) when stream: true is set. The OpenAI SDK handles SSE parsing automatically.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["FIKRA_API_KEY"],
base_url="https://api.fikraapi.co.ke/v1"
)
stream = client.chat.completions.create(
model="fikra-flash",
messages=[
{"role": "user", "content": "Explain quantum computing in one sentence."}
],
stream=True
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
Common Errors
- 400 Bad Request: Malformed JSON, missing required fields, or invalid model alias. Verify that the model name matches a current Fikra alias (e.g.,
fikra-flash). - 401 Unauthorized: Missing or invalid
Authorization: Bearerheader. EnsureFIKRA_API_KEYis set correctly and the key is active. - 402 Payment Required: Insufficient token credits in your Fikra wallet. Top up via the Fikra dashboard using Paystack (M-Pesa or cards accepted).
- 429 Too Many Requests: Rate limit exceeded. Unverified accounts are limited to 30 RPM; trusted accounts to 100 RPM. Implement exponential backoff.
- 503 Service Unavailable: Temporary server-side issue. Retry with backoff. Check the Fikra dashboard for service status if the issue persists.
Production Checklist
- [ ] Use environment variables for
FIKRA_API_KEY; never hardcode keys. - [ ] Set
base_urltohttps://api.fikraapi.co.ke/v1explicitly. - [ ] Validate model aliases against the current Fikra model registry before deployment.
- [ ] Implement retry logic with exponential backoff for 429 and 503 errors.
- [ ] Monitor token credit usage via the Fikra dashboard to avoid 402 errors.
- [ ] Test streaming behavior in staging before enabling in production.
- [ ] Log request IDs (if provided in responses) for support troubleshooting.
James Miano
CTO & ML Engineer at Lacesse Ventures. James specializes in low-overhead LLM quantization processes, custom ternary weights architectures, and localized server optimization.
Stop paying for overpriced round-trip latency
Why route queries over Western servers when you can use low-overhead hardware located in Nairobi? Save 87% on your monthly inference spend. No minimum credit limits. M-Pesa ready.