Sending examples
Send with Next.js
Send from a Route Handler so the key never reaches the browser.
Prerequisites
- A Sendtern API key stored as
SENDTERN_API_KEY - A Next.js 13.4+ app using the App Router
- 1
Add the key
Add the key to
.env.local. Don't prefix it withNEXT_PUBLIC_, that would expose it to the browser.1SENDTERN_API_KEY=st_your_api_key - 2
Create a Route Handler
Create
app/api/send/route.js. It runs on the server only.1// app/api/send/route.js 2export async function POST(request) { 3 const { email } = await request.json(); 4 5 const res = await fetch("https://sendtern.com/emails", { 6 method: "POST", 7 headers: { 8 Authorization: `Bearer ${process.env.SENDTERN_API_KEY}`, 9 "Content-Type": "application/json", 10 }, 11 body: JSON.stringify({ 12 from: "Acme <hello@mail.example.com>", 13 to: [email], 14 subject: "Welcome to Acme", 15 html: "<p>Thanks for signing up.</p>", 16 }), 17 }); 18 19 return Response.json(await res.json(), { status: res.status }); 20} - 3
Check the response
A successful request returns
200 OKwith the email id. Use it to look the email up later.1{ 2 "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d" 3}
Replace the example sender with an address on your verified domain.