JSPM

mailapidev

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q53018F
  • License MIT

The official Node.js SDK for MailApi.dev - Send, Verify, and Update Contacts.

Package Exports

  • mailapidev

Readme

MailApiDev Node.js SDK

The official Node.js library for the MailApi.dev API.

Installation

npm install mailapidev

Usage

First, get your API key from your MailApi.dev dashboard.

import { MailApiDev } from 'mailapidev';

// Initialize the client with your API key
const mailapi = new MailApiDev(process.env.MAILAPI_KEY);

// All methods return a { data, error } object
// On success, `data` will be populated and `error` will be null.
// On failure, `data` will be null and `error` will be a MailApiError object.

1. Send an Email

Send a transactional email.

async function sendEmail() {
  const { data, error } = await mailapi.emails.send({
    from: "Your App <noreply@mail.mailapi.dev>",
    to: "recipient@example.com",
    subject: "Hello World!",
    message: "<p>It works!</p>",
    reply_to: "support@example.com"
  });

  if (error) {
    console.error("Failed to send email:", error.message);
    console.error("Error Code:", error.code);
    return;
  }

  console.log("Email sent successfully:", data.messageId);
  console.log("Credits Remaining:", data.creditsRemaining);
}

2. Update a Contact

Update an existing contact's properties in your project. This is perfect for syncing subscription status or user activity.

async function updateContact() {
  // Use mailapi.emails.update() instead of mailapi.contacts.update()
  const { data, error } = await mailapi.emails.update({
    email: "user@example.com",
    subscriptionStatus: "active",
    plan: {
      name: "Pro",
      amount: 29.99,
      interval: "month"
    },
    // ... other fields
  });

  if (error) {
    console.error("Failed to update contact:", error.message);
    return;
  }

  console.log("Contact updated:", data.data);
}

### 3. Verify an Email

Check if an email address is valid and deliverable.

```javascript
async function verifyEmail() {
  const { data, error } = await mailapi.emails.verify({
    email: "user@example.com"
  });

  if (error) {
    console.error("Failed to verify email:", error.message);
    console.error("Error Code:", error.code);
    return;
  }

  if (data.valid) {
    console.log("Email is valid and deliverable!");
  } else {
    console.log("Email is invalid:", data.message);
  }
  
  console.log("Validators:", data.validators);
}