POST/v1/emails

Kirim Email

Kirim email transaksional ke satu atau banyak penerima. Domain pengirim harus sudah diverifikasi.

Request Body

fromstringwajib

Alamat pengirim. Format: Nama <email@domain.com>. Domain harus sudah diverifikasi.

tostring | string[]wajib

Penerima. Bisa satu alamat atau array (maks 50 penerima).

subjectstringwajib

Subjek email. Maks 255 karakter.

htmlstring

Konten HTML. Wajib ada html atau text.

textstring

Konten plain text. Digunakan sebagai fallback.

templateIdstring

ID template. Jika diisi, html dan text diabaikan.

variablesobject

Variabel untuk mengisi template. Contoh: { "nama": "Budi", "kode": "1234" }

replyTostring

Alamat email untuk balasan. Opsional.

Contoh

cURL

curl -X POST https://api.ayosend.id/v1/emails \
  -H "Authorization: Bearer ays_xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "from": "Toko <no-reply@toko.com>",
    "to": ["pelanggan@email.com"],
    "subject": "Kode OTP kamu",
    "html": "<p>Kode OTP: <strong>847291</strong>. Berlaku 5 menit.</p>"
  }'

Node.js

const res = await fetch('https://api.ayosend.id/v1/emails', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.AYOSEND_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    from: 'Toko <no-reply@toko.com>',
    to: ['pelanggan@email.com'],
    subject: 'Kode OTP kamu',
    html: '<p>Kode OTP: <strong>847291</strong></p>',
  }),
})
const data = await res.json()
console.log(data.id) // em_clxxx

PHP

$response = json_decode(file_get_contents(
  'https://api.ayosend.id/v1/emails', false,
  stream_context_create(['http' => [
    'method'  => 'POST',
    'header'  => implode("\r\n", [
      'Authorization: Bearer ' . $_ENV['AYOSEND_API_KEY'],
      'Content-Type: application/json',
    ]),
    'content' => json_encode([
      'from'    => 'Toko <no-reply@toko.com>',
      'to'      => ['pelanggan@email.com'],
      'subject' => 'Kode OTP kamu',
      'html'    => '<p>Kode OTP: <strong>847291</strong></p>',
    ]),
  ]])
), true);
echo $response['id'];

Response

200 OK

{
  "id": "em_clxxxxxxxxxxxxxxxx",
  "status": "queued"
}
StatusKeterangan
200Email masuk antrian
400Domain tidak diverifikasi / validasi gagal
401API key tidak valid
402Kredit tidak cukup — silakan top up
429Rate limit tercapai