Issues with resend

Hello!!

I’m trying to use Resend with my vercel.

My email at resend is verified:

My DNS is confirmed and valid

I have added enviorment variabels to vercel:

My domain in vercel is correct:


Current versus Expected behaviour

I keep getting 500 or 404 error when trying to send a email.

When navigation to the page i get this:

When checking the function file it shows green.

I’m really not sure what to do to make this work.


Code

This is in my api/email/route

import { VercelRequest, VercelResponse } from "@vercel/node";
import axios from "axios";

const RESEND_API_KEY = process.env.RESEND_API_KEY;
const sendEmail = async (html: string, subject: string, to: string[]) => {
  try {
    const response = await axios.post(
      "https://api.resend.com/emails",
      {
        from: "Acme <onboarding@resend.dev>",
        to,
        subject,
        html,
      },
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${RESEND_API_KEY}`,
        },
      }
    );
    return response.data;
  } catch (error) {
    console.error("Email sending failed:", error.response?.data || error.message);
    throw new Error("Failed to send email");
  }
};

// Main handler function
export default async function handler(req: VercelRequest, res: VercelResponse) {
  console.log("Request method:", req.method);
  console.log("Request body:", req.body);

  if (req.method === "POST") {
    const { htmlContent, subject, recipients } = req.body;

    if (!htmlContent || !subject || !recipients) {
      console.error("Missing required parameters:", { htmlContent, subject, recipients });
      return res.status(400).json({ error: "Missing required parameters" });
    }

    try {
      const response = await sendEmail(htmlContent, subject, recipients);
      return res.status(200).json(response);
    } catch (error: unknown) {
      console.error("Error sending email:", error);
      const errorMessage = error instanceof Error ? error.message : "An unknown error occurred";
      return res.status(500).json({ error: errorMessage });
    }
  } else {
    return res.status(405).json({ error: "Method Not Allowed" });
  }
}

Project information (URL, framework, environment, project settings)

Frameworks / Libraries

  • Frontend Framework: React (react, react-dom, react-router-dom)
  • Build Tool: Vite (vite, @vitejs/plugin-react-swc)
  • Styling: Tailwind CSS (tailwindcss, tailwindcss-animate, @tailwindcss/vite, tailwind-merge)
  • Form Handling: Formik + Yup (formik, yup)
  • Internationalization: i18next (i18next, react-i18next, i18next-browser-languagedetector)
  • UI Primitives: Radix UI (@radix-ui/react-dialog, @radix-ui/react-tabs, etc.)
  • Animations: Framer Motion
  • Notifications: React Toastify
  • Icons: Lucide React
  • Typewriter effect: react-simple-typewriter
  • Utility Libraries: clsx, class-variance-authority, axios

:globe_with_meridians: Environment

  • TypeScript project (typescript, tsc -b used in build script)
  • Environment Variables: Uses dotenv
  • Node Backend Support: Minimal, with @vercel/node, likely for serverless functions or API routes

:hammer_and_wrench: Project Settings

  • "type": "module": Indicates use of ECMAScript Modules
  • "private": true: Not intended for publishing to npm
  • "scripts":
    • dev: Start dev server with Vite
    • build: Type-check and build with Vite
    • lint: Lint the project
    • preview: Serve the build locally
    • watch: Custom Tailwind CSS watch command

There’s another community post with 404 debugging tips that might be helpful. Please give these solutions a try and let us know how it goes.

A human should be around soon to offer more advice. But you can also get helpful information quickly by asking v0.

Hi @christonn93, welcome to the Vercel Community!

Sorry that you’re running into this issue. Have you checked the Vercel runtime logs? Can you share those here?

Does the code work correctly locally?

[12:47:30.775] Cloning github.com/Christonn93/OnlinePortfolio (Branch: master, Commit: 7ade9ae)
[12:47:31.028] Cloning completed: 253.000ms
[12:47:32.396] Restored build cache from previous deployment (GfM83Gp9qhEFZSrLuwMjxJKtbsr5)
[12:47:32.523] Running build in Washington, D.C., USA (East) – iad1
[12:47:32.864] Running "vercel build"
[12:47:33.231] Vercel CLI 41.5.0
[12:47:33.913] Installing dependencies...
[12:47:35.164] npm warn deprecated @types/dotenv@8.2.3: This is a stub types definition. dotenv provides its own type definitions, so you do not need this installed.
[12:47:35.349] 
[12:47:35.349] added 2 packages in 1s
[12:47:35.350] 
[12:47:35.350] 85 packages are looking for funding
[12:47:35.350]   run `npm fund` for details
[12:47:35.386] Running "npm run build"
[12:47:36.320] 
[12:47:36.321] > online-portfolio@0.0.0 build
[12:47:36.321] > tsc -b && vite build
[12:47:36.321] 
[12:47:42.085] e[36mvite v6.2.1 e[32mbuilding for production...e[36me[39m
[12:47:42.158] transforming...
[12:47:47.912] e[32m✓e[39m 2373 modules transformed.
[12:47:48.272] rendering chunks...
[12:47:48.524] computing gzip size...
[12:47:48.551] e[2mdist/e[22me[32mindex.html                         e[39me[1me[2m  0.69 kBe[22me[1me[22me[2m │ gzip:   0.39 kBe[22m
[12:47:48.551] e[2mdist/e[22me[2massets/e[22me[32mredgo-BTNHkO8n.png          e[39me[1me[2m 69.82 kBe[22me[1me[22m
[12:47:48.551] e[2mdist/e[22me[2massets/e[22me[32mprofileImage-BnYelm8g.jpg   e[39me[1me[2m 83.59 kBe[22me[1me[22m
[12:47:48.552] e[2mdist/e[22me[2massets/e[22me[35mindex-nZnMak2J.css          e[39me[1me[2m 31.77 kBe[22me[1me[22me[2m │ gzip:   6.51 kBe[22m
[12:47:48.552] e[2mdist/e[22me[2massets/e[22me[36mserver.browser-BYJNpaAM.js  e[39me[1me[2m 70.60 kBe[22me[1me[22me[2m │ gzip:  23.14 kBe[22m
[12:47:48.552] e[2mdist/e[22me[2massets/e[22me[36mindex-B5KdLrUE.js           e[39me[1me[2m212.57 kBe[22me[1me[22me[2m │ gzip:  70.10 kBe[22m
[12:47:48.552] e[2mdist/e[22me[2massets/e[22me[36mindex-DfJhhd06.js           e[39me[1me[33m629.16 kBe[39me[22me[2m │ gzip: 204.41 kBe[22m
[12:47:48.552] e[32m✓ built in 6.43se[39m
[12:47:48.553] e[33m
[12:47:48.554] (!) Some chunks are larger than 500 kB after minification. Consider:
[12:47:48.557] - Using dynamic import() to code-split the application
[12:47:48.560] - Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
[12:47:48.560] - Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.e[39m
[12:47:48.878] Using TypeScript 5.7.3 (local user-provided)
[12:47:51.384] Build Completed in /vercel/output [17s]
[12:47:51.448] Deploying outputs...
[12:47:54.831] 
[12:47:55.143] Deployment completed
[12:48:09.360] Uploading build cache [88.59 MB]...
[12:48:11.406] Build cache uploaded: 2.046s 

Hey @christonn93 your snippet above looks to be your build logs. If you attempt to invoke your email function, your Logs tab on the project’s dashboard will display information about the function (and errors around it).

If you are on a Hobby account, your logs I think are only for the last hour, so that is why you’ll have to try to invoke the function so you have fresh logs to view.

let me know what you find, happy to look further!

1 Like

This is the log I sendt in chat:

This is the summary:

Here is full screenshots:



Please, let me know what if there is any deeper I can go to give you more information regarding the deployment and build.

Hi @christonn93, thanks for the information. But, I’d ask you to follow the comment from @miguelcabs: Issues with resend - #5 by miguelcabs

We need the runtime logs from your project, when you try sending an email:

1 Like

I guess this is the correct logg you are asking for :slight_smile:

Yes. That’s the correct one. Can you try following this solution: Deploying typescript serverless functions - #3 by dtchebotarev?

Of course I will :slight_smile: Thanks. I will let you know how it will go.

this is exactly it, @christonn93 .

So that error is showing because your code is currently written in a ESModule syntax which requires a little configuration for node to recognize vs the CommonJS style of scripts.

Not knowing how your code it setup, but this should be relatively low effort to get working. Definitely would recoomend leveraging TypeScript as Anshuman has referred, but let me know if you can’t get a solution deployed. Here to help!

Thanks :slight_smile:

I will try to sort out the code later today.
The code repo is this if you want to have a look :slight_smile:

1 Like

Taking a quick glance. I can see it is a combination of vite + an api route. I’m wondering if there is an issue with the setup between your typescript and your two separate apps (your vite site, and your api route).

This is actually a really good use-case for something like Next.js (not saying rewrite the app). But i’d suggest separating these and their configs more since Vite is building a client-side bundle where your API route is a node runtime bundle.

1 Like

The api route is for the email service only, the site itself is actually just a static spa using react.

But maybe I will have a look at building it in next in version two :slight_smile:
Just need ro learn next