Send email with Axios JS (and receive email too

Sending & Receiving Emails in Javascript made easy with Axios using MailSlurp API. Learn how to automate email testing and stay productive!

  • Table of contents

Axios JS sending emails Axios is a popular HTTP request library for Javascript and NodeJS. It lets you fetch webpages and call APIs from the browser and NodeJS in a reliable and friendly way. With a MailSlurp account Axios can also send and receive emails!

What is Axios?

You can install Axios from NPM as follows:

npm install axios

Making a request is easy once installed:

const axios = require("axios");
  .then(function (response) {
    // handle success
  .catch(function (error) {
    // handle error
  .then(function () {
    // always executed

What is MailSlurp?

MailSlurp is a free email API. You can create email addresses on demand then send and receive emails from Javascript. You can create a free account then use the API Key to send and receive emails with Axios.

How to ues email with Axios

To use MailSlurp with Axios first import the library and your API Key.

const axios = require("axios");
const API_KEY = "your-mailslurp-api-key";

Create an inbox

First we can create an email address like so:

async function createInbox() {
  // call MailSlurp createInbox endpoint
  return await axios
    .then((res) =>;

MailSlurp inboxes have real email addresses than can send and receive email.

  "id": "123",
  "emailAddress": ""

Send an email

Sending an email is easy with Axios and MailSlurp. Simple POST data to Include your API Key as a query parameter: ?apiKey=your-api-key.

// send email from inbox 1 to inbox 2
// NOTE you can send emails to any address with MailSlurp
await axios({
  method: "POST",
  url: `${API_KEY}`,
  data: {
    to: inbox2.emailAddress,
    subject: "Hello inbox 2",
    body: "Test from inbox 1",

Receive emails

You can receive emails with MailSlurp too!

// receive the email from inbox 2
const email = await axios
  .then((res) =>;
expect(email.subject).toEqual("Hello inbox 2");
expect(email.body).toEqual("Test from inbox 1");

Next steps

You can use Axios and MailSlurp for all your email needs. Sending, receiving, or even end-to-end testing.