All jobs

Add an Edge Function to a Supabase Project

Yes. An agent scaffolds a Supabase Edge Function that reads the caller's own rows under row level security, tests it on the local stack, and opens a pull request.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the project as it is. In a repository shaped like Razikus/supabase-nextjs-template it finds supabase/config.toml with project_id set, the API on port 54321, the database on 54322 at Postgres major version 15, and an [edge_runtime] section already enabled with the oneshot policy and an inspector port, but no supabase/functions directory and no [functions.<name>] block. It reads the four migrations: 20250107210416_MFA.sql creates the authenticative.is_user_authenticated() helper that requires aal2 when the user has a verified MFA factor, 20250130165844_example_storage.sql and 20250130181110_storage_policies.sql set up a private files bucket with per-user folder policies, and 20250130181641_todo_list.sql creates public.todo_list with title, urgent, description, done, done_at and an owner foreign key to auth.users, enables row level security and adds one policy, Owner can do everything, for authenticated users whose owner is auth.uid(). On the Next.js side nextjs/src/lib/supabase/unified.ts wraps the client and src/app/app/table/page.tsx lists and updates todos in the browser.

The function the prompt asks for is a summary endpoint: GET /functions/v1/todo-summary returning the caller's open, urgent and done counts. The agent runs supabase functions new todo-summary, which creates supabase/functions/todo-summary/index.ts, and writes a Deno handler that reads the Authorization header, creates a Supabase client with SUPABASE_URL and SUPABASE_ANON_KEY from the function's environment and that header forwarded as the global Authorization, and queries todo_list three times with count: 'exact', head: true. Because the client carries the user's JWT and not the service role key, the Owner can do everything policy applies inside the function exactly as it does in the browser, and the is_user_authenticated() check means an MFA-enrolled user who has not completed the second factor gets zero rows rather than everyone's. The agent adds [functions.todo-summary] with verify_jwt = true to config.toml so the gateway rejects a request with no valid token before the code runs.

It proves the function against the local stack. In the sandbox it runs supabase start, which applies the four migrations, then supabase functions serve todo-summary, signs up a user through the local auth API, inserts two todos as that user, and calls the function with the user's access token to show counts of two open and zero done, then calls it with no token to show the 401 from verify_jwt. It writes those calls into supabase/functions/todo-summary/test.sh so a reviewer can repeat them, and adds a deno.json beside the function with an imports map pinning @supabase/supabase-js so the import does not float.

The Next.js app is not changed; the function is callable from src/lib/supabase/unified.ts with functions.invoke('todo-summary') through the existing client, and the agent notes that one-liner in the pull request rather than adding UI the prompt did not ask for. It runs deno check on the function file and deno fmt --check, and opens a pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add a Supabase Edge Function named todo-summary to this project. It should answer GET with the calling user's counts of open, urgent and done rows in public.todo_list, using a client built from the request's Authorization header so row level security applies, never the service role key. Register it in supabase/config.toml with verify_jwt = true, pin supabase-js in a deno.json import map, start the local stack, serve the function, sign up a test user, insert rows, and show the counts and the 401 without a token. Save those calls as a test script beside the function and open a pull request. Do not change the Next.js app.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on its own branch. The diff adds supabase/functions/todo-summary/index.ts, a deno.json beside it with the pinned import, a test.sh with the local calls, and a [functions.todo-summary] block in supabase/config.toml. Nothing under nextjs/ and nothing under supabase/migrations changes, because the function only reads a table and a policy that already exist. The description shows the JSON the function returns and the 401 body from the gateway, and names the one line that calls it from the existing client wrapper.

The proof is the trajectory at GET /o/{org}/sessions/{id}/messages: the supabase start output with the migrations applied, the functions serve log, the sign-up and insert calls, and the two curl responses. The repository has no CI workflow of its own, so the sandbox run is the check until someone deploys the function with supabase functions deploy.

Before merging, a reviewer checks that the client is built from the request's Authorization header and SUPABASE_ANON_KEY, never from SUPABASE_SERVICE_ROLE_KEY, so the Owner can do everything policy and the MFA helper decide what is visible; that verify_jwt is true in config.toml; and that the response contains counts only, never row contents. The function is not deployed by the run; it exists on the branch until the team pushes it with the Supabase CLI.

Run it on every new Supabase project

A team that runs one Supabase project per product starts one run per repository with POST /o/{org}/sessions, this prompt and that repository in resources. Each run reads its own config.toml and its own migrations, so a project whose table is named differently or whose policy uses a different helper gets a function written against what is actually there, and each opens its own pull request. Send a request_id per run so a retry returns the run already started.

Set a webhook_url on the API key and every run the key starts posts run.completed or run.failed when it finishes, with a session_url for the result, so a script can collect the pull request links. The key runs at most max_concurrent sessions at once, 4 by default; a session beyond that is still created, answered 202 with status: "queued", and started when a slot frees.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

An Edge Function is a few dozen lines, and the part that takes the afternoon is proving it respects row level security: a local Supabase stack in Docker, the migrations applied, a real user signed up, rows inserted as that user, and the function called with and without a token. On Rush the run has its own sandbox with a computer in it, so the agent runs supabase start and the whole sequence there, as many times as it needs, and your laptop's Docker and your .env stay untouched.

The result is checkable rather than claimed. The pull request has the function, the config block and a test script that repeats the proof, and the trajectory keeps the local stack's output and both curl responses so a reviewer can read the 401 and the counts before merging. Several projects mean several sandboxes and several pull requests at once.

Frequently asked questions

Why not use the service role key inside the function?

Because then the function would see every user's rows and would have to reimplement the ownership rule by hand. Forwarding the caller's Authorization header to a client built on the anon key lets the Owner can do everything policy in 20250130181641_todo_list.sql and the is_user_authenticated() MFA helper do that work, the same way they do for the browser.

What does verify_jwt change?

With verify_jwt = true in the function's config.toml block, the gateway rejects a request with a missing or invalid token before the code runs, so the handler never has to deal with an anonymous caller. The test script's no-token call shows that 401.

Does the Next.js app need to change to call it?

No. The existing client in nextjs/src/lib/supabase/unified.ts already carries the user's session, so functions.invoke('todo-summary') on it sends the right header. The agent leaves the app alone because the prompt says so, and names that call in the pull request for whoever adds the UI.

How is the function tested without a deployed project?

Entirely against the local stack: supabase start applies the migrations from supabase/migrations, supabase functions serve runs the function on the local edge runtime the [edge_runtime] section already enables, and the test script signs up a user and inserts rows through the local API. Nothing in the run touches a hosted project.

Can it be scheduled instead of called on demand?

Yes, in a follow-up. A scheduled job would call a function with a service token on a timer, and that changes the security model, since there is no caller whose header can be forwarded. The agent would write a separate function for it rather than loosen this one.

More Supabase jobs

More jobs like this: Hand off a long task

Keep reading