Supabase Edge Function 401 “Invalid JWT” or “Missing authorization header”

By Abheesht Tripathi · Published

401 Unauthorized  ·  Invalid JWT  ·  Missing authorization header
(newer projects: Invalid Token or Protected Header formatting)

What it means

By default every Edge Function has verify_jwt turned on. The platform checks the Authorization header before your code runs and answers 401 itself if the header is missing, malformed, expired, or signed by a key it doesn't accept. Your function never started, so your logs show nothing and your own error handling never ran.

Tell the two cases apart by the body: the messages above come from the platform. A 401 with your own message (or an empty body) came from your code.

The usual causes

  1. No header at all. A raw fetch, a curl test, or a webhook from Stripe, Razorpay, GitHub or a cron service. Those callers don't have a Supabase user token.
  2. An expired user token that your code cached instead of asking supabase-js for the current session.
  3. A token from another project: staging token, production function.
  4. Key format mismatch. Supabase's 401 troubleshooting guide lists projects on the newer asymmetric JWT signing keys / API keys whose callers send something the legacy check doesn't understand. Its recommended fix is to turn the built-in check off and verify in your code.

Fix for browser calls: send the user's token

supabase.functions.invoke() attaches the signed-in user's access token and the apikey header for you:

const { data, error } = await supabase.functions.invoke('create-checkout', {
  body: { plan: 'pro' },
});

If you use fetch, get the session right before the call. Send API keys on the apikey header and keep Authorization for user tokens:

const { data: { session } } = await supabase.auth.getSession();
await fetch(`${SUPABASE_URL}/functions/v1/create-checkout`, {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${session.access_token}`,
    apikey: SUPABASE_PUBLISHABLE_KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ plan: 'pro' }),
});

Fix for webhooks and cron: turn the check off, verify differently

A payment webhook can never send a Supabase JWT. Turn verify_jwt off for that function only, and verify the provider's signature in your code instead:

# supabase/config.toml
[functions.stripe-webhook]
verify_jwt = false

or deploy with supabase functions deploy stripe-webhook --no-verify-jwt. The value in config.toml is applied on deploy, so a dashboard toggle can be overwritten by your next deploy: keep the setting in the file.

If you turn it off for user-facing functions, check the user yourself

With verify_jwt = false nothing checks the caller any more unless your code does. Verify the token with Supabase Auth and use a client that runs as that user:

import { createClient } from 'npm:@supabase/supabase-js@2';

Deno.serve(async (req) => {
  const authHeader = req.headers.get('Authorization') ?? '';
  const supabase = createClient(
    Deno.env.get('SUPABASE_URL')!,
    Deno.env.get('SUPABASE_ANON_KEY')!,
    { global: { headers: { Authorization: authHeader } } },
  );
  const token = authHeader.replace('Bearer ', '');
  const { data: { user }, error } = await supabase.auth.getUser(token);
  if (error || !user) {
    return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 });
  }
  // queries on `supabase` now run as this user, under RLS
  return Response.json({ ok: true, user_id: user.id });
});

Decoding the JWT without verifying its signature is not a check: anyone can make a token with any sub.

How to verify

# without a token: 401 from the platform (verify_jwt on) or from your code (off)
curl -i -X POST https://<project-ref>.supabase.co/functions/v1/create-checkout

# with a real user token: your function runs and its logs show the request
curl -i -X POST https://<project-ref>.supabase.co/functions/v1/create-checkout \
  -H "Authorization: Bearer <user-access-token>" \
  -H "apikey: <publishable-key>" \
  -H "Content-Type: application/json" -d '{"plan":"pro"}'

For a browser app, also return CORS headers on every response, including the 401 your code returns, or the browser will show a CORS error instead.

Related guides

More in Edge Functions, webhooks and APIs · all guides