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:

1
select vault.create_secret('https://project-ref.supabase.co', 'project_url');
2
select vault.create_secret('YOUR_SUPABASE_PUBLISHABLE_KEY', 'publishable_key');

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

1
select
2
cron.schedule(
3
'invoke-function-every-minute',
4
'* * * * *', -- every minute
5
$$
6
select
7
net.http_post(
8
url:= (select decrypted_secret from vault.decrypted_secrets where name = 'project_url') || '/functions/v1/function-name',
9
headers:=jsonb_build_object(
10
'Content-type', 'application/json',
11
'Authorization', 'Bearer ' || (select decrypted_secret from vault.decrypted_secrets where name = 'anon_key')
12
),
13
body:=concat('{"time": "', now(), '"}')::jsonb
14
) as request_id;
15
$$
16
);

Resources