# 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.

```bash
npm install @hatang/sdk
```

```ts
import { Hatang } from "@hatang/sdk";

const hatang = new Hatang(); // reads HATANG_API_KEY

const { data, error } = await hatang.emails.send(
  {
    from: "Acme <orders@example.com>", // a domain verified under Email → Domains
    to: "lan@example.net",
    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

1. Create an API key with the `email:send` scope under **Settings → API Keys** and put it in `HATANG_API_KEY`.
2. Add and verify your domain: publish the DKIM, SPF and return-path records it lists. See [Domains](https://hatang.dev/docs/guides/email/domains.md).
3. Send. Follow delivery with `emails.get(id)` or the `email.delivered` and `email.bounced` [webhooks](https://hatang.dev/docs/guides/getting-started/webhooks.md).

## Go further

- Every field, attachments, scheduling and batches: [Send email](https://hatang.dev/docs/guides/email/sending.md)
- Reusable templates with variables: [Templates](https://hatang.dev/docs/guides/email/templates.md)
- React Email components: pass `react: <WelcomeEmail />` instead of `html` ([Node.js SDK](https://hatang.dev/docs/guides/sdks/node.md))
- Coming from Resend, SendGrid or Postmark: [Migrate from Resend](https://hatang.dev/docs/guides/email/migrate-from-resend.md)

Source: https://hatang.dev/docs/guides/recipes/send-email-nodejs
