Invio di esempi
Invia con Next.js
Invia da un Route Handler in modo che la chiave non raggiunga mai il browser.
Prerequisiti
- Un Sendtern chiave API memorizzato come
SENDTERN_API_KEY - A Next.js 13.4+ app using the App Router
- 1
Aggiungere la chiave
Aggiungere la chiave a
.env.local, non prefiggerla conNEXT_PUBLIC_, che la esporrebbe al 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
Controlla la risposta
Una richiesta riuscita restituisce
200 OKcon l'ID email. Usalo per cercare l'email in seguito.1{ 2 "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d" 3}
Sostituisci il mittente di esempio con un indirizzo sul tuo dominio verificato.