Edge Functions

Scheduling Edge Functions


The hosted Supabase Platform supports the pg_cron extension, a recurring job scheduler in Postgres.

In combination with the pg_net extension, this allows us to invoke Edge Functions periodically on a set schedule.

Examples

Invoke an Edge Function every minute

Store project_url and anon_key in Supabase Vault:


_10
select vault.create_secret('https://project-ref.supabase.co', 'project_url');
_10
select vault.create_secret('YOUR_SUPABASE_ANON_KEY', 'anon_key');

Make a POST request to a Supabase Edge Function every minute:


_16
select
_16
cron.schedule(
_16
'invoke-function-every-minute',
_16
'* * * * *', -- every minute
_16
$$
_16
select
_16
net.http_post(
_16
url:= (select decrypted_secret from vault.decrypted_secrets where name = 'project_url') || '/functions/v1/function-name',
_16
headers:=jsonb_build_object(
_16
'Content-type', 'application/json',
_16
'Authorization', 'Bearer: ' || (select decrypted_secret from vault.decrypted_secrets where name = 'anon_key')
_16
),
_16
body:=concat('{"time": "', now(), '"}')::jsonb
_16
) as request_id;
_16
$$
_16
);

Resources