Ollama API Drop-in Replacement: Integrating Fikra API
Overview
Ollama is a local model runner that exposes an OpenAI-compatible API. Because Fikra API also implements an OpenAI-style chat-completions interface, you can configure Ollama (or any OpenAI-compatible client) to point to Fikra API as a remote backend. This guide demonstrates how to set up this integration using the openai Python SDK, which is the standard way to interact with Ollama's API surface.
Note: Ollama itself is a local inference engine. It does not natively proxy requests to external APIs like Fikra. Instead, you use an OpenAI-compatible client (like the
openaiPython SDK) and configure it to use Fikra's base URL and API key. This effectively replaces the local Ollama endpoint with Fikra's cloud endpoint.
Prerequisites
- A Fikra API account with an active API key (
fk_live_...). - Python 3.8+ installed.
- The
openaiPython package installed (pip install openai).
Environment Variable Setup
Store your Fikra API key in an environment variable to avoid hardcoding secrets.
export FIKRA_API_KEY="fk_live_your_key_here"
Python Example
The following example uses the openai Python SDK to call Fikra API. The base_url is set to Fikra's endpoint, and the api_key is read from the environment variable.
import os
from openai import OpenAI
# Initialize the client with Fikra's base URL and API key
client = OpenAI(
base_url="https://api.fikraapi.co.ke/v1",
api_key=os.getenv("FIKRA_API_KEY")
)
# Make a chat completion request
response = client.chat.completions.create(
model="fikra-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of Kenya?"}
]
)
print(response.choices[0].message.content)
Streaming Guidance
Fikra API supports streaming via Server-Sent Events (SSE). To enable streaming, set stream=True in the request. The response will be an iterator of chunks.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.fikraapi.co.ke/v1",
api_key=os.getenv("FIKRA_API_KEY")
)
# Enable streaming
stream = client.chat.completions.create(
model="fikra-flash",
messages=[
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
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
| HTTP Status | Meaning | Action |
|---|---|---|
| 400 | Bad Request | Check your request payload. Ensure model is a valid Fikra alias (e.g., fikra-flash) and messages is correctly formatted. |
| 401 | Unauthorized | Your API key is missing or invalid. Verify the FIKRA_API_KEY environment variable and ensure the key starts with fk_live_. |
| 402 | Payment Required | Your token-credit wallet is empty. Top up your account via the Fikra dashboard using Paystack (M-Pesa or cards). |
| 429 | Too Many Requests | You have exceeded your rate limit. Unverified accounts are limited to 30 RPM; trusted accounts to 100 RPM. Implement exponential backoff. |
| 503 | Service Unavailable | Fikra API is temporarily unavailable. Retry with backoff. Check the Fikra dashboard for status updates. |
Production Checklist
- [ ] API Key Security: Store
FIKRA_API_KEYin a secure environment variable or secret manager. Never commit it to version control. - [ ] Rate Limiting: Implement client-side rate limiting and exponential backoff to handle 429 responses gracefully.
- [ ] Error Handling: Catch and log specific HTTP errors (400, 401, 402, 429, 503) to provide meaningful feedback to users.
- [ ] Streaming Timeout: Set appropriate timeouts for streaming requests to avoid hanging connections.
- [ ] Model Availability: Verify that the model alias (e.g.,
fikra-flash) is available in your account by checking the Fikra dashboard or model registry. - [ ] Wallet Monitoring: Monitor your token-credit wallet balance to avoid 402 errors during peak usage.
- [ ] Logging: Log request IDs and response metadata for debugging and auditing purposes.
By following this guide, you can seamlessly integrate Fikra API into your existing OpenAI-compatible workflows, replacing local Ollama endpoints with Fikra's cloud-based models.
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.