By Fikra DevRel • 4 min read

Go OpenAI SDK Proxy Integration Guide

Go OpenAI SDK Proxy Integration Guide

Prerequisites

  • Go 1.18 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.
  • The github.com/sashabaranov/go-openai library (or equivalent OpenAI-compatible Go client).

Environment Variable Setup

Set the FIKRA_API_KEY environment variable to your Fikra API key. Do not hardcode keys in source code.

export FIKRA_API_KEY="fk_live_your_key_here"

Go Implementation

The go-openai library supports custom base URLs. Configure the client to point to Fikra’s OpenAI-compatible endpoint.

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/sashabaranov/go-openai"
)

func main() {
    apiKey := os.Getenv("FIKRA_API_KEY")
    if apiKey == "" {
        log.Fatal("FIKRA_API_KEY environment variable is not set")
    }

    config := openai.DefaultConfig(apiKey)
    config.BaseURL = "https://api.fikraapi.co.ke/v1"

    client := openai.NewClientWithConfig(config)

    messages := []openai.ChatCompletionMessage{
        {Role: openai.ChatMessageRoleUser, Content: "Hello, Fikra!"},
    }

    resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
        Model:    "fikra-flash",
        Messages: messages,
    })
    if err != nil {
        log.Fatalf("Error creating chat completion: %v", err)
    }

    fmt.Println(resp.Choices[0].Message.Content)
}

Streaming Guidance

To enable streaming, set Stream: true in the request. The response will be a channel of ChatCompletionChunk objects.

stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{
    Model:    "fikra-flash",
    Messages: messages,
    Stream:   true,
})
if err != nil {
    log.Fatalf("Error creating stream: %v", err)
}

for {
    chunk, err := stream.Recv()
    if err != nil {
        if err.Error() == "EOF" {
            break
        }
        log.Fatalf("Error receiving chunk: %v", err)
    }
    fmt.Print(chunk.Choices[0].Delta.Content)
}

Common Errors

  • 400 Bad Request: Malformed JSON, missing required fields, or invalid model alias. Verify the request body and ensure model is a valid Fikra alias (e.g., fikra-flash).
  • 401 Unauthorized: Missing or invalid Authorization header. Ensure the Bearer token is correctly formatted as Bearer fk_live_....
  • 402 Payment Required: Insufficient token credits in your Fikra wallet. Top up via the Fikra dashboard.
  • 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 issue. Retry with backoff. Check the Fikra dashboard for status updates.

Production Checklist

  1. Key Management: Store FIKRA_API_KEY in a secure environment variable or secret manager.
  2. Rate Limiting: Implement client-side rate limiting to stay within 30 RPM (unverified) or 100 RPM (trusted).
  3. Error Handling: Handle 401, 402, 429, and 503 errors with appropriate retries and user-facing messages.
  4. Timeouts: Set reasonable HTTP timeouts to avoid hanging requests.
  5. Logging: Log request IDs and error codes for debugging, but avoid logging sensitive data like API keys.
  6. Model Selection: Use fikra-flash for low-latency tasks; consult the Fikra model registry for other aliases like fikra-pro-20b if higher quality is needed.
  7. Monitoring: Monitor token usage and wallet balance to avoid unexpected 402 errors.

JM
// The Founder

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.

usagestatusdashboard