Developing Edge Functions locally
Get started with Edge Functions on your local machine.
Let's create a basic Edge Function on your local machine and then invoke it using the Supabase CLI.
Initialize a project#
Create a new Supabase project in a folder on your local machine:
CLI not installed?
Check out the CLI Docs to learn how to install the Supabase CLI on your local machine.
Create an Edge Function#
Let's create a new Edge Function called hello-world
inside your project:
This creates a function stub in your supabase
folder:
How to write the code#
The generated function uses native Deno.serve to handle requests. It gives you access to Request
and Response
objects.
Here's the generated Hello World Edge Function, that accepts a name in the Request
and responds with a greeting:
Running Edge Functions locally#
You can run your Edge Function locally using supabase functions serve
:
The functions serve
command has hot-reloading capabilities. It will watch for any changes to your files and restart the Deno server.
Invoking Edge Functions locally#
While serving your local Edge Function, you can invoke it using curl or one of the client libraries:
Where is my SUPABASE_ANON_KEY?
Run supabase status
to see your local credentials.
You should see the response { "message":"Hello Functions!" }
.
If you execute the function with a different payload, the response will change.
Modify the --data '{"name":"Functions"}'
line to --data '{"name":"World"}'
and try invoking the command again.
Next steps#
Check out the Deploy to Production guide to make your Edge Function available to the world.
Read on for some common development tips.
Development tips#
Here are a few recommendations when developing Edge Functions.
Skipping Authorization checks#
By default, Edge Functions require a valid JWT in the authorization header. If you want to use Edge Functions without Authorization checks (commonly used for Stripe webhooks), you can pass the --no-verify-jwt
flag when serving your Edge Functions locally.
Be careful when using this flag, as it will allow anyone to invoke your Edge Function without a valid JWT. The Supabase client libraries automatically handle authorization.
Using HTTP Methods#
Edge Functions support GET
, POST
, PUT
, PATCH
, DELETE
, and OPTIONS
. A Function can be designed to perform different actions based on a request's HTTP method. See the example on building a RESTful service to learn how to handle different HTTP methods in your Function.
HTML not supported
HTML content is not supported. GET
requests that return text/html
will be rewritten to text/plain
.
Naming Edge Functions#
We recommend using hyphens to name functions because hyphens are the most URL-friendly of all the naming conventions (snake_case, camelCase, PascalCase).
Organizing your Edge Functions#
We recommend developing “fat functions”. This means that you should develop few large functions, rather than many small functions. One common pattern when developing Functions is that you need to share code between two or more Functions. To do this, you can store any shared code in a folder prefixed with an underscore (_
). We also recommend a separate folder for Unit Tests including the name of the function followed by a -test
suffix.
We recommend this folder structure:
Using config.toml#
Individual function configuration like JWT verification and import map location can be set via the config.toml
file.
Error Handling#
The supabase-js
library provides several error types that you can use to handle errors that might occur when invoking Edge Functions:
Database Functions vs Edge Functions#
For data-intensive operations we recommend using Database Functions, which are executed within your database and can be called remotely using the REST and GraphQL API.
For use-cases which require low-latency we recommend Edge Functions, which are globally-distributed and can be written in TypeScript.