Skip to main content
Version: v2 RC

invoke()

Invoke a Supabase Function.

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

Parameters

  • functionNamerequiredstring

    the name of the function to invoke

  • FunctionInvokeOptionsrequiredobject

    No description provided.

      Properties
    • bodyoptionalFile | Blob | ArrayBuffer | FormData | ReadableStream | Record | string

      the body of the request

        Properties
      • stringrequiredobject

        No description provided.

      • Recordrequiredobject

        No description provided.

      • ReadableStreamrequiredobject

        No description provided.

      • FormDatarequiredobject

        No description provided.

      • Filerequiredobject

        No description provided.

      • Blobrequiredobject

        No description provided.

      • ArrayBufferrequiredobject

        No description provided.

    • headersoptionalobject

      object representing the headers to send with the request

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

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

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.

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

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

if (error instanceof FunctionsHttpError) {
console.log('Function returned an error', error.message)
} 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.

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

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