MailSlurp Client for Email and SMS API

17.2.0 · active · verified Tue Apr 21

MailSlurp Client is the official JavaScript and TypeScript library for interacting with the MailSlurp Email and SMS API. It enables developers to programmatically create on-demand email addresses and phone numbers without managing a mail server, facilitating sending and receiving real emails and SMS messages directly from applications or automated tests. The library is currently stable at version 17.2.0 and maintains a regular release cadence. Its key differentiator is providing a fully functional, programmatic interface to an email and SMS infrastructure, making it ideal for robust end-to-end testing of communication workflows. It supports handling attachments, setting custom timeouts for message arrival, and offers both CommonJS and ES module import patterns. The client integrates with the standard `fetch` API and allows for custom `fetch` implementations.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates initializing the MailSlurp client with an API key, creating a temporary email inbox, sending an email from that inbox, and includes a placeholder for demonstrating email reception. It highlights common use cases for automated email testing and integration workflows.

import { MailSlurp } from 'mailslurp-client';

// Assuming a test runner like Jest is present for 'expect'
// In a standalone script, you might use console.assert or a custom check.
const expect = (value: any) => ({
  toContain: (substring: string) => {
    if (typeof value !== 'string' || !value.includes(substring)) {
      throw new Error(`Expected "${value}" to contain "${substring}"`);
    }
  },
  toEqual: (expected: any) => {
    if (value !== expected) {
      throw new Error(`Expected "${value}" to equal "${expected}"`);
    }
  }
});

async function runMailSlurpExample() {
  // Retrieve API Key from environment variables or provide directly
  const apiKey = process.env.MAILSLURP_API_KEY ?? 'YOUR_MAILSLURP_API_KEY';
  if (apiKey === 'YOUR_MAILSLURP_API_KEY') {
    console.warn('WARNING: Replace "YOUR_MAILSLURP_API_KEY" with your actual MailSlurp API Key from mailslurp.com dashboard.');
    return;
  }

  // Create a new MailSlurp client instance
  const mailslurp = new MailSlurp({ apiKey });

  console.log('Creating a new MailSlurp inbox...');
  // Create a new random inbox for testing
  const inbox = await mailslurp.inboxController.createInbox({});

  console.log(`Inbox created with email address: ${inbox.emailAddress}`);
  // Assert that the email address is valid
  expect(inbox.emailAddress).toContain('@');

  // Example: Send an email from the created inbox
  const recipientEmail = 'test-recipient@example.com'; // In a real scenario, this could be another MailSlurp inbox
  const emailSubject = 'Hello from MailSlurp!';
  await mailslurp.sendController.sendEmailAndConfirm({
    inboxId: inbox.id,
    sendEmailOptions: {
      to: [recipientEmail],
      subject: emailSubject,
      body: 'This is a test email sent using the MailSlurp client.',
    },
  });
  console.log(`Email sent from ${inbox.emailAddress} to ${recipientEmail}`);

  // To demonstrate receiving, let's assume `recipientEmail` is another MailSlurp inbox you control
  // Or if sending to itself for loopback test (not typical, but for demonstration):
  // const receivedEmail = await mailslurp.waitForController.waitForLatestEmail({
  //   inboxId: inbox.id,
  //   timeout: 60000,
  // });
  // console.log(`Received email with subject: "${receivedEmail.subject}"`);
  // expect(receivedEmail.subject).toEqual(emailSubject);
  console.log('MailSlurp client initialized and inbox created/sent email. Check your MailSlurp dashboard.');
}

runMailSlurpExample().catch(console.error);

view raw JSON →