🌐 Available in other languages: Bahasa Indonesia | Français | Deutsch | 日本語 | 中文 | Español | Polski | Русский | Português | 한국어
This is a simple backend project that serves as an email sending service for contact forms. It is built using Node.js and the Resend email service, designed to be deployed as a Serverless Function on Vercel.
This solution is a simpler and more modern alternative to Gmail with OAuth2, requiring only an API key for authentication.
- Single API Endpoint: Accepts
POSTrequests containing form data (name,email,message). - Reliable Email Delivery: Uses Resend to send transactional emails.
- Ready for Vercel Deployment: Configured as a serverless function for scalability and ease of deployment.
- CORS Handling: Includes built-in CORS configuration to allow requests from your frontend domain.
- JSON Response: Provides clear status responses (
successorerror) for seamless frontend integration.
- Node.js (ES Modules)
- Resend
- Vercel Serverless Function
Follow these steps to get your email service up and running.
Before deploying, you’ll need an API Key and a Verified Domain from Resend.
- Create an Account: Sign up for a free account at resend.com.
- Verify Your Domain:
- In the Resend dashboard, go to "Domains".
- Add the domain you’ll use to send emails (e.g., your portfolio domain).
- Follow the instructions to add DNS records (typically MX and TXT) in your domain provider or DNS manager (like Netlify or Cloudflare).
- Create an API Key:
- Go to "API Keys".
- Click "Create API Key", give it a name, and set Permission to "Full Access".
- Copy and save the API Key — it will only be shown once.
- Clone or Fork This Repository: Get a copy of this project in your GitHub account.
- Import into Vercel:
- Log in to Vercel and click "Add New... > Project".
- Select your GitHub repository. Vercel will automatically detect it as a Node.js project.
- Set Environment Variables:
- In your Vercel project settings, go to "Environment Variables".
- Add the following variable:
- Name:
RESEND_API_KEY - Value: (Paste your Resend API key, starting with
re_...)
- Name:
- Deploy: Click "Deploy" to launch your project.
Once deployment completes, Vercel will provide a project URL (e.g., https://your-project-name.vercel.app).
Your API endpoint will be available at your Vercel deployment URL followed by the API path.
- URL:
https://your-project-name.vercel.app/api/handle-form - Method:
POST - Headers:
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"message": "Hello! I’m interested in collaborating with you."
}✅ Success (Status 200)
{
"status": "success",
"message": "Message sent successfully!"
}❌ Error (Status 400/500)
{
"status": "error",
"message": "All fields are required."
}Use fetch in your frontend JavaScript code to send form data to your deployed API endpoint.
// Example frontend implementation
const form = document.getElementById("contactForm");
form.addEventListener("submit", async (event) => {
event.preventDefault();
const apiUrl = "https://your-project-name.vercel.app/api/handle-form";
const formData = {
name: form.name.value,
email: form.email.value,
message: form.message.value,
};
try {
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
});
const data = await response.json();
if (data.status === "success") {
alert("Message sent successfully!");
form.reset();
} else {
alert("Error: " + data.message);
}
} catch (error) {
console.error("Error:", error);
alert("Failed to send message. Please try again later.");
}
});This project is licensed under the MIT License.