#!/usr/bin/env tsx /** * Simple Resend Webhook Test * * Tests the webhook endpoint directly without needing database records. * * Usage: * npx tsx scripts/test-resend-webhook-simple.ts */ const BASE_URL = process.env.APP_URL || 'http://localhost:3001'; async function testWebhook() { console.log('šŸ” Testing Resend Webhook Endpoint\n'); console.log(` URL: ${BASE_URL}/api/webhooks/resend\n`); // Test 1: GET request (should return webhook info) console.log('1ļøāƒ£ Testing GET request (webhook info)...'); try { const getResponse = await fetch(`${BASE_URL}/api/webhooks/resend`); const getData = await getResponse.json(); console.log(` Status: ${getResponse.status}`); console.log(` Response:`, JSON.stringify(getData, null, 2)); console.log(''); } catch (error) { console.log(` āŒ Error:`, error); } // Test 2: POST request with fake webhook payload console.log('2ļøāƒ£ Testing POST request (simulated webhook)...'); const fakeEmailId = 'test-email-' + Date.now(); const payload = { type: 'email.delivered', created_at: new Date().toISOString(), data: { email_id: fakeEmailId, from: 'noreply@themoyos.co.za', to: ['test@example.com'], subject: 'Test Email', created_at: new Date().toISOString(), }, }; try { const postResponse = await fetch(`${BASE_URL}/api/webhooks/resend`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), }); const postData = await postResponse.json(); console.log(` Status: ${postResponse.status}`); console.log(` Response:`, JSON.stringify(postData, null, 2)); if (postResponse.ok) { console.log('\nāœ… Webhook endpoint is working!'); console.log(' (Email not found in DB is expected for this test)'); } else { console.log('\nāš ļø Webhook returned an error'); } } catch (error) { console.log(` āŒ Error:`, error); } console.log('\nšŸ“‹ Summary:'); console.log(' - If you see "Email not found in logs" - webhook is working correctly'); console.log(' - The webhook only updates emails that exist in your database'); console.log(' - To test with real data:'); console.log(' 1. Send an email via the admin dashboard'); console.log(' 2. Deploy to production OR use ngrok for local testing'); console.log(' 3. Resend will send webhooks to your production URL'); } testWebhook().catch(console.error);