SupabaseS
Supabase6mo ago
john

How to correctly use Service Role Client in Edge Functions with new JWT Signing Keys?

Hey Supabase community!

I'm migrating my project to the new JWT Signing Keys and have disabled the legacy anon and
service_role
keys as recommended. However, I'm running into an issue authenticating my Edge Functions to use the service role client.

  1. The Old (Working) Method:


Previously, we used the legacy keys:

export const SUPABASE = {
    URL: Deno.env.get("SUPABASE_URL")!,
    ANON_KEY: Deno.env.get("SUPABASE_ANON_KEY")!,
    SERVICE_ROLE_KEY: Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
};

createClient(SUPABASE.URL, SUPABASE.SERVICE_ROLE_KEY, { ... });


We could invoke the function with the anon-key and it worked perfectly.

  1. The New (Not Working) Method:


Now, with the new API Keys enabled, we've updated our configuration:

export const SUPABASE = {
    URL: Deno.env.get("SUPABASE_URL")!,
    PUBLISHABLE_KEY: Deno.env.get("SUPABASE_PUBLISHABLE_KEY")!,
    SECRET_KEY: Deno.env.get("SUPABASE_SECRET_KEY")!,
};


We configured it to be invocable without JWT as a backend function triggered by a cron job:

# supabase/config.toml
[functions.my-ef]
verify_jwt = false


We invoke it directly:

curl -X POST 'https://<project>.supabase.co/functions/v1/my-ef' -H 'Content-Type: application/json'


The Problem:
The function is invoked, but when getServiceRoleClient() is called inside it, we get error, because the new SUPABASE_SECRET_KEY environment variable doesn't seem to be populated in the Edge Function runtime:

Error: Supabase configuration is missing for Service Role Client.


A test function confirmed that
SUPABASE_SERVICE_ROLE_KEY
is present in the runtime (though being disabled and invalid), but SUPABASE_SECRET_KEY is missing.

My Question:

After disabling legacy API keys, what is the correct way to configure and initialize the service role client (
createClient
) inside an Edge Function? How do we access the new secret key from within the Deno runtime?

Thanks!
Was this page helpful?