On this page

Edge Functions

Call Edge Functions from stitchdb-js with functions.invoke(): body, headers, method and errors.

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

The signed-in user's access token is sent automatically (or the anon key when nobody is signed in), so the function can check who is calling. Objects are sent as JSON; strings, Blob, ArrayBuffer and FormData are sent as they are. The response is parsed by its Content-Type: JSON, text or a Blob.

#Options

OptionPurpose
bodyRequest body
headersExtra request headers, for example { 'x-request-id': id }
method'POST' (default), 'GET', 'PUT', 'PATCH' or 'DELETE'

#Errors

JavaScript
import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from 'stitchdb-js'

const { data, error } = await stitchdb.functions.invoke('send-email', { body: { to } })
if (error instanceof FunctionsHttpError) {
  // the function answered with a 4xx/5xx status
  console.error(error.context.status, await error.context.json())
} else if (error instanceof FunctionsRelayError) {
  console.error('could not reach the function:', error.message)
} else if (error instanceof FunctionsFetchError) {
  console.error('network error:', error.message)
}

Write and deploy functions in Edge Functions; see Call functions for calling them over plain HTTP.