Deutsch
Error messageSupabase · API keys

Fix Supabase “Invalid API key” (401)

Why Supabase answers 401 Invalid API key, what the three hints mean, the four usual causes and how to fix it without putting the secret key in your frontend.

The message you seeSupabase API (PostgREST) · 401

Invalid API key

By Tim GeithnerPublished 6 min read

Short answer

Supabase’s API gateway checked the apikey your app sent and found no active key of the project behind that URL, so it stopped the request with 401 before it reached your database. The usual causes: a key from another project, a key that was rotated or disabled, a production build that uses a different value than your local one, or extra characters copied with the key. Paste your key into the Supabase key checker to see which key and project it is, then fix the value where your deployed app gets it. Never fix it by using the secret or service_role key in the browser.

Ask an AI about this page:ChatGPTClaude

On this page

Every request from your app to Supabase carries an API key in the apikey header. Supabase’s API gateway checks that key before the request reaches the database, Auth or Storage. If it is not an active key of the project behind the URL you called, the gateway answers 401 with Invalid API key. Nothing was read or written. The key itself is not secret (the publishable or anon key is meant to sit in your frontend), so the fix is never about hiding it. It is about sending the right one.

The full answer looks like this. The hint tells you what kind of key arrived:

The response from Supabase
{
  "message": "Invalid API key",
  "hint": "Double check the provided API key for typos. This API key might also be owned by another Supabase project."
}

We sent wrong keys to a real Supabase project on 4 December 2026 and saw three hints. A key in the new format (sb_publishable_… or sb_secret_…) that the project doesn’t know gets the hint above. A legacy JWT key or anything else gets Double check your Supabase `anon` or `service_role` API key. A valid key with extra text stuck to its end got Double check your API key. If you see No API key found in request instead, no key arrived at all.

Find your cause in 30 seconds

What happenedMost likely cause
You have more than one Supabase project, or copied the key from a chat or old fileThe key belongs to another project
It worked before, then someone rotated keys or migrated to the new key formatThe key was rotated or disabled
It works locally, fails on the deployed siteThe deployed build has a different value
The key was pasted into a hosting dashboard or a config file by handExtra characters were copied with the key

The four causes

1. The key belongs to another project

The URL and the key must come from the same project. A test project and a live project, an old prototype, or a key copied from someone else’s tutorial: each has its own keys, and each project rejects the others’. Paste your key into the Supabase key checker. For a legacy anon key it shows the project URL the key belongs to; compare it with the URL your app calls. The new sb_publishable_… keys don’t name their project, so compare them with Settings → API Keys of the project you expect.

2. The key was rotated or disabled

Keys stop working when they are replaced. After a migration to the new publishable and secret keys, Supabase lets you disable the legacy anon and service_role keys under Settings → API Keys. Every app, script or webhook that still sends a legacy key then fails. Supabase notes that this step is reversible, so you can re-activate the legacy keys while you update a client you missed. The same goes for any key you deleted or replaced, after a leak for example. Use the current key from Settings → API Keys.

3. The deployed build has a different value

Locally, your app reads .env. On Vercel, Netlify or another host, it reads the variables set on that platform, and Vite writes them into the JavaScript bundle at build time. So the live site can carry an old key or another project’s key while your laptop has the right one. Changing the variable is not enough on its own: Vercel, for example, applies changes only to new deployments. If the variable is missing entirely, supabase-js stops earlier with supabaseKey is required. instead of this error.

4. Extra characters were copied with the key

A Supabase key only contains letters, digits, _, - and dots. Quotes pasted into a hosting dashboard’s value field, a literal \n at the end, or a key that was cut off when copying all make it a different key. In our tests, a key wrapped in quotes or with \n appended was rejected with this error. A trailing space was not, because HTTP trims it.

How to fix it

  1. Find out which key you have. Paste it into the Supabase key checker. It runs in your browser and sends nothing. It tells you whether the key is a publishable, anon, secret or service_role key, and for legacy keys which project it belongs to.
  2. Get the right key. In the Supabase dashboard of the project whose URL your app uses, open Settings → API Keys and copy the publishable key (sb_publishable_…), or the legacy anon key if you haven’t switched yet. Copy the project URL from the same project.
  3. Set it where the deployed app reads it. Put both values into your hosting platform’s environment variables, for example VITE_SUPABASE_URL and VITE_SUPABASE_PUBLISHABLE_KEY, without quotes. Then deploy again so the new values are built into the app.
  4. Test the key outside your app with the command under “Check that the fix works”.

If this keeps happening, let the app tell you at startup what is wrong with its configuration, instead of failing on the first request:

Safesrc/lib/supabase-config.ts
/** Says what is wrong with the Supabase URL and key a build uses. An empty list means: looks fine. */
export function checkSupabaseConfig(url: string | undefined, key: string | undefined): string[] {
  if (!url || !key) return ["The Supabase URL or key is missing in this build."];
  const problems: string[] = [];
  if (!/^[A-Za-z0-9._-]+$/.test(key)) {
    problems.push("The key contains spaces, quotes or line breaks. Copy it again without them.");
  }
  if (key.includes("sb_secret_")) {
    problems.push("This is a secret key. It must never be in browser code.");
  }
  if (key.startsWith("eyJ")) {
    let claims: { role?: string; ref?: string } = {};
    try {
      claims = JSON.parse(atob((key.split(".")[1] ?? "").replace(/-/g, "+").replace(/_/g, "/")));
    } catch {
      problems.push("The key cannot be read. It was probably cut off when copying.");
    }
    if (claims.role === "service_role") {
      problems.push("This is the service_role key. It must never be in browser code.");
    }
    const host = URL.canParse(url) ? new URL(url).hostname : "";
    if (claims.ref && host.endsWith(".supabase.co") && host !== `${claims.ref}.supabase.co`) {
      problems.push(`The key belongs to project ${claims.ref}, the URL to ${host}.`);
    }
  }
  return problems;
}
Safesrc/main.tsx
import { checkSupabaseConfig } from "./lib/supabase-config";

const problems = checkSupabaseConfig(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY,
);
if (problems.length > 0) console.error("Supabase configuration:", problems.join(" "));

Use the variable names your project already uses. The check only reads the public key and the URL, which are in your bundle anyway.

Fix the Supabase Invalid API key error

My app gets 401 “Invalid API key” from Supabase. Find where the Supabase client is created and which environment variables hold the project URL and the key (for example VITE_SUPABASE_URL and VITE_SUPABASE_PUBLISHABLE_KEY), in .env files and in the code. Tell me which variables the deployed build reads and which values the code falls back to. Check that the key is a publishable (sb_publishable_) or legacy anon key, has no quotes, spaces or \n, and that no hard-coded key or URL from another project is left in the code. Add a startup check that logs what is wrong with the URL and key, without logging the key itself.

Do not use the secret key (sb_secret_) or the service_role key anywhere in frontend code or VITE_ variables, and do not disable row level security. I will copy the correct key from Settings → API Keys and set it in the hosting platform myself; tell me which variable names to use.

Check that the fix works

Test the key outside your app, against the project URL your app uses. The Auth health endpoint answers anyone with a valid key:

Check: does Supabase accept this key?
curl -s "https://YOUR_PROJECT_REF.supabase.co/auth/v1/health" \
  -H "apikey: YOUR_PUBLISHABLE_KEY"
  1. A valid key returns 200 and a short JSON that names GoTrue, Supabase’s Auth server. Invalid API key means the key and the URL still don’t belong together.
  2. Open your deployed site, reload it, and watch the network tab: requests to supabase.co must no longer answer 401.
  3. Paste the key from the deployed build into the key checker once more: it must say publishable or anon, never secret or service_role. Then check that every table is protected, because that public key is all a visitor needs; the guide to row level security shows how.

Frequently asked questions

Should I use the service_role or secret key instead, since that one works?

No. Those keys skip row level security. In browser code, every visitor can copy them and read, change or delete all your data. The new secret keys don’t even work in a browser: Supabase answers 401 when it sees a browser. Fix the public key instead.

It works locally but not on my deployed site. Why?

Your local build reads .env, the deployed build reads the variables of your hosting platform. They can hold different values, and Vite bakes them into the bundle at build time. Set the right value on the platform and deploy again; Vercel, for example, applies changed variables only to new deployments.

How is this different from “No API key found in request”?

That message means no key arrived at all: the apikey header is missing. Supabase’s new keys belong in the apikey header, not only in Authorization: Bearer. With supabase-js you don’t set either by hand; pass the key to createClient.

Can I see which project a key belongs to?

For legacy anon and service_role keys, yes: they are JWTs that name the project’s ref. The Supabase key checker decodes that in your browser without sending anything. The new sb_publishable_ keys don’t name their project, so compare them with Settings → API Keys of the project you expect.

Sources

  1. 1.Understanding API keys — Supabase Docs
  2. 2.Migrating to publishable and secret API keys — Supabase Docs
  3. 3.Env variables and modes — Vite
  4. 4.Environment variables — Vercel Docs
  5. 5.JavaScript: Initializing — Supabase Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt