Secure your Supabase functions with Unkey
Learn how to use Unkey to secure your Supabase functions

Supabase offers edge functions built upon Deno. They have a variety of uses for applications like OpenAI or working with their storage product. In this post, we will show you how to use Unkey to secure your function in just a few lines of code.
What is Unkey?
Unkey is an open source API management platform that helps developers secure, manage, and scale their APIs. Unkey has built-in features that can make it easier than ever to provide an API to your end users, including:
- Per key rate limiting
- Limited usage keys
- Time-based keys
- Per key analytics
Prerequisites
- Create a Supabase account
- Create a Unkey account and follow our Quickstart guide. So you have an API key to verify.
- Setup Supabase CLI for local development.
Create our project
Create a project folder
First, we need to create a folder. Let's call that unkey-supabase. This will be where our supabase functions exist going forward.
Start Supabase services
Now, we have a folder for our project. We can initialize and start Supabase for local development.
Make sure Docker is running. The start command uses Docker to start the Supabase services.
This command may take a while to run if this is the first time using the CLI.
Create a Supabase function
Now that Supabase is setup, we can create a Supabase function. This function will be where we secure it using Unkey.
This command creates a function stub in your Supabase folder at ./functions/hello-world/index.ts. This stub will have a function that returns the name passed in as data for the request.
Test your Supabase function
Before making any changes, let's ensure your Supabase function runs. Inside the function, you should see a cURL command similar to the following:
After invoking your Edge Function, you should see the response { "message":"Hello Functions!" }.
If you receive an error Invalid JWT, find the
ANON_KEYof your project in the Dashboard under Settings > API.
Add Unkey to secure our Supabase function
Add verifyKey to our function
Now that we have a function, we must add Unkey to secure the endpoint. Supabase uses Deno, so instead of installing our npm package, we will use ESM CDN to provide the verifyKey function we need.
What does verifyKey do?
Unkey's verifykey lets you verify a key from your end users. We will return a result and you can decide whether to give the user access to a resource or not based upon that result. For example, a response could be:
Updating our Supabase function
First, let's remove the boilerplate code from the function so we can work on adding Unkey.
Next, we will wrap the serve function inside a try-catch. Just in case something goes wrong, we can handle that.
Check headers for API Key
Inside our try, we can look for a header containing the user's API Key. In this example we will use x-unkey-api-key but you could call the header whatever you want. If there is no header will immediately return 401.
Verifying the key
The verifyKey function returns a result and error, making the logic easy to handle. Below is a simplified example of the verification flow.
Now you have a basic understanding of verification, let's add this to our Supabase function.
Testing our Supabase function
We can send a curl request to our endpoint to test this functionality. Below is an example of the curl to send. Remember, we now need to include our API key.
Adding CORS for added security
Adding CORS allows us to call our function from the frontend and decide what headers can be passed to our function. Inside your functions folder, add a file called cors.ts. Inside this cors file, we will tell the Supabase function which headers and origins are allowed.
Conclusion
In this post, we have covered how to use Unkey with Supabase functions to secure them. You can check out the code for this project in our Examples folder
