---
title: "Recipe: a Supabase backend (Auth, tables, Storage, Functions)"
description: "Supabase's postgrest-js, storage-js and functions-js run in a data module as published."
modificationDate: "2026-11-22"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: a Supabase backend (Auth, tables, Storage, Functions)

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/supabase: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

Supabase's `postgrest-js`, `storage-js` and `functions-js` run in a data module
as published. Auth is four calls in your own `supabase.ts`, copied from `bun
exact.mjs docs supabase session` and the section after it; its `supabase(store,
now)` makes the clients.
`bun add @supabase/postgrest-js @supabase/storage-js @supabase/functions-js`.

```prompt Use Supabase as my app's backend
Add a Supabase backend (Auth, tables, Storage, Functions) to the Exact app in this folder. Use https://github.com/expo/exact/blob/main/docs/recipes/supabase.md as the source for each step. The Exact checkout has the same page at docs/recipes/supabase.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say which tables, buckets and functions the app uses, ask me before you write code. Ask me for my project's URL and publishable key: they are settings, and neither is a secret. Put no secret key in the app. The project setup is mine: read `bun exact.mjs docs supabase accounts` and tell me what the project needs.
3. Install the clients: `bun add @supabase/postgrest-js @supabase/storage-js @supabase/functions-js`. Do not add `supabase-js` or `auth-js`, and do not use Realtime: they do not run here (`bun exact.mjs docs supabase limits`).
4. Write `supabase.ts` beside app.ts: copy it whole from `bun exact.mjs docs supabase session` and the section after it. As that section says, declare `"env": ["SUPABASE_URL", "SUPABASE_KEY"]` in app.json, and put their values in `.env.local`.
5. In app.ts, write the grants, `net.fetch ${PROJECT}` and `secret.keep ${SESSION}`, and the sources as the recipe shows. In a read, throw a client's `error`, so the resource keeps what it showed. In a write, return the error in the reply.
6. In app.contract, pass time as an argument (`time.epochAtZero + performanceNow()`). Sign in from a mutation, never from a resource. Each read takes `session.expiresAt` after `with`, and each write takes `session.userId`. Keep the recipe's `renew` task: its minute tick refreshes the session while the app is open.
7. For sign-out, Sign in with Apple, the web's tokens or a file over 4 MiB, follow `bun exact.mjs docs supabase accounts` and the section after it. Tell me before you add Sign in with Apple: it needs settings in my project.
8. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. The recipe was tested against a loopback stand-in, not a live project: ask me before a test or a check writes to my project. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me the tables and sources, what the tests and checks reported, what you did not try against my live project, and what I must still do in my project.
```

```ts
import { session, signInAnonymously, supabase, PROJECT, SESSION } from './supabase.ts';
export const grants = `net.fetch ${PROJECT}\nsecret.keep ${SESSION}`;
// Throw a client's `error`, so a resource keeps what it showed.
const ok = <T,>({ data, error }: { data: T; error: unknown }): T => { if (error) throw error; return data; };
const sources: Sources = {
  currentSession: async ([now], store) => {
    const s = await session(store, now);
    return { userId: s?.user.id ?? '', anonymous: s?.user.is_anonymous ?? true, expiresAt: s?.expires_at ?? 0 };
  },
  signInAnonymously: async ([now], store) => { await signInAnonymously(store, now); return { ok: true, error: '' }; },
  notes: async ([user], store) => ok(await (await supabase(store, undefined, user)).db.from('notes').select('id,text').order('id')) ?? [],
  save: async ([text, user, now], store) => {
    const { db } = await supabase(store, now, user);
    const { error } = await db.from('notes').insert({ text });
    return { ok: !error, error: error?.message ?? '' };
  },
};
```

```text
  resource time = exactTime() as shape Time
  state now = 0
  resource session = currentSession() with time.epochAtZero + now as shape Session else empty(userId="", anonymous=true, expiresAt=0)
  resource notes = notes(session.userId) with session.expiresAt when session.userId != "" as shape Note[] else empty()
  mutation started as shape Reply refreshes session
  mutation saved as shape Reply queue refreshes notes
  task renew mount
    every(60000, tick)
  task anonymous when session.userId == "" and not pending(session)
    after(1, start)
  action start
    if not pending(started)
      send started = signInAnonymously(time.epochAtZero + performanceNow())
  action tick
    now = performanceNow()
  action save(text: string)
    send saved = save(text, session.userId, time.epochAtZero + performanceNow())
```

- **Sign in from a mutation.** A resource is asked again when what it reads
  changes, even mid-answer.
- **Time is an argument.** The minute tick refreshes the session while the
  app is open, and a write refreshes first; reads take `expiresAt` after
  `with`, so they ask again with the new token.
- **Writes take `session.userId`,** so a save queued before a sign-out is
  refused, not sent as the next user.
- Project setup, sign-out, Apple, the web's tokens and files over 4 MiB:
  `docs supabase accounts` and the section after it.

`supabase-js`, `auth-js` and Realtime do not run here: `docs supabase limits`.
Tested against a loopback stand-in on the web and Hermes, not a live project.
