DEV Community

Cover image for How to create API keys in Supabase for roles other than "anon" and "service"?
acetrondi
acetrondi

Posted on • Edited on

4

How to create API keys in Supabase for roles other than "anon" and "service"?

If you're reading this post, you may already be familiar with Supabase. However, for those who are new, let me provide a brief introduction.

Supabase is an alternative to Firebase that utilizes PostgreSQL as its database and offers various features, including authentication, real-time capabilities, and storage.

To get started with Supabase, follow these steps:

  1. Obtain the JWT key from the Supabase dashboard or through this link.
    Obtain Jwt token/secret from supabase dashboard

    Never disclose your Jwt secret/token in public

  2. Create a role in your SQL editor:

CREATE ROLE your_role;
GRANT your_role TO authenticator;
-- grant role privileges here 
Enter fullscreen mode Exit fullscreen mode

3.Visit jwt.io and populate the payload field with the following information:

Enter data in payload field

    {
      "iss": "supabase",
      "ref": "project ref id",
      "role": "your_role",
      "exp": 2001128702
    }
Enter fullscreen mode Exit fullscreen mode

Replace ref with your project reference ID from Dashboard

4.In the "Verify Signature" field, enter the JWT Token/Secret obtained in step 1.

Note: Default algorithm for JWT token is "HS256"

//Header
{
  "alg": "HS256",
  "typ": "JWT"
}
Enter fullscreen mode Exit fullscreen mode

Enter your Jwt token in Verify Signature

5.The "Encoded/Token" field will display the newly generated token. Copy this token and include it in your REST API or client code and requests as Authorization: Bearer new_generated_token. This token will have all the privileges you gave to your_role role from sql editor in supabase dashboard.

Fetch/Copy newly generated token from the Encoded field

Note: Please exercise caution when assigning sensitive data as this role will have the privileges you grant it.

Peace!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay