For AI agents: the documentation index is at llms.txt. Guide pages have a markdown version at the same path with .md appended, before any query string or fragment.

Swift: Invokes a Supabase Edge Function.

Invoke a Supabase Edge Function. invoke buffers the whole response. It returns a FunctionResponse with the status, headers, and body, or decodes the body into a Decodable type.

Examples

Invocation with `Decodable` response

struct Response: Decodable {
  // Expected response definition
}

let response: Response = try await supabase.functions.invoke(
  "hello",
  body: .json(["foo": "bar"])
)

// Or name the type with `as:`
let same = try await supabase.functions.invoke(
  "hello",
  body: .json(["foo": "bar"]),
  as: Response.self
)

Invocation with custom response

let response = try await supabase.functions.invoke(
  "hello",
  body: .json(["foo": "bar"])
)

print(response.status.code) // Int
print(response.text ?? "") // The body as a UTF-8 string
print(response.body) // Data

Error handling

do {
  let response = try await supabase.functions.invoke(
    "hello",
    body: .json(["foo": "bar"])
  )
} catch let error as FunctionsError {
  switch error.kind {
  case .server:
    let code = error.response?.status.code ?? 0
    let body = String(decoding: error.response?.body ?? Data(), as: UTF8.self)
    print("Function returned code \(code) with response \(body)")
  case .relay:
    print("Relay error")
  default:
    print("Other error: \(error.localizedDescription)")
  }
}

Passing custom headers

let response = try await supabase.functions.invoke(
  "hello",
  options: FunctionInvokeOptions(
    headers: [
      HTTPField.Name("my-custom-header")!: "my-custom-header-value"
    ]
  )
)

Invoking a Function in the UsEast1 region

let response = try await supabase.functions.invoke(
  "hello",
  body: .json(["foo": "bar"]),
  options: FunctionInvokeOptions(
    region: .usEast1
  )
)

Calling with DELETE HTTP verb

let response = try await supabase.functions.invoke(
  "hello",
  body: .json(["foo": "bar"]),
  options: FunctionInvokeOptions(
    method: .delete,
    headers: [
      HTTPField.Name("my-custom-header")!: "my-custom-header-value"
    ]
  )
)

Calling with GET HTTP verb

let response = try await supabase.functions.invoke(
  "hello",
  options: FunctionInvokeOptions(
    method: .get,
    headers: [
      HTTPField.Name("my-custom-header")!: "my-custom-header-value"
    ]
  )
)

Pass additional query params

let response = try await supabase.functions.invoke(
  "hello",
  options: FunctionInvokeOptions(
    query: [URLQueryItem(name: "key", value: "value")]
  )
)

Invocation with a custom timeout

let response = try await supabase.functions.invoke(
  "hello",
  body: .json(["foo": "bar"]),
  options: FunctionInvokeOptions(
    timeout: .seconds(30)
  )
)