invoke()

Invoke a Supabase Function.

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

Parameters#

  • functionNamerequired
    string

    the name of the function to invoke

  • invokeOptionsrequired
    FunctionInvokeOptions

    No description provided.

Notes#

  • Requires an Authorization header.
  • Invoke params generally match the Fetch API spec.
  • When you pass in a body to your function, we automatically attach the Content-Type header for Blob, ArrayBuffer, File, FormData and String. If it doesn't match any of these types we assume the payload is json, serialise it and attach the Content-Type header as application/json. You can override this behaviour by passing in a Content-Type header of your own.
  • Responses are automatically parsed as json, blob and form-data depending on the Content-Type header sent by your function. Responses are parsed as text by default.

Examples#

Basic invocation.#

null

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

Error handling.#

A FunctionsHttpError error is returned if your function throws an error, FunctionsRelayError if the Supabase Relay has an error processing your function and FunctionsFetchError if there is a network error in calling your function.

1import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from "@supabase/supabase-js";
2
3const { data, error } = await supabase.functions.invoke('hello', {
4  headers: {
5    "my-custom-header": 'my-custom-header-value'
6  },
7  body: { foo: 'bar' }
8})
9
10if (error instanceof FunctionsHttpError) {
11  console.log('Function returned an error', error.message)
12} else if (error instanceof FunctionsRelayError) {
13  console.log('Relay error:', error.message)
14} else if (error instanceof FunctionsFetchError) {
15  console.log('Fetch error:', error.message)
16}

Passing custom headers.#

You can pass custom headers to your function. Note: supabase-js automatically passes the Authorization header with the signed in user's JWT.

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