Send transactional email from Node.js and Next.js
Install @hatang/sdk, verify your sending domain once, then call emails.send from server code. The API is Resend-compatible, and SMTP works too.
npm install @hatang/sdk
import { Hatang } from "@hatang/sdk";
const hatang = new Hatang(); // reads HATANG_API_KEY
const { data, error } = await hatang.emails.send(
{
subject: "Order #1024 is confirmed",
html: "<p>Hi Lan, your order <strong>#1024</strong> is confirmed.</p>",
text: "Hi Lan, your order #1024 is confirmed.",
},
{ idempotencyKey: "order-1024-confirmation" }, // a retry never sends twice
);
if (error) throw new Error(`${error.name}: ${error.message}`);
console.log(data.id);
In Next.js, call it from a Server Action or a Route Handler, never from a client component: the API key is a secret.
Checklist
- Create an API key with the
email:sendscope under Settings → API Keys and put it inHATANG_API_KEY. - Add and verify your domain: publish the DKIM, SPF and return-path records it lists. See Domains.
- Send. Follow delivery with
emails.get(id)or theemail.deliveredandemail.bouncedwebhooks.
Go further
- Every field, attachments, scheduling and batches: Send email
- Reusable templates with variables: Templates
- React Email components: pass
react: <WelcomeEmail />instead ofhtml(Node.js SDK) - Coming from Resend, SendGrid or Postmark: Migrate from Resend