← All Guides
How to Send Emails with Next.js
Learn how to send transactional emails from your Next.js application using AISend. This guide covers setup, sending your first email, and handling delivery events.
1. Install the AISend SDK
Add the AISend package to your Next.js project:
npm install aisend2. Set your API key
Add your AISend API key to your environment variables:
# .env.local
AISEND_API_KEY=as_your_api_key3. Create an API route
Create a Next.js API route handler to send emails:
// app/api/send/route.ts
import { AISend } from 'aisend';
import { NextResponse } from 'next/server';
const aisend = new AISend(process.env.AISEND_API_KEY!);
export async function POST(request: Request) {
const { to, subject, html } = await request.json();
const result = await aisend.emails.send({
from: 'hello@yourdomain.com',
to,
subject,
html,
});
return NextResponse.json(result);
}4. Send from your frontend
Call your API route from any component:
await fetch('/api/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
to: 'user@example.com',
subject: 'Welcome!',
html: '<p>Thanks for signing up.</p>',
}),
});5. Set up webhooks (optional)
Configure webhooks in the AISend dashboard to receive delivery events (sent, delivered, bounced) at your API endpoint. This lets you track email status in real-time.