Back to Rivet

Supabase Functions Quickstart

website/src/content/docs/actors/quickstart/supabase.mdx

2.3.22.7 KB
Original Source

Set up a Rivet project locally that runs on Supabase Edge Functions. The @rivetkit/supabase package wires the WebAssembly runtime for you.

<Note> Prefer to start from a complete project? See the runnable [`hello-world-supabase-functions`](https://github.com/rivet-dev/rivet/tree/main/examples/hello-world-supabase-functions) example. </Note>

Steps

<Steps> <Step title="Prerequisites">

The CLI runs the local Rivet engine as a bundled native binary, so Docker is only needed for Supabase itself. A Supabase project is only needed to deploy.

</Step> <Step title="Create the Function">
sh
npx supabase functions new rivet

Add the packages used by the function:

sh
npm install rivetkit @rivetkit/supabase
</Step> <Step title="Configure the Function">

Call serve from @rivetkit/supabase. It loads the WebAssembly runtime and serves the Rivet handler.

ts
import { actor } from "rivetkit";
import { serve, setup } from "@rivetkit/supabase";

const counter = actor({
  state: { count: 0 },
  actions: {
    increment: (c, amount = 1) => {
      c.state.count += amount;
      return c.state.count;
    },
  },
});

// `setup` returns a typed registry, so a client can type itself with
// `typeof registry`.
export const registry = setup({ use: { counter } });

await serve(registry);
</Step> <Step title="Run Locally">

Start Rivet. The CLI runs the local engine, spawns supabase functions serve for you, and populates the connection values:

sh
npx @rivetkit/cli dev --provider supabase

Visit http://localhost:6420 in your browser (or point your AI agent at it) to open the Rivet developer tools and inspect your actors live.

</Step> <Step title="Call the Actor">

Connect to your actor from a client. This connects directly to the local engine on http://localhost:6420:

ts
import { createClient } from "rivetkit/client";
import type { registry } from "./supabase/functions/rivet/index";

const client = createClient<typeof registry>("http://localhost:6420");

const counter = client.counter.getOrCreate(["my-counter"]);
const count = await counter.increment(3);
console.log("New count:", count);

See the JavaScript client documentation for more information.

</Step> <Step title="Deploy">

Ready to ship? See Deploying to Supabase Functions.

</Step> </Steps>