Skip to main content
This route uses the reference write implementation. You deploy its write endpoint as a Supabase Edge Function, and your app sends writes to the function with the reference connector. Compared with the PostgREST route, you deploy and maintain one function, and in return each transaction is applied in one database transaction. The function runs each write as the signed-in user. Inside each transaction, it switches to the authenticated role and sets the user’s token claims. Policies that use auth.uid() therefore work the same way as for requests from the Supabase client SDKs.
Diagram of the Edge Function route. Inside Supabase, an Edge Function runs the reference write endpoint as the signed-in user and writes each transaction atomically to Postgres, where RLS policies apply. Supabase Postgres replicates to the PowerSync Service, which syncs to the SDK in the client app. The reference connector's uploadData() sends queued transactions to the Edge Function. Supabase Auth issues the JWTs that the Edge Function and PowerSync verify.

The Edge Function runs inside your Supabase project. Supabase Auth issues the tokens, and RLS policies authorize each write.

Before You Start

  • PowerSync connected to your Supabase project and configured for Supabase Auth. See the Supabase guide.
  • Your Supabase project signs tokens with asymmetric JWT signing keys, the default for new projects. Projects that use the legacy JWT secret must migrate first.
  • Each table that your app writes to has an id primary key or unique column that holds the client’s row ID.
  • An app that signs users in with Supabase Auth.
  • The Supabase CLI.

Set Up

1

Add Grants and RLS Policies

Grant the authenticated role access to every table that your app writes to, enable RLS on each one, and add a policy for each operation that your app uploads:
  • A PUT is an upsert, so it needs INSERT and UPDATE policies, and the existing row must be visible to the user.
  • A PATCH needs an UPDATE policy, and a DELETE needs a DELETE policy.
Use WITH CHECK so that users can’t assign rows to someone else. For example:
If authenticated has grants on a table that has RLS disabled, any signed-in user can write any row in it. Check every table in the schemas that your app writes to, not only the tables that you sync.
2

Deploy the Function

Clone powersync-reference-write-implementation. From its backend folder, link your project and deploy the function:
If Docker isn’t running, add --use-api to the deploy command.The function connects to your project’s database directly. Each function worker keeps up to three connections, and they all count against your database’s connection limit. If many workers run at once, set the function to use the transaction pooler. To configure it, copy the pooler connection string from Connect > Transaction pooler in the Supabase dashboard. URL-encode any special characters in the password and add sslmode=verify-full:
From this repository, run only supabase functions deploy powersync-write and supabase secrets set against your project. Other commands, such as supabase config push, can apply the repository’s local development config to your project.
To deploy from your own repository instead, see the repository’s Supabase Edge Function guide.
3

Check the Deployment

A request without a token returns 401.
4

Connect Your App

Copy the reference connector for your SDK into your app and pass it to connect(), as described in the reference write implementation’s Connect Your App step. Then make these changes to your copy:
  1. Set the write endpoint URL to https://<project-ref>.supabase.co/functions/v1/powersync-write. Your PowerSync instance URL doesn’t change.
  2. Replace fetchAuthToken() with code that returns the access token of the current Supabase session. The fetchCredentials() in the Supabase example for your SDK shows how to read it.
  3. Stop caching the token: return fetchAuthToken() directly from getAuthToken(), as the comments in the example describe. The Supabase client SDK refreshes the session itself. The connector clears its cache only after a write request fails authentication, so while the cache is in place, sync keeps sending an expired token.
For example, with supabase-js:
The function accepts only a signed-in user’s access token. It rejects API keys, anon tokens, and service-role tokens.

How Failures Are Handled

A policy can also hide a row from the user. A PATCH or DELETE on a hidden row succeeds without changing anything. The app’s local change reverts after the next sync. Because an RLS denial is retried, a mistake in a policy shows up as a growing upload queue and repeated uploadError values in the sync status. To stop one denied write from holding up the queue, add a retry limit to your copy of the connector. When a transaction reaches the limit, drop it or store it.

Customization

Edit these files in backend/supabase/functions/powersync-write/, then deploy again:

Before Production

Before you deploy the function for real users, review the production checklist. It covers authorization, demo authentication, and how failed writes are recovered.