© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•3y ago•
2 replies
Deleted User

supabase-js createServerClient doesn't work with service role?

I can use the service role in a NextJS Route Handler to bypass RLS. This code works:

import { createClient } from "@supabase/supabase-js";

export async function POST(request: Request) {
  const adminClient = createClientJS(projectUrl, serviceRole);
  const res = await adminClient
    .from("profiles")
    .update({ credits: 123 })
    .eq("id", userId)
    .select();
  console.dir(res, { depth: null });
import { createClient } from "@supabase/supabase-js";

export async function POST(request: Request) {
  const adminClient = createClientJS(projectUrl, serviceRole);
  const res = await adminClient
    .from("profiles")
    .update({ credits: 123 })
    .eq("id", userId)
    .select();
  console.dir(res, { depth: null });


If I follow the docs for NextJS (https://supabase.com/docs/guides/auth/server-side/creating-a-client?environment=route-handler)

It works when using the anon key but not the service role key:

import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { cookies } from 'next/headers'

export async function POST(request: Request) {
  const cookieStore = cookies();
  const adminClient2 = createServerClient(projectUrl, serviceRole, {
    cookies: {
      get(name: string) {
        return cookieStore.get(name)?.value;
      },
      set(name: string, value: string, options: CookieOptions) {
        cookieStore.set({ name, value, ...options });
      },
      remove(name: string, options: CookieOptions) {
        cookieStore.set({ name, value: "", ...options });
      },
    },
  });

  const res = await adminClient2
    .from("profiles")
    .update({ credits: 123 })
    .eq("id", userId)
    .select();
  console.dir(res, { depth: null });
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { cookies } from 'next/headers'

export async function POST(request: Request) {
  const cookieStore = cookies();
  const adminClient2 = createServerClient(projectUrl, serviceRole, {
    cookies: {
      get(name: string) {
        return cookieStore.get(name)?.value;
      },
      set(name: string, value: string, options: CookieOptions) {
        cookieStore.set({ name, value, ...options });
      },
      remove(name: string, options: CookieOptions) {
        cookieStore.set({ name, value: "", ...options });
      },
    },
  });

  const res = await adminClient2
    .from("profiles")
    .update({ credits: 123 })
    .eq("id", userId)
    .select();
  console.dir(res, { depth: null });


The
console.dir
console.dir
returns a status of 200 and no errors but the database isn't updated.

I guess it doesn't really make sense to use the user's cookies as these aren't relevant when using the service role. Should I just use
createClient
createClient
as I did in my first code example?
Creating a Supabase client for SSR | Supabase Docs
Configure Supabase client to use Cookies
Creating a Supabase client for SSR | Supabase Docs
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
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Supabase start doesn't work
SupabaseSSupabase / help-and-questions
8mo ago
JS package, createServerClient deprecated?
SupabaseSSupabase / help-and-questions
2mo ago
createServerClient
SupabaseSSupabase / help-and-questions
14mo ago
Can't read tables with service role
SupabaseSSupabase / help-and-questions
6mo ago