How-to

Stream Chat Responses into a Web App with SSE

Last updated 2026-11-25. Facts are checked against the live registry.

Overview

Fikra API is an OpenAI-compatible AI inference API for developers and growing AI teams. Its chat completions endpoint accepts OpenAI-style requests and responses with streaming support, so you can stream token chunks into a web app as server-sent events (SSE).

Prerequisites

  • A Fikra API key set as the FIKRA_API_KEY environment variable
  • The OpenAI Python SDK
  • A web app that can read an SSE stream (for example, EventSource in the browser)

Step-by-step

  1. Create an OpenAI-style client pointed at the Fikra API base URL.
  2. Call the chat completions endpoint with stream=True and a model from your account.
  3. Forward each chunk to the browser over SSE.
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="deepseek-v4-flash-0731",
    messages=[{"role": "user", "content": "Write a short Python snippet."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content:
        # Send each piece to the web app as an SSE event
        print(delta.content, end="", flush=True)

In production, the loop above sits in a server route that writes each delta.content value as an SSE data: event. The browser subscribes to that route and appends events to the chat UI as they arrive.

Verify it works

Run the script and confirm tokens appear progressively rather than all at once. Then connect your web app to the SSE route and confirm the UI updates incrementally with each chunk.

Costs

Pricing is per 1M tokens, billed in USD from your token-credit wallet:

  • deepseek-v4-flash-0731: $0.05625 input, $0.17 output
  • deepseek-v4.1-flash: $0.05625 input, $0.09 output
  • deepseek-v4.1-flash-1017: $0.05 input, $0.11 output
  • deepseek-v4.1-flash-1017: $0.05625 input, $0.35 output
  • deepseek-v4.1-flash-1017: $6.00 input, $12.00 output
  • deepseek-v4.1-flash-1017: $0.05625 input, $0.33 output

Common errors

  • 401: Bad or inactive API key. Check the key and confirm it is scoped and active.
  • 400: Unsupported model. Use a model name from your available models list.
  • 429: Rate limit reached. Limits are 30 requests/min for unverified keys and 3,000 requests/min for trusted keys; retry after a short delay.
  • 503: Upstream is busy. Retry the request with backoff.

Live prices -> · Agent account -> · SDKs ->