How to use EmailTemplate in Next.js (App Router)
In Next.js your EmailTemplate API key should only live on the server. The cleanest pattern is a route handler or server action that calls POST /api/v1/templates/:id/render and returns the HTML, decoupled from whatever sends it afterwards. This guide shows the pattern with the App Router, environment variables and React Email on the Node runtime.
How do I render email templates in a Next.js route handler?
-
Define environment variables
Your API key must never appear in the client bundle. Keep it in .env.local and only expose a public prefix through NEXT_PUBLIC_.
bash# .env.local EMAIL_TEMPLATE_API_KEY=et_your_key EMAIL_TEMPLATE_API_URL=https://api.emailtemplate.theboomer.dev/api/v1 ORDER_TEMPLATE_ID=64f8a1b2c3d4e5f6a7b8c9d1 -
Create a route handler that renders
A server route handler calls the EmailTemplate render endpoint and returns the HTML. The key never leaks and the client only sees the result.
ts// src/app/api/email/render/route.ts import { NextResponse } from 'next/server' export async function POST(request: Request) { const body = await request.json() const res = await fetch( process.env.EMAIL_TEMPLATE_API_URL + '/templates/' + process.env.ORDER_TEMPLATE_ID + '/render', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': process.env.EMAIL_TEMPLATE_API_KEY as string }, body: JSON.stringify({ variables: body.variables }) } ) if (!res.ok) { return NextResponse.json({ error: 'Render failed' }, { status: res.status }) } const data = await res.json() return NextResponse.json({ html: data.html, subject: data.subject }) } -
Use a server action to send
The server action orchestrates: render with EmailTemplate and send with your ESP. The client invokes the action without touching credentials.
ts// src/app/actions/send-order.ts 'use server' export async function sendOrderConfirmation(order: { id: string; email: string; total: string }) { const res = await fetch(process.env.EMAIL_TEMPLATE_API_URL + '/templates/' + process.env.ORDER_TEMPLATE_ID + '/render', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': process.env.EMAIL_TEMPLATE_API_KEY as string }, body: JSON.stringify({ variables: { 'user.name': order.email, 'order.id': order.id, 'order.total': order.total } }) }) const { html, subject } = await res.json() // Send html with Klaviyo, Mailchimp or Resend here return { ok: res.ok, subject } } -
Render React Email on the server
If you prefer authoring with React Email, force the Node runtime and use @react-email/render to get the HTML before storing it in EmailTemplate.
ts// src/app/api/email/compile/route.ts export const runtime = 'nodejs' import { render } from '@react-email/render' import { OrderConfirmation } from '@/emails/OrderConfirmation' export async function POST() { const html = render(OrderConfirmation({ userName: 'Ana', orderId: 'A-1024', orderTotal: '89.00 EUR' })) return Response.json({ html }) } -
Handle errors and retries
A render can fail because of the network or a 429. Retry with backoff and never block the user response: enqueue the send.
tsasync function renderWithRetry(url: string, init: RequestInit, attempts = 3) { for (let i = 0; i < attempts; i++) { const res = await fetch(url, init) if (res.ok) return res if (res.status !== 429 && res.status < 500) throw new Error('Render error ' + res.status) await new Promise((r) => setTimeout(r, 500 * 2 ** i)) } throw new Error('Render unavailable') }
| Item | Free | Pro | Team | Enterprise |
|---|---|---|---|---|
| Monthly price (EUR, VAT excluded) | €0 | €19 | €59 | €149 |
| Renders per month | 500 | 25,000 | 100,000 | 500,000 |
| Concurrent templates | 3 | 25 | Unlimited | Unlimited |
| Users | 1 | 2 | 10 | 25 |
| AI credits per month | 50 | 500 | 2,000 | 10,000 |
| AI operation cost | generate: 10 credits · suggest: 5 credits. Charged to the credit wallet (they do not consume renders) and the same on every plan. Packs: 500 credits / €9, 2,000 / €29, 5,000 / €69, 10,000 / €119. | |||
| Stack provider export | format=resend returns the React Email component as JSX (text/javascript) you can reuse in the App Router; format=klaviyo returns Liquid HTML and format=mailchimp returns JSON. | |||
Subscription prices in USD/month; credit packs are charged in EUR. Product-published data, not estimates.
Frequently asked questions
Can I call the EmailTemplate API from the client?
No. Your API key would be exposed in the bundle. Always use a route handler or a server action on the server.
Next.js or React Email?
They complement each other. React Email handles component authoring; EmailTemplate provides versioning, API rendering and export to Klaviyo, Mailchimp and Resend.
How do I test the render without sending emails?
Use the dashboard preview or the render endpoint response: it returns the final HTML without delivering any message.