Skip to content

Commit b7bf928

Browse files
Arckie Jadulcoclaude
andcommitted
fix: defer Resend initialization to request time
Instantiating Resend at module load crashes the dev server when RESEND_API_KEY is not set. Move initialization inside the handler and add a missing-key guard. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 12195c2 commit b7bf928

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

server/api/contact.post.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Resend } from 'resend'
22

3-
const resend = new Resend(process.env.RESEND_API_KEY)
4-
53
export default defineEventHandler(async (event) => {
64
const { name, email, message } = await readBody(event)
75

@@ -12,7 +10,13 @@ export default defineEventHandler(async (event) => {
1210
throw createError({ statusCode: 400, statusMessage: 'Invalid email address' })
1311
}
1412

13+
const apiKey = process.env.RESEND_API_KEY
14+
if (!apiKey) {
15+
throw createError({ statusCode: 500, statusMessage: 'Email service not configured' })
16+
}
17+
1518
try {
19+
const resend = new Resend(apiKey)
1620
await resend.emails.send({
1721
from: 'Portfolio Contact <noreply@arckiejadulco.dev>',
1822
to: 'arckie.jadulco@gmail.com',

0 commit comments

Comments
 (0)