JavaScript: Invokes a Supabase Edge Function. (original) (raw)

Invokes a function

Invoke a Supabase Edge Function.

Parameters

(Required)
The name of the Function to invoke.

(Required)
Options for invoking the Function.

Examples

Basic invocation

const { data, error } = await supabase.functions.invoke('hello', {
  body: { foo: 'bar' }
})

Error handling

import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from "@supabase/supabase-js";

const { data, error } = await supabase.functions.invoke('hello', {
  headers: {
    "my-custom-header": 'my-custom-header-value'
  },
  body: { foo: 'bar' }
})

if (error instanceof FunctionsHttpError) {
  const errorMessage = await error.context.json()
  console.log('Function returned an error', errorMessage)
} else if (error instanceof FunctionsRelayError) {
  console.log('Relay error:', error.message)
} else if (error instanceof FunctionsFetchError) {
  console.log('Fetch error:', error.message)
}

Passing custom headers

const { data, error } = await supabase.functions.invoke('hello', {
  headers: {
    "my-custom-header": 'my-custom-header-value'
  },
  body: { foo: 'bar' }
})

Calling with DELETE HTTP verb

const { data, error } = await supabase.functions.invoke('hello', {
  headers: {
    "my-custom-header": 'my-custom-header-value'
  },
  body: { foo: 'bar' },
  method: 'DELETE'
})

Invoking a Function in the UsEast1 region

import { createClient, FunctionRegion } from '@supabase/supabase-js'

const { data, error } = await supabase.functions.invoke('hello', {
  body: { foo: 'bar' },
  region: FunctionRegion.UsEast1
})

Calling with GET HTTP verb

const { data, error } = await supabase.functions.invoke('hello', {
  headers: {
    "my-custom-header": 'my-custom-header-value'
  },
  method: 'GET'
})