PatchPaste · Guides

Last updated 30 October 2026

Supabase Edge Function secrets: where to put your OpenAI or Stripe key

Your AI builder wired up OpenAI or Stripe, and it works. But where is the key? If it's in your frontend, every visitor can copy it. Here's how to move it into an Edge Function secret and make sure it's gone from your live app.

Short answer: save the key as an Edge Function secret (Supabase dashboard → Edge Function Secrets, or supabase secrets set OPENAI_API_KEY=...), read it in the function with Deno.env.get('OPENAI_API_KEY'), and have your app call the function instead of OpenAI. Functions read a new secret straight away, with no redeploy (Supabase docs: Edge Function secrets). Then make sure only signed-in users can call that function.

Secret or public? Which keys go where

KeyWhere it belongs
OpenAI, Anthropic, Resend, Stripe secret (sk_live_) keysEdge Function secret (or Lovable Secrets). Never in the browser.
Supabase service_role or sb_secret_ keyAlready in every Edge Function as SUPABASE_SERVICE_ROLE_KEY or SUPABASE_SECRET_KEYS. Never in the browser.
Supabase URL, anon or publishable keyFine in the frontend (for example VITE_SUPABASE_PUBLISHABLE_KEY), as long as Row Level Security is on.
Stripe publishable key (pk_)Fine in the frontend.

Supabase lists the variables every Edge Function already has: SUPABASE_URL, SUPABASE_DB_URL, SUPABASE_PUBLISHABLE_KEYS and SUPABASE_SECRET_KEYS (plus the older SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY). It says the secret ones “bypass Row Level Security, so use them in Edge Functions and never in a browser” (Supabase docs). Why VITE_ values are public: are VITE_ environment variables secret?

Step 1: save the key as a secret

In the Supabase dashboard: open Edge Function Secrets, enter a name such as OPENAI_API_KEY and the value, and save. You need the Owner or Administrator role. Developers can see secrets but not change them.

With the Supabase CLI:

supabase secrets set OPENAI_API_KEY=sk-...
supabase secrets list

Or put several in a file and run supabase secrets set --env-file .env. Two rules from Supabase's docs:

In Lovable: go to More → Settings → Secrets, or ask in chat and Lovable shows a secure form. Lovable says secrets are “injected into your Edge Functions automatically, and never reach the browser”. They're write-only, so you can't read a value back (Lovable docs: Secrets). If your project uses your own Supabase project, the secrets live in Supabase's dashboard instead.

Lovable trap: Lovable says not to gitignore .env in a Lovable project, because it holds the public VITE_ values the build needs. So if a secret key ever lands in that .env, it's committed to your repo and built into your app. Only public VITE_ values belong there.

Step 2: read it in an Edge Function

The function does the OpenAI call on the server, so the key never leaves Supabase. This example uses Supabase's withSupabase wrapper with auth: 'user', which only lets signed-in users in and handles CORS for browser calls (Supabase docs: Securing Edge Functions, CORS):

// supabase/functions/ask-ai/index.ts
import { withSupabase } from 'npm:@supabase/server@1'

export default {
  fetch: withSupabase({ auth: 'user' }, async (req, ctx) => {
    const { prompt } = await req.json()
    const res = await fetch('https://api.openai.com/v1/chat/completions', {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${Deno.env.get('OPENAI_API_KEY')}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        model: 'gpt-4o-mini', // or the model your app already uses
        messages: [{ role: 'user', content: String(prompt).slice(0, 2000) }],
      }),
    })
    const data = await res.json()
    // Send back the answer only. Never return or log the key.
    return Response.json({ answer: data.choices?.[0]?.message?.content ?? null })
  }),
}

Then in your app, replace the direct call with:

const { data, error } = await supabase.functions.invoke('ask-ai', {
  body: { prompt },
})

Finally, delete the old key from your frontend code, from any VITE_ or NEXT_PUBLIC_ variable, and remove dangerouslyAllowBrowser: true if you used the OpenAI SDK in the browser.

Step 3: don't leave the function open

Moving the key is only half the job. If anyone on the internet can call your function, they can still spend your OpenAI credit, just through your function instead of with your key.

Step 4: replace the old key and check the live site

If the key was ever in your frontend, assume it's been copied. Create a new key at OpenAI or Stripe, save it as the secret (functions pick it up immediately), and delete the old key. Then check your live site: open DevTools, search the loaded JavaScript for sk- or sk_live_, and make sure nothing comes up. More on rotating Supabase admin keys: what to do if your service_role key is exposed.

A prompt you can paste into Lovable, Bolt or Cursor

Move every OpenAI, Stripe secret or other private API call out of the frontend into a Supabase Edge Function. Read the key with Deno.env.get from Edge Function secrets (ask me to add it as a secret, do not put it in .env or any VITE_ variable). Only allow signed-in users to call the function, keep verify_jwt on, and never return or log the key. Call the function from the app with supabase.functions.invoke. Remove the old key and dangerouslyAllowBrowser from the frontend code.

Quick checklist

Before launch, run the full Lovable/Bolt pre-launch checklist.

Edge Function secrets FAQ

Where should I store my OpenAI or Stripe secret key in a Supabase app?

In Edge Function secrets, never in your frontend code or a VITE_ or NEXT_PUBLIC_ variable. Set it in the Supabase dashboard under Edge Function Secrets or with supabase secrets set NAME=value, then read it inside the function with Deno.env.get('NAME'). Your app calls the function, and the function calls OpenAI or Stripe.

Do I need to redeploy after setting a Supabase secret?

No. Supabase's docs say functions read a new secret immediately, so you don't need to redeploy.

Why can't I name my secret SUPABASE_SOMETHING?

The SUPABASE_ prefix is reserved for the variables Supabase injects, such as SUPABASE_URL and SUPABASE_SECRET_KEYS. The dashboard and the Management API reject it. Lovable also reserves SUPABASE_ and LOVABLE_.

Is my key safe just because it's in an Edge Function secret?

The key itself is, but the function might not be. If anyone can call the function, anyone can use your OpenAI credit through it. Leave verify_jwt on (the default) for functions your users call, and only turn it off for webhooks that check their own signature.

In Lovable, should my API key go in Secrets or .env?

Secrets. Lovable says its Secrets are injected into Edge Functions and never reach the browser, while VITE_ values in .env are built into the browser code. Lovable even rejects VITE_ names in Secrets. Lovable also says not to gitignore .env in a Lovable project, so a secret key in .env ends up committed and public.

How do I check my key is no longer in my live app?

Open your live site, open DevTools, go to Sources (or Network, filtered to JS) and search the loaded files for the start of the key, like sk- or sk_live_. Or paste your URL into PatchPaste's free scan, which searches the JavaScript your site sends for Stripe, OpenAI, Anthropic, AWS, GitHub and Supabase admin keys.

My key was in the frontend before. Is moving it enough?

No. Anyone who loaded your site before the fix may have copied it. After the new function works, create a new key at the provider, save it as the secret, and delete the old key.

Is a secret key still in your live app?

Paste your URL. PatchPaste searches the JavaScript your site sends to every visitor for Stripe, OpenAI, Anthropic, AWS, GitHub and Supabase admin keys (shown masked), and checks common Supabase tables, exposed files and headers. The report is free.

Free. No login. No code access.

Want to see one first? View a sample report.