Integrating Fikra API with Supabase Edge Functions Custom LLM Framework
Integrating Fikra API with Supabase Edge Functions
This guide demonstrates how to configure the Supabase Edge Functions custom LLM framework to use Fikra API as your custom OpenAI-compatible endpoint. By pointing the base_url to api.fikraapi.co.ke/v1, you can leverage Fikra's models within your Supabase serverless functions.
Prerequisites
- A Supabase project with Edge Functions enabled.
- A valid API key from Fikra API.
- The
@supabase/functions-jsoropenaipackage installed in your Edge Function environment.
Step 1: Install Dependencies
In your Supabase Edge Function directory, ensure the openai package is available. If using Deno (default for Supabase Edge Functions), you can import it directly via URL or add it to your deno.json.
Step 2: Configure the Custom LLM Client
Below is the exact JavaScript code block to initialize the OpenAI client with the Fikra API base URL. Replace YOUR_FIKRA_API_KEY with your actual secret.
import OpenAI from 'npm:[email protected]';
// Initialize the OpenAI client with Fikra API as the custom endpoint
const fikraClient = new OpenAI({
apiKey: 'YOUR_FIKRA_API_KEY',
baseURL: 'https://api.fikraapi.co.ke/v1',
});
// Example: Create a chat completion request
async function generateResponse(prompt) {
try {
const completion = await fikraClient.chat.completions.create({
model: 'fikra-large', // Use the specific model name provided by Fikra
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: prompt },
],
max_tokens: 150,
temperature: 0.7,
});
return completion.choices[0].message.content;
} catch (error) {
console.error('Error calling Fikra API:', error);
throw error;
}
}
// Export the function for use in your Edge Function handler
export default {
async fetch(request) {
const { prompt } = await request.json();
const response = await generateResponse(prompt);
return new Response(JSON.stringify({ response }), {
headers: { 'Content-Type': 'application/json' },
});
},
};
Step 3: Deploy the Edge Function
- Save the code above as
index.tsin yoursupabase/functions/fikra-llm/directory. - Deploy the function using the Supabase CLI:
bash supabase functions deploy fikra-llm - Set the environment variable for the API key if not hardcoded (recommended for production):
bash supabase secrets set FIKRA_API_KEY=YOUR_FIKRA_API_KEY
Step 4: Test the Integration
Invoke your Edge Function to verify the connection:
curl -X POST https://<your-project-ref>.supabase.co/functions/v1/fikra-llm \
-H "Authorization: Bearer <your-supabase-anon-key>" \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello, Fikra!"}'
Notes
- Base URL: Ensure the
baseURLis exactlyhttps://api.fikraapi.co.ke/v1. - Model Names: Refer to the Fikra API documentation for available model identifiers.
- Error Handling: Always handle potential network or authentication errors in production environments.
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.