Fix: Initialize Resend client in handler to access runtime env vars

This commit is contained in:
Daniel LaForce 2025-04-28 19:45:50 -06:00
parent 6698c24a09
commit 0a0dd5f527
1 changed files with 5 additions and 5 deletions

View File

@ -1,10 +1,6 @@
import type { APIRoute } from 'astro';
import { Resend } from 'resend';
// Initialize Resend with API key from environment variables
// IMPORTANT: Set RESEND_API_KEY in your deployment environment (e.g., Cloudflare Pages)
const resend = new Resend(import.meta.env.RESEND_API_KEY);
// Define the expected structure of the form data
interface FormData {
name: string;
@ -14,6 +10,10 @@ interface FormData {
}
export const POST: APIRoute = async ({ request }) => {
// Initialize Resend inside the handler to access runtime env vars/secrets
// IMPORTANT: Set RESEND_API_KEY in your deployment environment (e.g., Cloudflare Pages)
const resend = new Resend(import.meta.env.RESEND_API_KEY);
// Check if API key is configured
if (!import.meta.env.RESEND_API_KEY) {
console.error("Resend API key is not configured.");
@ -56,7 +56,7 @@ export const POST: APIRoute = async ({ request }) => {
<p><strong>Message:</strong></p>
<p>${data.message.replace(/\n/g, '<br>')}</p>
`,
reply_to: data.email, // Set the reply-to header to the sender's email
replyTo: data.email, // Set the reply-to header to the sender's email
});
if (emailError) {