S
Supabase2w ago
Joel

postgrest exception: Function is restricted to service role

I'm currently using supabase-py version 2.18 and with the following setup:
url = os.environ["SUPABASE_URL"]
key = os.environ["SUPABASE_SERVICE_ROLE_KEY"]

return create_client(url, key)
url = os.environ["SUPABASE_URL"]
key = os.environ["SUPABASE_SERVICE_ROLE_KEY"]

return create_client(url, key)
I'm getting the following error:
postgrest.exceptions.APIError: {'message': 'upsert_batch_for_city_week is restricted to service role', 'code': 'P0001', 'hint': None, 'details': None}
postgrest.exceptions.APIError: {'message': 'upsert_batch_for_city_week is restricted to service role', 'code': 'P0001', 'hint': None, 'details': None}
for a function that has the following guard:
IF auth.role() IS DISTINCT FROM 'service_role' THEN
RAISE EXCEPTION '...';
END IF;
IF auth.role() IS DISTINCT FROM 'service_role' THEN
RAISE EXCEPTION '...';
END IF;
I've logged the url and key and confirmed that the values are correct. I've also tried providing the key as a Bearer token in the authorization header but that returns a different error.
What am I missing here to initialize a client with the service role and run this function?
2 Replies
garyaustin
garyaustin2w ago
Is that the error message from your function? Look in the API Gateway log. It shows the role making the call.
Joel
JoelOP2w ago
Thanks for pointing that out, new to Supabase and wasn't aware that was something I could look at. I was getting a 400 error and after removing a duplicate function with different params which I suspect was being called it now works.

Did you find this page helpful?