On this page

Edge Functions

Create, serve locally, deploy and manage Edge Functions and their secrets with the CLI.

Terminal
stitchdb functions new send-email   # stitchdb/functions/send-email/index.ts
stitchdb functions serve            # local, live reload
stitchdb functions deploy send-email
stitchdb functions deploy           # every function

#Serve locally

functions serve runs every folder in stitchdb/functions at http://127.0.0.1:54321/functions/v1/<name> and follows their logs. Each function runs in its own Deno process that restarts when you save a file. Functions get STITCHDB_URL, STITCHDB_ANON_KEY, STITCHDB_SERVICE_ROLE_KEY and STITCHDB_DB_URL for the local project, plus the secrets in stitchdb/functions/.env.

Terminal
curl -i http://127.0.0.1:54321/functions/v1/send-email \
  -H 'Authorization: Bearer <local anon key>' -d '{"to":"[email protected]"}'

#Deploy

Each deploy uploads the function's folder as a new version (the previous 20 are kept, see Deploy with the API). Imports of ../_shared/… are bundled with the function. A deno.json or import_map.json in the folder is used as the import map.

Calls need a valid JWT (anon key or a user's token) unless you turn that off, for example for webhooks:

toml
# stitchdb/config.toml
[functions.stripe-webhook]
verify_jwt = false

or per deploy: stitchdb functions deploy stripe-webhook --no-verify-jwt.

CommandDoes
stitchdb functions listDeployed functions, versions and JWT setting
stitchdb functions download <name>The deployed code into stitchdb/functions/<name>
stitchdb functions delete <name>Deletes the function (its URL stops working)

#Secrets

Terminal
stitchdb secrets set STRIPE_KEY=sk_live_… RESEND_KEY=re_…
stitchdb secrets set --env-file .env.production
stitchdb secrets list
stitchdb secrets unset STRIPE_KEY

Secrets are environment variables for every function of the project (Deno.env.get('STRIPE_KEY')). Values are write-only: secrets list shows names and digests. See Environment and secrets.