> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sertifikasitrainer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Resend (email)

> Setup API key dan verifikasi domain untuk email transaksional.

AI Mentor mengirim email (invite peserta, notifikasi) lewat **Resend**. Kalau `RESEND_API_KEY` kosong, pengiriman di-skip — app tetap jalan.

<Frame>
  <img src="https://cdn.resend.com/brand/resend-wordmark-black.svg" alt="Logo Resend" width="160" />
</Frame>

**Dashboard:** [resend.com](https://resend.com)\
**Docs:** [resend.com/docs](https://resend.com/docs/introduction)

## Env di project

```bash theme={null}
RESEND_API_KEY=re_xxxxxxxx
EMAIL_FROM=AI Mentor <noreply@sertifikasitrainer.com>
```

Kode: `apps/api/src/email/email.service.ts`

## Setup lengkap

<Steps>
  <Step title="Daftar akun">
    Buat akun di [resend.com](https://resend.com/signup)
  </Step>

  <Step title="Tambah & verifikasi domain">
    Dashboard → **Domains** → **Add Domain** → masukkan `sertifikasitrainer.com` (atau subdomain `mail.`)

    Tambahkan record DNS yang ditampilkan Resend (biasanya):

    * **SPF** (TXT)
    * **DKIM** (CNAME `resend._domainkey`)
    * **DMARC** (TXT, opsional tapi disarankan)

    DNS bisa diatur di **Cloudflare DNS** (zone yang sama dengan domain utama).
  </Step>

  <Step title="Verifikasi">
    Klik **Verify DNS Records** di Resend. Propagasi bisa 5 menit–24 jam.
  </Step>

  <Step title="Buat API key">
    Dashboard → **API Keys** → **Create API Key**

    * Nama: mis. `trainerhub-prod`
    * Permission: **Sending access** (cukup untuk production)
    * Scope domain: `sertifikasitrainer.com` (lebih aman)

    Salin key `re_...` — **hanya ditampilkan sekali**.
  </Step>

  <Step title="Tes kirim">
    ```bash theme={null}
    curl -X POST 'https://api.resend.com/emails' \
      -H "Authorization: Bearer re_xxx" \
      -H "Content-Type: application/json" \
      -d '{
        "from": "AI Mentor <noreply@sertifikasitrainer.com>",
        "to": ["email-kamu@gmail.com"],
        "subject": "Tes Resend",
        "html": "<p>Hello dari TrainerHub</p>"
      }'
    ```

    Cek tab **Emails** di dashboard untuk status delivery.
  </Step>

  <Step title="Masukkan ke .env">
    ```bash theme={null}
    ./thub env merge
    ./thub secrets push   # production
    ```
  </Step>
</Steps>

## Tanpa domain terverifikasi (dev saja)

Resend menyediakan domain sandbox `onboarding@resend.dev` untuk testing — **jangan** dipakai production. Untuk production wajib domain verified.

## Troubleshooting

| Masalah                   | Solusi                                        |
| ------------------------- | --------------------------------------------- |
| Email tidak sampai        | Cek spam; pastikan SPF/DKIM hijau di Resend   |
| 403 dari API              | Key salah atau permission bukan Sending       |
| From address rejected     | `EMAIL_FROM` harus domain yang sudah verified |
| `[EmailService] skipping` | `RESEND_API_KEY` kosong di merged env         |

Docs: [Create API key](https://resend.com/docs/create-an-api-key) · [Managing domains](https://resend.com/docs/dashboard/domains)
