How to Use Notify with TypeScript/JavaScript

Send your first email with Notify using pure TypeScript/JavaScript and the Fetch API. No dependencies needed.

Prerequisites

Before you start, make sure you have:

  • Your API key – We generated one for you when you signed up. This authenticates your app and lets you send emails.
  • A verified domain (optional) – Not required for testing. Trial accounts can send up to 100 emails total (10/hr limit). To scale beyond that, verify your domain for better deliverability.

1. Send an Email

Call the Notify API directly with fetch:

async function sendEmail() {
  try {
    const response = await fetch('https://notify.cx/api/public/v1/send-email', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': 'YOUR_NOTIFY_API_KEY'
      },
      body: JSON.stringify({
        to: 'recipient@example.com',
        subject: 'Hello world',
        name: 'John Doe',
        message: 'Your email content here' // Plain text or HTML
      })
    });

    if (!response.ok) {
      throw new Error(`Failed to send email: ${response.statusText}`);
    }

    console.log('Email sent successfully:', await response.json());
  } catch (error) {
    console.error('Failed to send email:', error);
  }
}

sendEmail();

Run this in a browser console, Deno, Bun, or any JS/TS environment that supports fetch. Keep your API key on the server whenever possible.


2. Sending Template-Based Emails

If you already have Notify templates, use the template endpoint instead of hand-writing HTML in the message field. Template creation is deprecated — see Templates (Deprecated).

async function sendTemplatedEmail() {
  try {
    const response = await fetch(
      'https://notify.cx/api/public/v1/send-email-from-template',
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': 'YOUR_NOTIFY_API_KEY'
        },
        body: JSON.stringify({
          to: 'recipient@example.com',
          from: 'noreply@notify.cx',
          templateId: '<your_template_id>',
          variables: {
            name: 'John Doe',
            company: 'Example Inc.'
          }
        })
      }
    );

    if (!response.ok) {
      throw new Error(
        `Failed to send email from template: ${response.statusText}`
      );
    }

    console.log(
      'Email from template sent successfully:',
      await response.json()
    );
  } catch (error) {
    console.error('Failed to send email from template:', error);
  }
}

sendTemplatedEmail();

Always wrap API calls in try/catch and check response.ok.


Where To Next?

Now you’re ready to send emails with Notify using pure TypeScript/JavaScript! 🚀