/supabase-tutorials

How to debug Supabase edge functions?

Master a step-by-step guide to debug Supabase Edge Functions. Set up your environment, test locally, deploy, and monitor logs to ensure smooth production performance.

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 debug Supabase edge functions?

 

Step 1: Set Up Your Development Environment

 

Before you start debugging Supabase Edge Functions, ensure that your development environment is correctly set up. This includes having Node.js installed as well as the Supabase CLI.


# Install Node.js if you haven't
# Visit nodejs.org to download the installer or use a package manager 

# Install Supabase CLI
npm install -g supabase

 

Step 2: Initialize Supabase Project

 

To work with Supabase Edge Functions, you must initialize a project. Make sure you are signed into your Supabase account.


# Create a new directory for your project and navigate into it
mkdir my-supabase-project && cd my-supabase-project

# Initialize the Supabase project
supabase init

# Authenticate your Supabase account
supabase login

 

Step 3: Create an Edge Function

 

Supabase Edge Functions can be created using the command line. Write your logic in JavaScript or TypeScript.


# Create a new Edge Function
supabase functions new hello-world

# Navigate to the created function directory
cd supabase/functions/hello-world

 

Step 4: Write Your Function Logic

 

Modify the index.ts (or index.js if using JavaScript) file inside your function directory. Write the logic you need for your edge function.


// index.ts

export default async function handler(req: Request) {
  const { name } = await req.json();
  return new Response(`Hello, ${name}`, { status: 200 });
}

 

Step 5: Develop and Test Locally

 

Testing locally is crucial to ensure that your function works before deploying. Use supabase serve to serve the function locally.


# Start the local development server
supabase functions serve hello-world

# Your function should be accessible at http://localhost:54321/functions/v1/hello-world

 

Step 6: Debugging Locally

 

While the function is running locally, debug using console.log() or a similar logging utility. Open your browser's developer console or CLI to read logs.


// Add console statements in your function for debugging
export default async function handler(req: Request) {
  const { name } = await req.json();
  console.log("Received request with name:", name); // Debugging log
  return new Response(`Hello, ${name}`, { status: 200 });
}

// Logs will be visible in the terminal where you ran the 'supabase functions serve' command.

 

Step 7: Deploy Your Function

 

Once satisfied with local testing, deploy your function to Supabase, allowing it to run in the production environment.


# Deploy your function to Supabase
supabase functions deploy hello-world

 

Step 8: Monitor Logs on Supabase

 

After deploying, monitor your Edge Function for any issues using the Supabase dashboard, where you can see logs and execution metrics.


# Use the Supabase dashboard to access function logs:
# Navigate to your Supabase project
# Go to the "Functions" section
# Select your deployed function to view logs

 

Step 9: Handle Errors Gracefully

 

Implement error handling within your Edge Function to troubleshoot effectively.


export default async function handler(req: Request) {
  try {
    const { name } = await req.json();
    if (!name) {
      throw new Error("Name is required");
    }
    return new Response(`Hello, ${name}`, { status: 200 });
  } catch (error) {
    console.error("Error handling request:", error); // Log error
    return new Response("Internal Server Error", { status: 500 });
  }
}

By following these steps, you will be able to effectively debug your Supabase Edge Functions, ensuring they perform as expected both locally and in production.

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