Overview
Fikra API is an OpenAI-compatible AI inference API designed for developers and growing AI teams. You can run chat models behind one OpenAI-style endpoint and pay in USD or KES. This page shows how to use the DeepSeek v4 model (deepseek-v4-flash-0731) to generate code as part of a script using the chat completions endpoint.
The API base URL is https://api.fikraapi.co.ke/v1, and the chat endpoint is POST https://api.fikraapi.co.ke/v1/chat/completions. Requests and responses follow the OpenAI chat-completions format, and streaming is supported.
Prerequisites
- A Fikra API account with an active API key.
- The
openaiPython SDK installed. - Your API key stored in the environment variable
FIKRA_API_KEY.
Step-by-step
- Create an
OpenAIclient with the Fikra API base URL and your API key:
```python from openai import OpenAI import os
client = OpenAI( base_url="https://api.fikraapi.co.ke/v1", api_key=os.environ["FIKRA_API_KEY"], ) ```
- Send a chat completion request to a DeepSeek v4 model with a code-generation prompt:
```python response = client.chat.completions.create( model="deepseek-v4-flash-0731", messages=[ { "role": "user", "content": "Write a Python script that reads a CSV file and prints the sum of a column.", } ], )
print(response.choices[0].message.content) ```
- The response body contains the generated script as the
message.contentvalue. You can save it to a.pyfile and run it, or use it directly in your own automation.
Other DeepSeek v4 models are also available: deepseek-v4.1-flash and deepseek-v4.1-flash-1017. Set model to one of those names if you prefer a different version.
Verify it works
Check that response.choices[0].message.content is a non-empty string containing Python code. For example, print the value and confirm it includes import csv or a similar code block. Unauthorized or malformed requests will return an error status instead of a valid completion.
You can also use stream=True in the request to receive tokens as they are generated.
Costs
Pricing is based on tokens processed. For deepseek-v4-flash-0731:
- Input: $0.09 USD per 1M tokens
- Output: $0.15 USD per 1M tokens
Other available models have different rates, including deepseek-v4.1-flash (input $0.25 / output $0.80 per 1M tokens), deepseek-v4.1-flash-1017 (input $0.30 / output $0.85 per 1M tokens), and fikra-auto (input $0.40 / output $0.40 per 1M tokens). For models not listed here, check the pricing page.
Payments are handled through a token-credit wallet. You can top up with M-Pesa or card via Paystack, and agents can use x402 USDC on Base.
Common errors
- 401 Unauthorized to Your API key is invalid or inactive. Check that
FIKRA_API_KEYis set correctly and that the key has not been revoked. - 400 Bad Request to The requested model is unsupported or the request format is invalid. Confirm the model name matches one available on Fikra API.
- 429 Too Many Requests to You have hit the rate limit. Limits are 30 requests per minute for unverified keys and 100 requests per minute for trusted keys. Wait before retrying.
- 503 Service Unavailable to The upstream service is busy. Retry the request with exponential backoff.