Edge Functions

Dart Edge


Dart Edge, a project built and maintained by Invertase, enables Dart code to be run on different edge environments. This guide will walk you through how you can start using Dart Edge to write your backend code using Dart and run it on Supabase.

Prerequisites

Make sure you have the edge CLI installed in addition to the Supabase CLI.


_10
# install the edge CLI
_10
dart pub global activate edge

Setting up your first Dart Edge project

Create a new Dart Edge project using the new command on the edge CLI. This will create a familiar boilerplate for your Dart Edge project such as main.dart and pubspec.yaml file.


_10
edge new supabase_functions new_project

cd into the project and initialize a new Supabase project inside the project you just created.


_10
cd new_project
_10
supabase init

Run pub get to install the dependencies.


_10
dart pub get

Open the lib/main.dart and look at the initial template.


_10
void main() {
_10
SupabaseFunctions(fetch: (request) {
_10
return Response("Hello from Supabase Edge Functions!");
_10
});
_10
}

SupabaseFunctions class has a fetch handler, which is expected to return a Response object, and has a request parameter, which contains information about the request such as body and headers.

Supporting multiple edge functions

By default, Dart Edge will create a single main.dart file in your project, which will be compiled into a function called dart_edge. You can override this behavior and create multiple functions by creating an edge.yaml file in the root of your project. Specify how to map which of your dart files should compile to what Supabase edge functions using the functions option.


_10
supabase:
_10
functions:
_10
api_handler: 'lib/api.dart'
_10
webhook_handler: 'lib/webhooks.dart'

Using Supabase client

Now let’s try to interact with our Supabase database. We will start by installing the dependencies.

Add dependencies

Add supabase to interact with Supabase service.


_10
dart pub add supabase

We also need to use a special HTTP client to HTTP requests in the edge environment.


_10
dart pub add edge_http_client

Initialize SupabaseClient

At this point, you can initialize a SupabaseClient by passing the EdgeHttpClient. We can access the Supabase credentials through the environment variables using Deno.env.get() method. Note that Supabase credentials are available by default, so no configurations are necessary.

Now we have the following sample functions code to query Supabase database using the Supabase client.


_19
import 'dart:convert';
_19
_19
import 'package:supabase_functions/supabase_functions.dart';
_19
import 'package:edge_http_client/edge_http_client.dart';
_19
import 'package:supabase/supabase.dart';
_19
_19
void main() {
_19
final supabase = SupabaseClient(
_19
Deno.env.get('SUPABASE_URL')!,
_19
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!, // Use service role key to bypass RLS
_19
httpClient: EdgeHttpClient(),
_19
);
_19
_19
SupabaseFunctions(fetch: (request) async {
_19
// You can query `public.users` table for example.
_19
final List users = await supabase.from('users').select().limit(10);
_19
return Response.json(users);
_19
});
_19
}

Run the Functions

Run locally

Create a edge.yaml file if you don't have one yet in the root of your project. Then, add the following configuration to specify the path to your Supabase project.


_10
supabase:
_10
project_path: './supabase'

Also, currently Dart Edge doesn't work with the latest versions of Flutter. If you do have the latest version of Flutter on your machine, you can use Flutter Version Management (FVM) to use an older version of Flutter for your Dart Edge project.


_10
fvm use 3.7.12 -f

You can read more about how to configure FVM for your project in the FVM documentation.

Run the following command to compile your Dart code. This command will start a watcher that will recompile anytime you make changes to your code.


_10
edge build supabase_functions --dev
_10
_10
# Or with FVM
_10
fvm dart pub run edge build supabase_functions --dev

With docker running, start your local Supabase with the following command


_10
supabase start

While you have the above code running, open another terminal and run the following to start the Supabase local development environment.


_10
supabase functions serve dart_edge --no-verify-jwt

You should be able to access your local function here:

http://localhost:54321/functions/v1/dart_edge

Deploy to the Edge

Run the following commands to compile and deploy your function on Supabase Edge functions.


_10
edge build supabase_functions
_10
supabase functions deploy dart_edge

You will be asked to provide the project reference of your Supabase instance to which you want to deploy the function. You can link your local project to your Supabase instance to avoid having to provide the Supabase reference every time you deploy.