Next.js
Use the Posthawk Node.js SDK with Next.js Server Actions and API Routes.
SDK
use serverSend emails from Next.js Server Actions. The SDK works in any server-side context.
Example
typescript
// app/actions/send-email.ts
'use server';
import { Posthawk } from 'posthawk';
const posthawk = new Posthawk(process.env.POSTHAWK_API_KEY!);
export async function sendWelcomeEmail(email: string, name: string) {
const { data, error } = await posthawk.emails.send({
from: 'hello@yourdomain.com',
to: email,
subject: `Welcome, ${name}!`,
html: `<h1>Welcome, ${name}!</h1><p>Thanks for signing up.</p>`,
});
if (error) throw new Error(error.message);
return data;
}SDK
app/api/send/route.tsUse the SDK in a Next.js API route handler.
Example
typescript
import { Posthawk } from 'posthawk';
import { NextRequest, NextResponse } from 'next/server';
const posthawk = new Posthawk(process.env.POSTHAWK_API_KEY!);
export async function POST(request: NextRequest) {
const { to, subject, html } = await request.json();
const { data, error } = await posthawk.emails.send({
from: 'hello@yourdomain.com', to, subject, html,
});
if (error) return NextResponse.json({ error: error.message }, { status: error.statusCode });
return NextResponse.json(data);
}