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

# SendGrid

> Kirim email transaksional berdasarkan peristiwa Dodo Payments menggunakan API Kirim Email SendGrid.

## Pendahuluan

Kirim email transaksional secara otomatis untuk konfirmasi pembayaran, pembaruan langganan, dan pemberitahuan penting menggunakan SendGrid. Trigger email yang dipersonalisasi berdasarkan peristiwa pembayaran dengan konten dinamis dan template profesional.

<Info>
  Integrasi ini membutuhkan SendGrid API Key dengan izin Mail Send.
</Info>

## Memulai

<Steps>
  <Step title="Open the Webhook Section">
    Di dashboard Dodo Payments Anda, navigasikan ke <b>Webhooks → + Add Endpoint</b> dan perluas dropdown integrasi.

    <Frame>
      <img src="https://mintcdn.com/dodopayments/slbAEdrLLwKHfaRf/images/integrations/sendgrid.png?fit=max&auto=format&n=slbAEdrLLwKHfaRf&q=85&s=bb13e15660cf9765d6501190d738525d" alt="Add Endpoint and integrations dropdown" style={{ maxHeight: '500px', width: 'auto' }} width="1682" height="944" data-path="images/integrations/sendgrid.png" />
    </Frame>
  </Step>

  <Step title="Select SendGrid">
    Pilih kartu integrasi <b>SendGrid</b>.
  </Step>

  <Step title="Enter API Key">
    Masukkan SendGrid API Key Anda di konfigurasi.
  </Step>

  <Step title="Configure Transformation">
    Sunting kode transformasi untuk memformat email sesuai Mail Send API SendGrid.
  </Step>

  <Step title="Test & Create">
    Uji dengan payload sampel dan klik <b>Create</b> untuk mengaktifkan pengiriman email.
  </Step>

  <Step title="Done!">
    🎉 Event pembayaran kini secara otomatis akan memicu email transaksional melalui SendGrid.
  </Step>
</Steps>

## Contoh Kode Transformasi

### Email Konfirmasi Pembayaran

```javascript payment_confirmation.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.succeeded") {
    const p = webhook.payload.data;
    webhook.url = "https://api.sendgrid.com/v3/mail/send";
    webhook.payload = {
      personalizations: [
        {
          to: [{ email: p.customer.email }],
          dynamic_template_data: {
            customer_name: p.customer.name,
            payment_amount: (p.total_amount / 100).toFixed(2),
            payment_id: p.payment_id,
            payment_date: new Date(webhook.payload.timestamp).toLocaleDateString(),
            currency: p.currency || "USD"
          }
        }
      ],
      from: {
        email: "payments@yourdomain.com",
        name: "Your Company"
      },
      template_id: "d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
  }
  return webhook;
}
```

### Email Selamat Datang Langganan

```javascript subscription_welcome.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "subscription.active") {
    const s = webhook.payload.data;
    webhook.url = "https://api.sendgrid.com/v3/mail/send";
    webhook.payload = {
      personalizations: [
        {
          to: [{ email: s.customer.email }],
          dynamic_template_data: {
            customer_name: s.customer.name,
            subscription_id: s.subscription_id,
            product_name: s.product_id,
            amount: (s.recurring_pre_tax_amount / 100).toFixed(2),
            frequency: s.payment_frequency_interval,
            next_billing: new Date(s.next_billing_date).toLocaleDateString()
          }
        }
      ],
      from: {
        email: "welcome@yourdomain.com",
        name: "Your Company"
      },
      template_id: "d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
  }
  return webhook;
}
```

### Pemberitahuan Kegagalan Pembayaran

```javascript payment_failure.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.failed") {
    const p = webhook.payload.data;
    webhook.url = "https://api.sendgrid.com/v3/mail/send";
    webhook.payload = {
      personalizations: [
        {
          to: [{ email: p.customer.email }],
          dynamic_template_data: {
            customer_name: p.customer.name,
            payment_amount: (p.total_amount / 100).toFixed(2),
            error_message: p.error_message || "Payment processing failed",
            payment_id: p.payment_id,
            retry_link: `https://yourdomain.com/retry-payment/${p.payment_id}`
          }
        }
      ],
      from: {
        email: "support@yourdomain.com",
        name: "Your Company Support"
      },
      template_id: "d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    };
  }
  return webhook;
}
```

## Tips

* Gunakan template dinamis SendGrid untuk konten yang dipersonalisasi
* Sertakan data pembayaran yang relevan dalam variabel template
* Atur alamat pengirim dan nama pengirim yang tepat
* Gunakan ID template untuk format email yang konsisten
* Sertakan tautan berhenti berlangganan untuk kepatuhan

## Pemecahan Masalah

<AccordionGroup>
  <Accordion title="Emails not being sent">
    * Verifikasi bahwa API Key memiliki izin Mail Send
    * Periksa apakah ID template valid dan aktif
    * Pastikan alamat email penerima valid
    * Tinjau batasan dan kuota pengiriman SendGrid
  </Accordion>

  <Accordion title="Transformation errors">
    * Validasi struktur JSON sesuai format API SendGrid
    * Periksa bahwa semua bidang yang diperlukan ada
    * Pastikan variabel data template diformat dengan benar
    * Verifikasi alamat email pengirim sudah diverifikasi di SendGrid
  </Accordion>
</AccordionGroup>
