Skip to content

NodeJS Guide

If you haven't already, see Getting Started. The following examples will assume you have already received your API key and created a namespace.

mailisk-node

Mailisk offers a NodeJS library mailisk-node. This is an official library that wraps the API Reference endpoints into functions.

See the README for more usage examples.

Installation

First install the library using npm

shell
npm install --save-dev mailisk

Or yarn

shell
yarn add mailisk --dev

Setup Client

Once installed import the library and create the client by passing in your API key

js
const { MailiskClient } = require("mailisk");

const mailisk = new MailiskClient({ apiKey: "YOUR_API_KEY" });

Reading Email

You can read emails using the searchInbox function.

  • By default it uses the wait flag. This means the call won't return until at least one email is received. Disabling this flag via wait: false can cause it to return an empty response immediately.
  • The request timeout is adjustable by passing timeout in the request options. By default it uses a timeout of 5 minutes.
  • By default it returns emails in the last 15 minutes. This ensures that only new emails are returned. Without this, older emails would also be returned, potentially disrupting you if you were waiting for a specific email. This can be overridden by passing the from_timestamp parameter (from_timestamp: 0 will disable filtering by email age).
js
// timeout of 5 minutes
await mailisk.searchInbox(namespace);
// timeout of 1 minute
await mailisk.searchInbox(namespace, {}, { timeout: 1000 * 60 });
// returns immediately, even if the result would be empty
await mailisk.searchInbox(namespace, { wait: false });

The response will look similar to this:

json
{
  "total_count": 1,
  "options": {
    "limit": 10,
    "offset": 0
  },
  "data": [
    {
      "id": "1659368409795-42UcuQtMy",
      "from": {
        "address": "contact@mailisk.com",
        "name": ""
      },
      "to": [
        {
          "address": "john@sh7o3t3e4jvj.mailisk.net",
          "name": ""
        }
      ],
      "subject": "Test - Welcome to Mailisk 👋",
      "html": "<html>...",
      "text": "*Welcome to Mailisk*\n\nHello there 👋\nWelcome to Mailisk! We're excited to have you!\n\nGo ahead and ...",
      "received_date": "2022-08-01T15:40:09.000Z",
      "received_timestamp": 1659368409,
      "expires_timestamp": 1659372009
    }
  ]
}

The total_count tells us the total number of emails that match our query, depending on limit and offset a subset of this will be returned. See the Search Inbox Response for more information on the other fields.

Sending Email

Mailisk supports sending an email using Virtual SMTP. This will fetch the SMTP settings for the selected namespace and send an email. These emails can only be sent to an address that ends in @{namespace}.mailisk.net.

js
const namespace = "mynamespace";

await mailisk.sendVirtualEmail(namespace, {
  from: "test@example.com",
  to: `john@${namespace}.mailisk.net`,
  subject: "This is a test",
  text: "Testing",
});

Request SMS Numbers

Request and activate an SMS number from the dashboard (see SMS Testing). Surface it to your Node tests via configuration, e.g. process.env.MAILISK_SMS_NUMBER, so your specs all hit the same destination.

Listing SMS Numbers

Surface the list of approved numbers tied to your API key with listSmsNumbers(). This is useful for one-time setup scripts or sanity checks in CI before running OTP flows.

js
const { data: smsNumbers } = await mailisk.listSmsNumbers();
if (!smsNumbers.length) throw new Error("No SMS numbers available");

const activeNumber = smsNumbers.find((num) => num.status === "active");
if (!activeNumber) throw new Error("Activate an SMS number before running tests");
console.log(`Using SMS number ${activeNumber.phone_number}`);

Reading SMS

Use searchSmsMessages(phoneNumber, filters?, options?) to poll for inbound texts. It follows the same defaults as searchInbox:

  • wait defaults to true, holding the request open until at least one SMS matches the filters.
  • The third options argument lets you override the 5 minute timeout to handle slower OTP flows.
  • Filter arguments align with the Search SMS API: body, from_number, from_date, to_date, limit, offset, etc.
js
const smsNumber = process.env.MAILISK_SMS_NUMBER;
const { data: smsMessages } = await mailisk.searchSmsMessages(
  smsNumber,
  {
    body: "Your login code",
  },
  {
    timeout: 1000 * 120,
  }
);

if (!smsMessages.length) throw new Error("SMS not received in time");
const sms = smsMessages[0];
const otp = sms.body.match(/(\d{6})/)[1];

If you need to reuse the same number across test runs, store run-specific state in your application (user IDs, tenants, etc.) and filter using the message body. Alternatively, avoid running parallel tests that share the same number, and set from_date to exclude messages created before the current test run.

An example with test Playwright is available here.

Sending Virtual SMS

To simulate inbound SMS (useful for previewing one-off notifications or driving tests without your production provider), call sendVirtualSms. Provide both numbers in E.164 or local dial format; Mailisk will deliver the SMS into the destination number you own.

js
await mailisk.sendVirtualSms({
  from_number: "+15551234567",
  to_number: process.env.MAILISK_SMS_NUMBER,
  body: "Your login code is 123456",
});

Virtual SMS do not consume your regular inbound quota and land in the same search results as provider-delivered messages, so you can keep using searchSmsMessages to assert content.

Breaking changes

  • With version v2.0.0 the default from_timestamp has been changed from the past 5 seconds to the past 15 minutes. Since this can break existing tests a new major version has been released.