S
Supabase2mo ago
tomi

Invalid API Key Next.js

Hello all, I'm trying to setup an 'invite by emai' function in my Next.js app with Supabase auth (admin) from @supabase-js but I keep getting the error: Error [AuthApiError]: Invalid API Key I don't believe this to be true as all my CRUD functions run properly on the same DB. I would also like to add that this only happens when I try to connect to the auth client. When I use the login/sign in from supabase auth (not admin), I don't get this. I've tried pasting both the service role and secret key directly into my function (bypassing the env), but I get the same error. The relevant code is:
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(
DB_URL,
SECRET_KEY / SERVICE_ROLE_KEY,
{
auth: {
persistSession: false,
autoRefreshToken: false,
},
}
);
const { data, error } = await supabase.auth.admin.inviteUserByEmail(email, {
data: {
role: role,
},
redirectTo: "/auth/confirm",
});
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(
DB_URL,
SECRET_KEY / SERVICE_ROLE_KEY,
{
auth: {
persistSession: false,
autoRefreshToken: false,
},
}
);
const { data, error } = await supabase.auth.admin.inviteUserByEmail(email, {
data: {
role: role,
},
redirectTo: "/auth/confirm",
});
I'd really appreciate any help here.
1 Reply
tomi
tomiOP2mo ago
After a few more hours of digging, I saw that there are essentially two different URLs I should be using: The one in this guide https://supabase.com/docs/guides/auth/server-side/nextjs for non-admin functions and the one with my project id for the admin functions. I'm not sure that this was explicit in the documentation.

Did you find this page helpful?