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.
To access the auth token securely for your Edge Function call, we recommend storing them in Supabase Vault.
Examples
Invoke an Edge Function every minute
Store project_url
and anon_key
in Supabase Vault:
_10select vault.create_secret('https://project-ref.supabase.co', 'project_url');_10select vault.create_secret('YOUR_SUPABASE_ANON_KEY', 'anon_key');
Make a POST request to a Supabase Edge Function every minute:
_16select_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 );