© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•13mo ago•
5 replies
Floppy Disk

Next JS Service Client

I wanted to create Service Role client in NextJS app following how regular client is being created, I only replaced
Anon
Anon
key with
Service
Service
. This doesn't seem to work, thought it's because of cookies being set but seems these are necessary. Any help?

export const createServiceClient = () => {
  const cookieStore = cookies();

  return createServerClient<Database>(
    env.NEXT_PUBLIC_SUPABASE_URL!,
    env.SUPABASE_SERVICE_ACCOUNT_KEY!,
    {
      cookies: {
        getAll() {
          return cookieStore.getAll();
        },
        setAll(cookiesToSet) {
          try {
            cookiesToSet.forEach(({ name, value, options }) => {
              cookieStore.set(name, value, options);
            });
          } catch (error) {
            // The `set` method was called from a Server Component.
            // This can be ignored if you have middleware refreshing
            // user sessions.
          }
        },
      },
    },
  );
};
export const createServiceClient = () => {
  const cookieStore = cookies();

  return createServerClient<Database>(
    env.NEXT_PUBLIC_SUPABASE_URL!,
    env.SUPABASE_SERVICE_ACCOUNT_KEY!,
    {
      cookies: {
        getAll() {
          return cookieStore.getAll();
        },
        setAll(cookiesToSet) {
          try {
            cookiesToSet.forEach(({ name, value, options }) => {
              cookieStore.set(name, value, options);
            });
          } catch (error) {
            // The `set` method was called from a Server Component.
            // This can be ignored if you have middleware refreshing
            // user sessions.
          }
        },
      },
    },
  );
};
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

RLS Connection Error with Next.js + Prisma (service_role)
SupabaseSSupabase / help-and-questions
7mo ago
React/Next.js
SupabaseSSupabase / help-and-questions
3y ago
Confusion when combining createServerSupabaseClient and clientside auth helpers (Next.js).
SupabaseSSupabase / help-and-questions
4y ago
Next.js SSR RLS
SupabaseSSupabase / help-and-questions
13mo ago