Supabase

Supabase Edge Functions with an OpenAI-compatible uncensored API

Call Abliterator from Supabase Edge Functions: keep the key in supabase secrets, handle CORS, invoke from supabase-js and stream to the browser with toReadableStream.

Reviewed SupabaseTypeScript

Quick answer

Set the key with supabase secrets set ABL_API_KEY=..., then in the function create new OpenAI({ baseURL: "https://abliterator.ai/v1", apiKey: Deno.env.get("ABL_API_KEY") }) from npm:openai inside Deno.serve.

Edge Functions run on Deno, so the official openai package loads through npm:openai. The key is a project secret read with Deno.env.get, as described in Supabase's Edge Function secrets guide, and browser calls need the CORS headers Supabase publishes. The functions were type-checked against the Deno 2.7 types and their handlers called with test requests against a stand-in server; the Supabase CLI itself was not run.

Setup

supabase functions new triage
echo 'ABL_API_KEY=sk-abl-v1-...' >> supabase/functions/.env
supabase secrets set ABL_API_KEY=sk-abl-v1-...

supabase/functions/.env is for local supabase start and functions serve; secrets set is for the hosted project. Keep the .env file out of git.

A JSON function

supabase/functions/triage/index.ts:

import { corsHeaders } from "npm:@supabase/supabase-js@^2/cors";
import OpenAI from "npm:openai";

const abliterator = new OpenAI({ baseURL: "https://abliterator.ai/v1", apiKey: Deno.env.get("ABL_API_KEY") });

Deno.serve(async (req) => {
  if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders });

  const { report } = (await req.json()) as { report: string };
  const r = await abliterator.chat.completions.create({
    model: "deepseek-v4.1-flash-abliterated",
    messages: [
      { role: "system", content: "Triage user abuse reports: category, severity 1-5, and a one-line reason." },
      { role: "user", content: report },
    ],
    max_tokens: 200,
  });

  return Response.json({ triage: r.choices[0].message.content }, { headers: corsHeaders });
});

Call it from your app with supabase-js, which adds the project key and the user's session token:

import { createClient } from "@supabase/supabase-js";

const supabase = createClient("https://YOUR-PROJECT.supabase.co", "YOUR-PUBLISHABLE-KEY");

const { data, error } = await supabase.functions.invoke("triage", {
  body: { report: "User keeps sending links to a fake bank login page in group chats." },
});
console.log(error ?? data.triage);

Stream to the browser

Return the SDK stream as a ReadableStream with toReadableStream(). A second function, supabase/functions/brief/index.ts:

import { corsHeaders } from "npm:@supabase/supabase-js@^2/cors";
import OpenAI from "npm:openai";

const abliterator = new OpenAI({ baseURL: "https://abliterator.ai/v1", apiKey: Deno.env.get("ABL_API_KEY") });

Deno.serve(async (req) => {
  if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders });

  const { topic } = (await req.json()) as { topic: string };
  const stream = await abliterator.chat.completions.create(
    {
      model: "kimi-k3.1-pro-abliterated",
      messages: [{ role: "user", content: `Write a short trust-and-safety policy brief on ${topic}.` }],
      stream: true,
    },
    { signal: req.signal },
  );

  return new Response(stream.toReadableStream(), {
    headers: { ...corsHeaders, "Content-Type": "application/x-ndjson" },
  });
});

In the browser, fetch the function URL and rebuild the stream with the same SDK. Pass the signed-in user's access token so Supabase accepts the call:

import { ChatCompletionStream } from "openai/lib/ChatCompletionStream";

export async function streamBrief(functionsUrl: string, accessToken: string, topic: string, onText: (t: string) => void) {
  const res = await fetch(`${functionsUrl}/brief`, {
    method: "POST",
    headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json" },
    body: JSON.stringify({ topic }),
  });
  if (!res.ok || !res.body) throw new Error(`brief failed: ${res.status}`);

  const stream = ChatCompletionStream.fromReadableStream(res.body);
  stream.on("content", (delta) => onText(delta));
  await stream.finalChatCompletion();
}

The browser bundle only imports the stream parser; the Abliterator key never leaves the function.

Troubleshooting

  • CORS error in the browser. The OPTIONS preflight must return corsHeaders, and every other response must include them too, as in both functions.
  • Invalid JWT or 401 before your code runs. Supabase checks the caller's token by default. Send the user's access token or project key, or deploy with --no-verify-jwt if the function does its own auth.
  • Missing credentials from the SDK. ABL_API_KEY is not set for this environment. Locally, check supabase/functions/.env and restart; in production, run supabase secrets list.
  • secrets set rejects the name. Secret names cannot start with SUPABASE_. ABL_API_KEY is fine.
  • Long answers are cut off. Edge Functions have a per-invocation time limit. Stream, cap max_tokens, and use deepseek-v4.1-flash-abliterated for interactive features.

Frequently asked questions

Which 401 am I getting: Supabase's or Abliterator's?

Supabase rejects calls without a valid project key or user JWT before your code runs. A 401 from inside the function, with an OpenAI-style error body, comes from Abliterator and means ABL_API_KEY is missing or wrong.

Do I need to redeploy after changing the Abliterator key?

No. Supabase says functions read a new secret immediately. Run supabase secrets set ABL_API_KEY=... again and the next invocation uses it.

Why use toReadableStream instead of Server-Sent Events?

It sends newline-delimited JSON chunks that the openai package can rebuild on the client with ChatCompletionStream.fromReadableStream, so the browser gets the same content events as server code without writing an SSE parser.