Supabase Edge Functions

--platform supabase writes supabase/functions/<name>/ with index.ts, main.js and prelude.js. Deploy it with supabase functions deploy <name>. See Building. For a whole function from an empty directory, see A function from an empty directory.

The path

The platform gives the function its paths with the function's name in front (/functions/v1/api/books arrives as /api/books). The entry sets SERVER_CONTEXT_PATH to /<name>, so the mappings are those of a server: @GetMapping('/books').

The context starts while the instance loads.

The environment

The function's environment is the application's environment: a secret GREETING is ${GREETING}, and SPRING_SUPABASE_AUTH_ISSUER is spring.supabase.auth.issuer.

With the Supabase starter

With spring_boot_starter_supabase, spring.supabase.url, anon-key and service-role-key default to SUPABASE_URL, SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY, so the repositories (the REST path), Storage and the resource server need no configuration.

Only the REST path: a function has no sockets, so no spring.supabase.db.url, no @Transactional.

verify_jwt

Set verify_jwt = false for the function in supabase/config.toml: the application checks the tokens itself (the resource server), and can then have endpoints open to everyone.

The anon key as a bearer token

The anon key as a bearer token is no token. The Supabase clients send the project's anon key as the bearer token of everyone who is not signed in. The key is public and names nobody, so such a request goes on without its Authorization header (supabaseAnonKeyFilter): an open endpoint answers it, a protected one answers 401. Nothing is gained by sending the key.

spring.edge.supabase.anon-key-as-anonymous: false lets the resource server refuse it instead. The service role key is never a user's token: 401.

The local stack

On the local stack the functions reach the project at http://kong:8000 while the tokens name http://127.0.0.1:54321. Set the issuer under [edge_runtime.secrets] of config.toml:

[edge_runtime.secrets]
SPRING_SUPABASE_AUTH_ISSUER = "http://127.0.0.1:54321/auth/v1"

This is not needed on a hosted project.

The address of the client

The address of the client is the address of the connection, which is the platform's gateway. A rate limit by address needs spring.web.rate-limit.trusted-proxies; a limit by user does not.

Example

example/supabase/function is such a function. See An Edge Function in Dart.

See also