Skip to content

supabase-community/functions-ex

Supabase Functions

Edge Functions client implementation for the Supabase Potion SDK in Elixir

Installation

def deps do
  [
    {:supabase_potion, "~> 0.6"},
    {:supabase_functions, "~> 0.1"}
  ]
end

Usage

Given a simple Edge Function that simply echos a raw string:

// simple-text/index.ts
Deno.serve(async (req) => {
  return new Response("Hello from Deno!", {
    headers: { "Content-Type": "text/plain" }
  });
});

From your Elixir server, after having started your Supabase.Client yo ucan inke this function as

client = Supabase.init_client!("SUPABASE_URl", "SUPABASE_KEY")

Supabase.Functions.invoke(client, "simple-text")
# {:ok, %Supabase.Response{status: 200, body: "Hello from Deno!"}}

It also work with data streaming, given an Edge Function

// stream-data/index.ts
Deno.serve(async (req) => {
  const stream = new ReadableStream({
    start(controller) {
      let count = 0;
      const interval = setInterval(() => {
        if (count >= 5) {
          clearInterval(interval);
          controller.close();
          return;
        }
        const message = `Event ${count}\n`;
        controller.enqueue(new TextEncoder().encode(message));
        count++;
      }, 1000);
    }
  });

  return new Response(stream, {
    headers: {
      "Content-Type": "text/event-stream",
      "Cache-Control": "no-cache",
      "Connection": "keep-alive"
    }
  });
});

The you could invoke it as

client = Supabase.init_client!("SUPABASE_URl", "SUPABASE_KEY")

# you can control the response streaming handling too (optional)
on_response = fn {status, headers, body} ->
  require Logger

  Logger.info("received response with #{status} status")
  Logger.info("received #{inspect(headers, pretty: true)} headers")

  file = File.stream!("output.txt", [:write, :utf8])

  body
  |> Stream.into(file)
  |> Stream.run()
end

Supabase.Functions.invoke(client, "stream-data", on_response: on_response)
# :ok

Timeout Support

You can control the timeout for function invocations using the timeout option. If no timeout is specified, requests will timeout after 15 seconds by default.

client = Supabase.init_client!("SUPABASE_URL", "SUPABASE_KEY")

# Basic invocation with default timeout (15 seconds)
{:ok, response} = Supabase.Functions.invoke(client, "my-function")

# Custom timeout (5 seconds)
{:ok, response} = Supabase.Functions.invoke(client, "my-function", timeout: 5_000)

# Timeout with body and headers  
{:ok, response} = Supabase.Functions.invoke(client, "my-function", 
  body: %{data: "value"}, 
  headers: %{"x-custom" => "header"},
  timeout: 30_000)

# Streaming with timeout
on_response = fn {status, headers, body} ->
  # Handle streaming response
  {:ok, body}
end

{:ok, response} = Supabase.Functions.invoke(client, "my-function",
  on_response: on_response,
  timeout: 10_000)

This feature provides:

  • Request cancellation: Long-running requests will timeout and be cancelled
  • Better resource management: Prevents hanging connections
  • Comprehensive timeout coverage: Sets both receive timeout (per-chunk) and request timeout (complete response)
  • Feature parity with JS client: Matches timeout functionality in the JavaScript SDK

About

Elixir library to interact with Supabase Edge Functions

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Contributors 2

  •  
  •