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.
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.
Authorization header, or the API key when there is no session. An Authorization header that you pass in FunctionInvokeOptions.headers wins.FunctionBody. Each factory sets the Content-Type: .json(_:) sends application/json, .text(_:) sends text/plain; charset=utf-8, and .data(_:contentType:) sends application/octet-stream unless you pass another type. A Content-Type in FunctionInvokeOptions.headers overrides it..json(_:) encodes and invoke decodes with the SDK's coders, which write and read Date as ISO 8601. Pass decoder: to invoke, or encoder: to .json(_:encoder:), to change that.x-region header and forceFunctionRegion query parameter are set to ensure proper function routing.FunctionInvokeOptions(timeout:). This only controls the client's request timeout — it cannot extend function execution beyond the platform's 150-second gateway idle timeout, after which a 504 Gateway Timeout is returned regardless of the value passed.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
)
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
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)")
}
}
let response = try await supabase.functions.invoke(
"hello",
options: FunctionInvokeOptions(
headers: [
HTTPField.Name("my-custom-header")!: "my-custom-header-value"
]
)
)
let response = try await supabase.functions.invoke(
"hello",
body: .json(["foo": "bar"]),
options: FunctionInvokeOptions(
region: .usEast1
)
)
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"
]
)
)
let response = try await supabase.functions.invoke(
"hello",
options: FunctionInvokeOptions(
method: .get,
headers: [
HTTPField.Name("my-custom-header")!: "my-custom-header-value"
]
)
)
let response = try await supabase.functions.invoke(
"hello",
options: FunctionInvokeOptions(
query: [URLQueryItem(name: "key", value: "value")]
)
)
let response = try await supabase.functions.invoke(
"hello",
body: .json(["foo": "bar"]),
options: FunctionInvokeOptions(
timeout: .seconds(30)
)
)