Registry / communication / mailslurp-client

mailslurp-client

JSON →
library17.5.0jsnpmunverified

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.

npm install mailslurp-client
INSTALL
IMPORT
SIG · MAILSLURP-CLIENT
M
mailslurp-client
communicationjavascriptv17.5.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MailSlurp
import { MailSlurp } from 'mailslurp-client'
const MailSlurp = require('mailslurp-client')
For CommonJS environments, `require('mailslurp-client').default` is typically needed to access the main `MailSlurp` class. Native ESM `import { MailSlurp }` is the standard for modern projects.
InboxController
import { InboxController } from 'mailslurp-client'
import InboxController from 'mailslurp-client'
Represents the controller for managing email inboxes. Other controllers like `SendController` or `WaitForController` are also available as named exports.
CreateInboxOptions
import type { CreateInboxOptions } from 'mailslurp-client'
Explicit type import for configuring inbox creation. Essential for TypeScript users to get correct type inference and auto-completion.

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);
Debug
Known issues
gotchaMailSlurp API operations, especially waiting for emails (`waitForController`), are designed to hold connections open and require sufficient timeouts. SMTP is an inherently slow protocol, and short timeouts can lead to test failures or unexpected behavior.
fix
Set appropriate timeouts for API calls, especially `waitForController` methods. The recommended timeout is 60,000 ms (60 seconds) or more for consistent email arrival.
affects: >=1.0.0
gotchaAn API Key is mandatory for all MailSlurp operations. Failing to provide a valid key will result in authentication errors and prevent any API calls from succeeding.
fix
Ensure `apiKey` is correctly provided during `MailSlurp` client instantiation. Obtain a key from the MailSlurp dashboard (mailslurp.com).
affects: >=1.0.0
gotchaThe MailSlurp client supports both CommonJS (`require`) and ES Module (`import`) syntax. Developers commonly make mistakes in combining these or using the wrong pattern for their environment, e.g., `require('pkg')` instead of `require('pkg').default` for the main class in CJS.
fix
For ES Modules, use `import { MailSlurp } from 'mailslurp-client';`. For CommonJS, use `const MailSlurp = require('mailslurp-client').default;` to access the primary class.
affects: >=3.0.0
gotchaWhile the client allows overriding the default `fetch` implementation, using a non-standard or incomplete polyfill in Node.js environments can lead to unexpected behavior or missing features compared to a robust solution like `cross-fetch`.
fix
If overriding `fetch`, consider using a well-maintained polyfill such as `cross-fetch` to ensure consistent and complete `fetch` API functionality across different JavaScript environments.
affects: >=1.0.0
Errors
Common errors & fixes
MailSlurp API Key not found. Please provide an API Key to the MailSlurp client.
The `apiKey` option was not provided or was empty when instantiating `new MailSlurp({ apiKey: '...' })`.
fix
Obtain your API Key from the MailSlurp dashboard (app.mailslurp.com) and pass it to the client: `const mailslurp = new MailSlurp({ apiKey: process.env.MAILSLURP_API_KEY });`
Error: Timeout of 60000ms exceeded
An API call, particularly a `waitForController` method, waited longer than the specified timeout for a condition (e.g., email arrival) to be met.
fix
Increase the `timeout` parameter for the specific MailSlurp operation, or globally configure the fetch client's timeout. SMTP can be slow, so 60-120 seconds might be necessary for reliability.
TypeError: (0 , mailslurp_client__WEBPACK_IMPORTED_MODULE_0__.MailSlurp) is not a constructor
Incorrect import statement for `MailSlurp` class. This typically indicates a mismatch between the module system being used (CommonJS vs. ESM) and the import syntax.
fix
For ES Modules: `import { MailSlurp } from 'mailslurp-client';`. For CommonJS: `const MailSlurp = require('mailslurp-client').default;`.
Upgrade
Version history
17.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources