Skip to main content
Supabase is a hosted Postgres platform with built-in auth, storage, and serverless Edge Functions. An Edge Function is a small TypeScript function that runs on Deno, so you can call a SambaCloud model from server-side code without exposing your API key to the browser. This guide builds one that answers a prompt with a SambaCloud model, with an option to stream the reply, and runs it on your machine with the Supabase CLI.
Supabase’s built-in Supabase.ai.Session has an openaicompatible mode for self-managed servers, but it does not work with SambaCloud. The mode sends no Authorization header, so SambaCloud answers 401 Unauthorized, and there is no option to add one. Use the OpenAI SDK as shown below instead.

Prerequisites

  • A SambaCloud account and API key
  • Docker, running. The Supabase CLI starts its local stack in containers.
  • The Supabase CLI. This guide was tested with versions 2.121.0 and 3.2.0.
You do not need a Supabase account to follow this guide, because everything runs locally.

Installation and setup

1

Create a Supabase project and start the local stack

The first supabase start downloads the container images, which takes a few minutes. supabase functions serve needs the stack to be running, and fails with supabase start is not running otherwise.
2

Create the Edge Function

This creates supabase/functions/sambacloud-chat/index.ts.
3

Store your SambaCloud API key

Create supabase/functions/.env and add your key. The function reads it as SAMBANOVA_API_KEY:
supabase/functions/.env
Do not commit this file.

Call SambaCloud from the function

Replace the contents of supabase/functions/sambacloud-chat/index.ts. The function uses the OpenAI SDK with the SambaCloud base URL, and sends stream: true to the model when the caller asks for a streamed reply:
supabase/functions/sambacloud-chat/index.ts
Serve it and call it:
In a second terminal, send a prompt:
Output (abridged)
To stream the reply, add "stream": true and pass -N to curl so it prints each chunk as it arrives:
--no-verify-jwt skips Supabase’s check for a signed-in user, so you can call the function with a plain curl. supabase functions new already sets verify_jwt = false for this function in supabase/config.toml, so the function also works without the flag. If you change it to true, the function answers 401 Missing authorization header unless the request carries a Supabase JWT or you serve it with --no-verify-jwt. MiniMax-M3.1-Flash is a reasoning model, so the first streamed text can take a couple of seconds. In 32 test runs it arrived after 1.1 to 5.2 s and the reply finished after 3.1 to 8.0 s.
Supabase’s database includes the pgvector extension. To store and search vectors alongside this function, see the pgvector guide.

Troubleshooting

supabase functions serve needs the local stack. Make sure Docker is running, then run supabase start from the project folder.
The function failed before it could answer. Look at the supabase functions serve terminal. Missing credentials. Please pass an apiKey means SAMBANOVA_API_KEY is not set, so check that supabase/functions/.env exists and contains the key.
The function requires a Supabase JWT when verify_jwt is true for it in supabase/config.toml. supabase functions new sets it to false, so check whether you changed it. For local testing, serve the function with --no-verify-jwt, as shown above, or set verify_jwt = false.
The key in supabase/functions/.env is not valid. Copy a key from the SambaCloud API keys page. Restart supabase functions serve after you change the file.
The model value is not a SambaCloud model ID. See SambaCloud models for the current list.
The baseURL is missing /v1. Set it to https://api.sambanova.ai/v1.
You used Supabase.ai.Session with mode: 'openaicompatible'. That mode cannot send a SambaCloud key, so use the OpenAI SDK as shown in this guide.

Additional resources