On this page

Call functions

Call functions from your app with the SDK, from any HTTP client, or from SQL.

#From the JavaScript SDK

JavaScript
const { data, error } = await stitchdb.functions.invoke('hello-world', {
  body: { name: 'Ada' },
})
// data → { message: 'Hello Ada!' }

body can be an object (sent as JSON), a string, a FormData, a Blob or an ArrayBuffer. The SDK adds the signed-in user's token, or your anon key.

JavaScript
// GET with a query string and custom headers
const { data, error } = await stitchdb.functions.invoke('search?q=shoes', {
  method: 'GET',
  headers: { 'x-request-id': crypto.randomUUID() },
})
if (error) console.error(error.message)

#Over HTTP

Terminal
curl -X POST 'https://<project-ref>.p.stitchdb.com/functions/v1/hello-world' \
  -H 'Authorization: Bearer <anon-key>' \
  -H 'Content-Type: application/json' \
  -d '{"name":"Ada"}'

#On a schedule, from SQL

Combine Cron jobs with HTTP from SQL. Store the service role key in Vault first:

SQL
-- Call the function every night at 03:00 UTC (pg_cron + pg_net).
select cron.schedule('nightly-report', '0 3 * * *', $$
  select net.http_post(
    url := 'https://<project-ref>.p.stitchdb.com/functions/v1/nightly-report',
    headers := jsonb_build_object(
      'Authorization', 'Bearer ' || (select decrypted_secret from vault.decrypted_secrets where name = 'service_role_key'),
      'Content-Type', 'application/json'),
    body := '{}'::jsonb)
$$);