# Resend Supabase Edge function: error

**URL:** <https://community.weweb.io/t/resend-supabase-edge-function-error/10285>\
**Category:** Ask us anything\
**Tags:** supabase\
**Created:** [August 16, 2024, 2:32pm UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285 "2024-08-16T14:32:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)\
**Post date:** [August 16, 2024, 2:32pm UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/1 "2024-08-16T14:32:35Z")

</div>

After many hours of trial and error I managed to create a Resend Supabase Edge function.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/2/2b514800ba2dda8bd210d96da303b4eb7856ef42.png)

However, it doesn’t work, I always get an **error** :

message: “Failed to send a request to the Edge Function”  
cause  
name: “FunctionsFetchError”

Chrome Network log:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/a/a43ce4203845e2b9cf5f59069d29db4b2e52d976.png)

Note: paipa.pm is **verified in Resend** and I could send a test mail from Resend.

Note: The WeWeb doc is incomplete and not clear enough: Some sentences are cut off, it’s not clear how to set **to, from, subject, html** dynamically. Use **header** fields? **html** = WeWeb **Body** field?

Email is one of the most important features for an app. IMHO WeWeb should develop a **Resend plugin** which doesn’t need Supabase Edge functions.

There is a similar issue but the answers do not help:

> [@Error: Failed to send request to the edge function](https://community.weweb.io/t/error-failed-to-send-request-to-the-edge-function/8875):
>
> Hi, i’m trying to combine weweb, supabase and open ai through an edge function, created in supabase. I’ve gotten the edge function too run using the CLI and now I’m trying to create an workflow using “invoke supabase edge function” I’ve added the correct name and it is POST and two headers, one content-type and one authorization, and finally one an Image hardcoded in the body, that should be analyzed by the AI. When triggering the action i get the error message: “Failed to send request to the e…

Or is it a CORS error? I tried this solution: **[CORS (Cross-Origin Resource Sharing) support for Invoking from the browser | Supabase Docs](https://supabase.com/docs/guides/functions/cors)**  
but it suddenly displays **two** errors:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/8/890972dd1f3d640ffebd2fd011fbc4b37242ef61.png)

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/1/1e26b828f3f70ac7ae60c034076fa04cf127dc24.png)

In **Postman** , I get this response:  
“message”: “Hello Functions!”

Note that the **sent fields** are **correct** :  
From:  
pm@paipa.pm  
Subject:  
Test-Mail  
To:  
[email@xxx.com](mailto:email@xxx.com)  
Request payload:  
This is a Testmail from WeWeb with Resend

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [August 16, 2024, 5:25pm UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/2 "2024-08-16T17:25:55Z")

</div>

You need to send CORS headers with your endpoint when there is an OPTIONS request.

> **[CORS (Cross-Origin Resource Sharing) support for Invoking from the browser |...](https://supabase.com/docs/guides/functions/cors)**
>
> Add CORS headers to invoke Edge Functions from the browser.

---

<div class="post-metadata">

**Author:** ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)\
**Post date:** [August 17, 2024, 6:45am UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/3 "2024-08-17T06:45:14Z")

</div>

I know (see my contribution). Maybe I made an error in the index.ts. I just pasted the code from Supabase at the beginning of the WeWeb index.ts code (and changed the path to cors.ts as well as the API key).  
Here’s the complete index.ts, maybe you see the error. VS only says that **name Deno is unknown**.

// Set the API key for the Resend service  
const RESEND\_API\_KEY = ‘xxxxx’  
import { corsHeaders } from ‘d:/\_shared/cors.ts’

console.log(`Function "browser-with-cors" up and running!`)

Deno.serve(async (req) =\> {  
// This is needed if you’re planning to invoke your function from a browser.  
if (req.method === ‘OPTIONS’) {  
return new Response(‘ok’, { headers: corsHeaders })  
}

try {  
const { name } = await req.json()  
const data = {  
message: `Hello ${name}!`,  
}

```
return new Response(JSON.stringify(data), {
  headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  status: 200,
})

```

} catch (error) {  
return new Response(JSON.stringify({ error: error.message }), {  
headers: { …corsHeaders, ‘Content-Type’: ‘application/json’ },  
status: 400,  
})  
}  
})

// Define an asynchronous function (handler) to handle requests with parameters  
const handler = async (\_request: Request, from: string, to: string, subject: string, html: string): Promise =\> {  
// Send a POST request to the Resend API to send an email  
const res = await fetch(‘[https://api.resend.com/emails](https://api.resend.com/emails)’, {  
method: ‘POST’,  
headers: {  
‘Content-Type’: ‘application/json’, // Set the content type to JSON  
Authorization: `Bearer ${RESEND_API_KEY}`, // Include the API key in the Authorization header  
},  
body: JSON.stringify({  
from: from,  
to: to,  
subject: subject,  
html: html,  
}),  
})

// Parse the response as JSON  
const data = await res.json()

// Return a new Response object with the JSON data, status 200, and appropriate headers  
return new Response(JSON.stringify(data), {  
status: 200,  
headers: {  
‘Content-Type’: ‘application/json’,  
},  
})  
}

// Start a Deno HTTP server and pass the handler function with dynamic “from”, “to”, “subject”, and “html” values  
Deno.serve(async (req) =\> {  
const { searchParams } = new URL(req.url)

// Example: Extract “from” and “to” values from query parameters  
const from = searchParams.get(‘from’) || ‘defaultfrom@example.com’  
const to = searchParams.get(‘to’) || ‘defaultto@example.com’  
const subject = searchParams.get(‘subject’) || ‘Best email ever 😀’  
const html = searchParams.get(‘html’) || ‘It works!’

// Call the modified handler function with dynamic “from”,“to”, “subject”, and “html” values  
return await handler(req, from, to, subject, html)  
})

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [August 17, 2024, 7:38am UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/4 "2024-08-17T07:38:44Z")

</div>

If you want, this is my code for Resend

```auto
import "https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts";
import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
import { corsHeaders } from '../_shared/cors.ts';

// Get the Resend API key from environment variables
const RESEND_API_KEY = Deno.env.get('RESEND_API_KEY');

// Define the handler function for the server
const handler = async (request: Request): Promise<Response> => {
    // Handle CORS preflight request
    if (request.method === 'OPTIONS') {
        return new Response('ok', { headers: corsHeaders });
    }

    // Only allow POST requests
    if (request.method !== 'POST') {
        return new Response('Method not allowed', {
            status: 405,
            headers: corsHeaders,
        });
    }

    let requestBody;
    try {
        // Parse the request body as JSON
        requestBody = await request.json();
    } catch (error) {
        return new Response('Invalid JSON', {
            status: 400,
            headers: corsHeaders,
        });
    }

    // Extract necessary fields from the request body
    const { from, to, subject, html } = requestBody;

    // Check if all required fields are present
    if (!from || !to || !subject || !html) {
        return new Response('Missing required fields', {
            status: 400,
            headers: corsHeaders,
        });
    }

    try {
        // Make a POST request to the Resend API
        const res = await fetch('https://api.resend.com/emails', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${RESEND_API_KEY}`,
            },
            body: JSON.stringify({
                from,
                to,
                subject,
                html,
            }),
        });

        // Check if the request was successful
        if (res.ok) {
            const data = await res.json();
            return new Response(JSON.stringify(data), {
                status: 200,
                headers: { ...corsHeaders, 'Content-Type': 'application/json' },
            });
        } else {
            const errorData = await res.text();
            return new Response(errorData, {
                status: res.status,
                headers: { ...corsHeaders, 'Content-Type': 'application/json' },
            });
        }
    } catch (error) {
        return new Response(JSON.stringify({ error: error.message }), {
            status: 500,
            headers: { ...corsHeaders, 'Content-Type': 'application/json' },
        });
    }
};

// Start the server with the handler
serve(handler);

```

Provide all these fields in the Body option as arguments.

```auto
{
   "from":"hello@broberto.sk", 
   "to":"delivered@resend.dev", 
   "subject": "Broberto is testing", 
   "html":"<h1>Broberto is testing this</h1><p>And it indeed works bro! Lesgoooo!</p>"}

```

---

<div class="post-metadata">

**Author:** ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)\
**Post date:** [August 17, 2024, 9:05am UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/5 "2024-08-17T09:05:26Z")

</div>

Thank you for sharing your code! However, even after changing this line:  
const RESEND\_API\_KEY = Deno.env.get(‘RESEND\_API\_KEY’);  
to my own API key:  
const RESEND\_API\_KEY = “xxxxx”;  
I get the CORS error.  
There seems to be an error in this line:  
import { serve } from “[/http/server.ts | std@0.190.0 | Deno](https://deno.land/std@0.190.0/http/server.ts)”;  
**Cannot find module ‘[/http/server.ts | std@0.190.0 | Deno](https://deno.land/std@0.190.0/http/server.ts)’ or its corresponding type declarations**.  
And **name Deno** is **unkown** again if I use your API Key instead of mine.

I looked at the Deno page _deno.land/std@0.224.0/http/server.ts_ and it says to use this string:  
import \* as mod from “[/http/server.ts | std@0.224.0 | Deno](https://deno.land/std@0.224.0/http/server.ts)”;

But using this doesn’t help.  
In the meantime, I found out how to **resolve the VS errors: Install the Deno language server and CLI**. But this doesn’t resolve the CORS issue. Any further idea?

Additional question:  
I would like to use the WeWeb header fields for from, to, and subject instead of putting them into the body:  
// Extract necessary fields from the request body  
const { from, to, subject, html } = requestBody;

I’m not an experienced coder. How should I change the script to this end (maybe I could fix this by using parts of the WeWeb script)?

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [August 17, 2024, 9:32am UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/6 "2024-08-17T09:32:21Z")

</div>

I’d probably need to see this to debug this. [I do sessions to solve things like this if you’d be interested.](https://broberto.sk/consult)

I’d need to see how your function and folders are structured, also see what cors.ts you have. It’s really difficult to tell right away from error logs.

---

<div class="post-metadata">

**Author:** ![BertrandG](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/bertrandg/32/5129_2.png) [@BertrandG](https://community.weweb.io/u/BertrandG)\
**Post date:** [August 17, 2024, 12:26pm UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/7 "2024-08-17T12:26:19Z")

</div>

I just booked a session with you tomorrow 16:00 CET. Maybe we can also look into 1 or 2 other issues if time left.  
BTW: I use the cors.ts from [CORS (Cross-Origin Resource Sharing) support for Invoking from the browser | Supabase Docs](https://supabase.com/docs/guides/functions/cors)

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [August 17, 2024, 12:36pm UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/8 "2024-08-17T12:36:23Z")

</div>

> [@BertrandG](#):
>
> I just booked a session with you tomorrow 16:00 CET. Maybe we can also look into 1 or 2 other issues if time left.

Thanks! I will be there 🙂 And absolutely, usually once the main challenge is solved I’m open to any other issues/questions etc. so definitely, if you have something else, feel free to prepare it.

---

<div class="post-metadata">

**Author:** ![laurentvienne](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@laurentvienne](https://community.weweb.io/u/laurentvienne)\
**Post date:** [October 28, 2025, 3:41am UTC](https://community.weweb.io/t/resend-supabase-edge-function-error/10285/9 "2025-10-28T03:41:37Z")

</div>

import { corsHeaders } from ‘../\_shared/cors.ts’; I don’t understand where I have to create this file in supabase ?
