Supabase Functions on Deno Deploy
We’re excited to announce our partnership with Supabase in the launch of their new product, Supabase Functions, which allows you to deploy code globally on the edge within seconds. It’s built on top of our Deno Deploy infrastructure, which includes auto-scaling and auto-caching by default, so you can focus less on infrastructure and more on building product.
With Deno, Supabase Functions get first class TypeScript support, ESM style imports, out of the box security, and support for modern web APIs.
If you haven’t heard of Supabase before, it is an open source Firebase alternative, that provides backend services to create and deploy websites, services, and apps. The new Functions product joins an impressive suite of managed solutions, including database, storage, and a user management system.
Let’s take a look at how to get started.
Functions with Supabase
Getting started with Supabase Functions only takes minutes.
Install and setup the Supabase CLI
On Mac, you can use homebrew:
$ brew install supabase/tap/supabase
See other installation instructions.
Log into your Supabase CLI by grabbing an access token here and pasting it into the prompt that appears after this command:
$ supabase login
Create a Supabase function
You can create a function with the subcommand functions
:
$ supabase functions new hello
This will create a function stub supabase/functions/hello/index.ts
, which will
look like this:
import { serve } from "https://deno.land/[email protected]/http/server.ts";
console.log("Hello from Functions!");
serve(async (req) => {
const { name } = await req.json();
const data = {
message: `Hello ${name}!`,
};
return new Response(
JSON.stringify(data),
{ headers: { "Content-Type": "application/json" } },
);
});
Execute the function locally
First, start Docker, then start the Supabase stack:
$ supabase start
Started local development setup.
API URL: http://localhost:54321
DB URL: postgresql://postgres:postgres@localhost:54322/postgres
Studio URL: http://localhost:54323
Inbucket URL: http://localhost:54324
anon key: eyxxxxx
service_role key: eyzzzzz
Grab the anon key
, because we’ll need to pass it in the headers to invoke the
function.
Next, start the function watcher:
$ supabase functions serve hello
Note that supabase function serve
has hot-reloading capabilities. It’ll
restart the Deno server if there are any changes to your files.
Once the function is being served locally, you can execute it using curl.
Replace ANON_KEY
with the anon key from the supabase start
output.
$ curl --location --request POST 'http://localhost:54321/functions/v1/' \
--header 'Authorization: Bearer ANON_KEY' \
--header 'Content-Type: application/json' \
--data '{"name":"Functions"}'
You should receive the response {"message":"Hello Functions!"}
.
Deploy the function
You can deploy the function directly from the CLI. First, you have to link a Supabase project. Head over to supabase.com and create a project.
Your Supabase project should have a URL structure like
https://app.supabase.io/project/xyzabcxxxxxx
. The alphabetical letters after
/project/
is the project’s ref
, which you’ll have to provide in the below
command:
$ supabase link --ref xyzabcxxxxxx
Finished supabase link.
Then, deployment is as simple as:
$ supabase functions deploy hello
Bundling supabase/functions/hello
Deployed Function hello on project xyzabcxxxxxx.
Finally, execute the function in production. Note that the ANON_KEY
here
should be in the settings of your project on Supabase’s website.
curl --location --request POST 'https://PROJECT_REF.supabase.co/functions/' \
--header 'Authorization: Bearer ANON_KEY' \
--header 'Content-Type: application/json' \
--data '{"name": "Functions"}'
You should receive the response {"message":"Hello Functions!"}
.
For more information, see Supabase’s documentation.
What’s next?
We are huge advocates for making web development as easy and delightful as possible, and enabling Supabase developers to enhance their apps with edge functions deployed globally with Deno Deploy is a step in that direction. And with more developers using Deno and Deno Deploy, it’ll help us improve so that you can continue to have the best experience building and deploying products for your users.
Want to provide fast, easy, and global edge deployments for your users? Email us at [email protected].