Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

add Dart Edge guide

dshukertjr3083d924aed4
diff --git a/apps/docs/pages/guides/functions/examples/dart-edge.mdx b/apps/docs/pages/guides/functions/examples/dart-edge.mdx
new file mode 100644
index 00000000000..ef0dcff01dd
--- /dev/null
+++ b/apps/docs/pages/guides/functions/examples/dart-edge.mdx
@@ -0,0 +1,192 @@
+import Layout from '~/layouts/DefaultGuideLayout'
+
+export const meta = {
+ id: 'examples-dart-edge',
+ title: 'Dart Edge',
+ description: 'Write your functions using Dart.',
+}
+
+[Dart Edge](https://docs.dartedge.dev/) is a project that enables Dart code to be run on different edge environments built by [Invertase](https://invertase.io/). 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.
+
+> ⚠️ WARNING: Dart Edge is an experimental project - it's probably not ready for production usage unless you're okay with living on the 'edge'.
+
+## **Pre requirements**
+
+Make sure you have [the edge CLI](https://docs.dartedge.dev/#getting-started) installed in addition to [the Supabase CLI](https://supabase.com/docs/guides/cli#installation).
+
+```bash
+# install the edge CLI
+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.
+
+```bash
+edge new supabase_functions new_project
+```
+
+`cd` into the project and initialize a new Supabase project inside the project you just created.
+
+```bash
+cd new_project
+supabase init
+```
+
+Run `pub get` to install the dependencies.
+
+```bash
+dart pub get
+```
+
+Open the `lib/main.dart` and look at the initial template.
+
+```dart
+void main() {
+ SupabaseFunctions(fetch: (request) {
+ return Response("Hello from Supabase Edge Functions!");
+ });
+}
+```
+
+`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.
+
+## Using Supabase Client
+
+Now let’s try to interact with our Supabase database. We will start by installing the dependencies.
+
+### Add dependencies
+
+Add [supabase](https://pub.dev/packages/supabase) to interact with Supabase service.
+
+```bash
+dart pub add supabase
+```
+
+We also need to use a special HTTP client to HTTP requests in the edge environment.
+
+```bash
+dart pub add edge_http_client
+```
+
+We also need to add the `yet_another_json_isolate` package, which is the JSON encoder/decoder that utilizes isolate.
+
+```bash
+dart pub add yet_another_json_isolate
+```
+
+### Override the JSON isolate
+
+Since isolates are not supported in the edge, we have to override its behavior to handle JSON serialization using the main thread.
+
+```dart
+import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
+
+class EdgeIsolate implements YAJsonIsolate {
+ @override
+ Future decode(String json) {
+ return Future.value(jsonDecode(json));
+ }
+
+ @override
+ Future<void> dispose() async {}
+
+ @override
+ Future<String> encode(Object? json) {
+ return Future.value(jsonEncode(json));
+ }
+
+ @override
+ Future<void> initialize() async {}
+}
+```
+
+### Initialize SupabaseClient
+
+At this point, you can initialize a `SupabaseClient` by passing the [EdgeHttpClient](https://pub.dev/packages/edge_http_client) and the `EdgeIsolate`. We can access the Supabase credentials through the [environment variables](https://supabase.com/docs/guides/functions/secrets) using `Deno.env.get()` method. Note that [Supabase credentials are available by default](https://supabase.com/docs/guides/functions/secrets), so no configurations are necessary.
+
+Combining the isolates we overrode earlier, we have the following sample functions code to query Supabase database using the Supabase client.
+
+```dart
+import 'dart:convert';
+
+import 'package:supabase_functions/supabase_functions.dart';
+import 'package:edge_http_client/edge_http_client.dart';
+import 'package:supabase/supabase.dart';
+import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
+
+void main() {
+ final supabase = SupabaseClient(
+ Deno.env.get('SUPABASE_URL')!,
+ Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!, // Use service role key to bypass RLS
+ httpClient: EdgeHttpClient(),
+ isolate: EdgeIsolate(),
+ );
+
+ SupabaseFunctions(fetch: (request) async {
+ // You can query `public.users` table for example.
+ final List users = await supabase.from('users').select().limit(10);
+ return Response.json(users);
+ });
+}
+
+class EdgeIsolate implements YAJsonIsolate {
+ @override
+ Future decode(String json) {
+ return Future.value(jsonDecode(json));
+ }
+
+ @override
+ Future<void> dispose() async {}
+
+ @override
+ Future<String> encode(Object? json) {
+ return Future.value(jsonEncode(json));
+ }
+
+ @override
+ Future<void> initialize() async {}
+}
+```
+
+## Run the Functions
+
+### Run locally
+
+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.
+
+```bash
+edge build supabase_functions --dev
+```
+
+With docker running, start your local Supabase with the following command
+
+```bash
+supabase start
+```
+
+While you have the above code running, open another terminal and run the following to start the Supabase local development environment.
+
+```bash
+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](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.
+
+```bash
+edge build supabase_functions
+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](https://supabase.com/docs/guides/cli/local-development#link-your-project) to avoid having to provide the Supabase reference every time you deploy.
+
+export const Page = ({ children }) => <Layout meta={meta} children={children} />
+
+export default Page