/supabase-tutorials

How to pass headers to edge functions in Supabase?

Discover how to pass custom headers to Supabase Edge Functions. This step-by-step guide covers project setup, function coding, deployment, and testing for effective API integration.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to pass headers to edge functions in Supabase?

 

Step 1: Set Up Your Supabase Project

 

To get started, make sure you have a Supabase account and a project set up. If you haven't already, go to the Supabase website and create an account. Once your account is set up, create a new project in the Supabase dashboard. This setup will provide you with the API URL and the Anon/Public API key which will be used in your client-side code.

 

Step 2: Create an Edge Function

 

Supabase Edge Functions are deployed using the Deno runtime and allow you to write server-side logic. Navigate to the "Functions" section in the Supabase dashboard and click on "New Function" to create one.

 

Step 3: Write Your Edge Function Code

 

In the Edge Function editor, you can write your function logic. For the purpose of this tutorial, we'll write a simple function that handles incoming requests and expects certain headers. Here is a sample function code:


export default async (req) => {
  const headers = req.headers;

  const authToken = headers.get('authorization');
  const customHeader = headers.get('x-custom-header');

  if (!authToken || !customHeader) {
    return new Response('Missing headers', { status: 400 });
  }

  return new Response(`Authorization: ${authToken}, Custom Header: ${customHeader}`, { status: 200 });
}

This function retrieves the 'authorization' and 'x-custom-header' headers from the incoming request and checks if they exist. If they do, it responds with the values; otherwise, it returns a "Missing headers" error.

 

Step 4: Deploy Your Edge Function

 

After writing your code, deploy the Edge Function by clicking on the "Deploy" button within the editor. Supabase will handle the deployment and generate a unique URL for the function.

 

Step 5: Invoke the Edge Function from Your Client

 

With your Edge Function deployed, you can invoke it from your client-side application. Here is an example using JavaScript and Fetch API to make a request with custom headers:


const functionUrl = 'YOUR_FUNCTION_URL';
const authToken = 'Bearer YOUR_AUTH_TOKEN';
const customHeaderValue = 'CustomValue';

fetch(functionUrl, {
  method: 'GET',
  headers: {
    'authorization': authToken,
    'x-custom-header': customHeaderValue,
  },
})
  .then(response => response.text())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Replace YOUR_FUNCTION_URL with the URL of your deployed function and update the authorization and custom header values accordingly.

 

Step 6: Test Your Setup

 

Finally, test the setup by running your client-side code. You should see the response containing the header values you sent in the request if everything is set up correctly.

By following these steps, you can effectively pass headers to Edge Functions in Supabase. This allows for enhanced communication between your client application and server-side logic.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022