Skip to content
On this page

NodeJS Guide

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

If you're using working with Node and using Cypress check out the Cypress Guide.

mailisk-node

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

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.

  • It uses the wait property by default. This means the call won't timeout until a result is returned or 5 minutes pass. This is adjustable by passing the timeout paramter (it can also be disabled by passing wait: false).
  • It uses a default from_timestamp of current timestamp - 5 seconds. This means that older emails will be ignored. This can be overriden by passing the from_timestamp parameter (from_timestmap: 0 will disable filtering by email age).
js
const { data: emails } = await mailisk.searchInbox(namespace, {
  to_addr_prefix: "john@mynamespace.mailisk.net",
  limit: 10,
});

This snippet will read the last 10 emails that were sent to your namespace with the address john@mynamespace.mailisk.net.

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,
      "spam_score": 1.6
    }
  ]
}

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",
});