Socket
Book a DemoInstallSign in
Socket

@psteinroe/fastify-supabase

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@psteinroe/fastify-supabase

A Fastify plugin to use authenticated Supabase clients in your API.

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

Supabase Plugin for Fastify

A Fastify plugin to use authenticated Supabase clients in your API.

Latest build GitHub Stars

Installation

Install with your favorite package manager.

pnpm add @psteinroe/fastify-supabase

If your package manager does not install peer dependencies automatically, you need to install them too

pnpm add @fastify/jwt @supabase/supabase-js

Quick Start

First, register @fastify/jwt with your Supabase JWT secret. You can obtain it from Supabase Studio.

import fastifyJWT from "@fastify/jwt";

fastify.register(fastifyJWT, {
  secret: SUPABASE_JWT_SECRET,
});

Then, register the Supabase plugin.

import fastifySupabase from "@psteinroe/fastify-supabase";

fastify.register(fastifySupabase, {
  url: SUPABASE_URL,
  anonKey: SUPABASE_ANON_KEY,
  serviceKey: SUPABASE_SERVICE_KEY,
  // you can pass any `SupabaseClientOptions`
  options: {},
});

You can now access a SupabaseClient authenticated as the service role directly on the Fastify instance with fastify.supabaseClient.

To protect a route and use a user-authenticated SupabaseClient, create a onRequest route handler to verify the JWT. Note that this is a @fastify/jwt feature.

import { onRequestHookHandler } from "fastify";

export const verifyApiKey: onRequestHookHandler = async (request) => {
  await request.jwtVerify();
};

Now pass the hook to the route handler. You can now access either a client authenticated with the service role via the Fastify instance, or a user-authenticated client via the request. Thanks to @fastify/jwt, you can also access the Supabase User object on the request.

import { FastifyInstance } from "fastify";

import { verifyApiKey } from "../helpers/verify";

export default async function routes(fastify: FastifyInstance) {
  fastify.get("/health", {
    onRequest: [verifyApiKey],
    handler: async (request, reply) => {
      // authenticated as the user that is making the request
      const { data } = request.supabaseClient.from("article").select("*");

      // authenticated with service role
      const { data } = fastify.supabaseClient.from("article").select("*");

      // access the `User` object on the request
      const tenantId = request.user.app_metadata.tenantId;

      return reply.send("OK");
    },
  });
}

Thats it!

Fastify Supabase is created by psteinroe. Follow @psteinroe on Twitter for future project updates.

Keywords

Supabase

FAQs

Package last updated on 18 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts