Skip to content
>_VibeUnstuck

Missing publishableKey

Last verified:

The fix

Missing publishableKey. Clerk has no publishable key at render. Set NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY to a pk_test_ or pk_live_ key, and set CLERK_SECRET_KEY on the server. Restart or redeploy.

  1. 1

    Put both keys in the env file the framework reads

    For Next.js, set NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY in .env.local. Development keys are pk_test_ and sk_test_. Production keys are pk_live_ and sk_live_. Clerk's CLI can write them with npx clerk@latest env pull for an existing app. Do not commit the file. Do not prefix the secret with NEXT_PUBLIC_.

  2. 2

    Restart, or redeploy

    Next.js reads env files at process start. Saving .env.local while next dev is running does not update NEXT_PUBLIC_ values. Stop it and start it again. On Vercel, add both names for the environment you are opening, use the development instance on preview and the production instance on production, then redeploy. A previous deployment keeps the empty value.

  3. 3

    Pass the key only if you are not using the default name

    ClerkProvider picks up NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY by itself. Pass publishableKey when the variable has another name. An empty expression still throws. Do not hardcode the key in the layout.

    <ClerkProvider publishableKey={process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY}>
      {children}
    </ClerkProvider>
  4. 4

    Do not add clerkMiddleware to fill in a missing key

    Middleware does not invent the publishable key. If the error is auth() was called but Clerk can't detect usage of clerkMiddleware(), the keys may already be set and the matcher is the bug. Fix that page. Do not paste sk_live_ into the client to get past either message.

Why it happens

Clerk's client reads the publishable key before it renders. When that value is missing, the thrown text includes Missing publishableKey. One long-standing sentence is @clerk/nextjs: Missing publishableKey. You can get your key at https://dashboard.clerk.com/last-active?path=api-keys. Newer releases phrase the same gap as the keys missing from the environment. The fix is the variables, not a different provider.

Next.js only inlines a public env var that is present when the process starts, and the name has to be NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY. The value starts with pk_test_ for a development instance and pk_live_ for production. Vite uses VITE_CLERK_PUBLISHABLE_KEY. The secret is CLERK_SECRET_KEY, with sk_test_ or sk_live_, and it must not use a public prefix. auth() failing because clerkMiddleware never ran is the other page. A key that is present and then says the middleware was not detected is that page, not an empty variable.

The next error you'll probably hit

Once this is fixed, the usual follow-up is auth() was called but Clerk can't detect usage of clerkMiddleware(). auth() ran on a request that never passed through clerkMiddleware(). The file is in the wrong folder, the matcher skipped the route, or proxy.ts does not export the middleware.

Prompt to paste back into your AI tool

Paste this above the error.

Clerk throws: Missing publishableKey. The full sentence is:
<paste>
The framework is <Next.js or Vite>. The env file we edited is <path>.

Do not commit the key. Do not put CLERK_SECRET_KEY in a NEXT_PUBLIC_ or VITE_ variable. Do not hardcode pk_ or sk_ in a component. Do not add clerkMiddleware as a substitute for the key.

1. For Next.js, require NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY (pk_test_ or pk_live_) and CLERK_SECRET_KEY (sk_test_ or sk_live_). For Vite, the public name is VITE_CLERK_PUBLISHABLE_KEY.
2. Tell me to restart the dev server, or to set both on the host and redeploy. Use development keys on preview and production keys on production.
3. If the error is instead that auth() can't detect clerkMiddleware, stop. That is a different page.
4. Do not print the key values.

Common questions

+The key is in .env.local and the browser still says Missing publishableKey. Why?

The dev server was already running when the file changed, or the name is not NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY. Restart. A public variable with a typo is undefined in the client.

+Vercel has the variable and the deployed site still throws. Why?

The deployment was built before the variable existed, or it is set on a different environment than the URL you opened. Add it for that environment and redeploy. Preview should use the development instance keys.

Related errors