examples
Send Email with Axios in JavaScript using MailSlurp
Use Axios with the MailSlurp email API to create inboxes, send messages, and verify received email from JavaScript and Node.js.
Axios is a popular HTTP request library for JavaScript and Node.js. It lets you call APIs from browser and server environments with simple request patterns.
With a MailSlurp account, Axios can power full email workflows: create inboxes, send messages, and validate received content in tests.
Use this example after the JavaScript send email guide when you need a runnable API-based workflow for Node.js, a backend route, or CI.
Executive summary
- Use Axios to call MailSlurp inbox and email endpoints.
- Create temporary inboxes per run to isolate tests.
- Send messages via API and assert subject/body on receipt.
- Keep the API key server-side or in CI secrets.
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");
axios
.get("/user?ID=12345")
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
What is MailSlurp?
MailSlurp is an email API for testing and automation. You can create email addresses on demand, then send and receive emails from JavaScript. You can create an account and use your API key from a trusted runtime.
How to use email with Axios
To use MailSlurp with Axios, first import the library and create a client. Keep the API key in a server-side environment variable; do not put it in browser JavaScript.
const axios = require("axios");
const client = axios.create({
baseURL: "https://api.mailslurp.com",
headers: {
"x-api-key": process.env.MAILSLURP_API_KEY,
},
});
Create an inbox
First we can create an email address like so:
async function createInbox() {
return await client.post("/inboxes").then((res) => res.data);
}
MailSlurp inboxes have real email addresses that can send and receive email.
{
"id": "123",
"emailAddress": "123@mailslurp.com"
}
Send an email
Sending email with Axios is straightforward using MailSlurp's send endpoint. POST data to https://api.mailslurp.com/sendEmail with your API key in the x-api-key header.
// send email from inbox 1 to inbox 2
await client({
method: "POST",
url: "/sendEmail",
data: {
senderId: inbox1.id,
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 client
.get("/waitForLatestEmail", {
params: {
inboxId: inbox2.id,
timeout: 60000,
unreadOnly: true,
},
})
.then((res) => res.data);
expect(email.from).toEqual(inbox1.emailAddress);
expect(email.subject).toEqual("Hello inbox 2");
expect(email.body).toEqual("Test from inbox 1");
Next steps
You can use Axios and MailSlurp for sending, receiving, and end-to-end email testing.
- Send email with JavaScript guide
- Email integration testing product
- Temporary email API
- More JavaScript email examples
Axios production checklist
Before shipping Axios-based email flows:
- Validate send/receive behavior in Email Sandbox.
- Add deterministic coverage using Email Integration Testing.
- Capture async payloads with Email Webhooks.
- Route retries and parsing via Email Automation Routing.
- Confirm sender and inbox outcomes through an Email Deliverability Test.