Mailinator API Client
The `mailinator-client` package provides the official JavaScript SDK for interacting with the Mailinator REST API. It acts as a thin, typed wrapper around the Mailinator API, with its structure and available endpoints derived directly from the Mailinator OpenAPI specification. Currently at version 1.1.0, the library leverages Microsoft's `typed-rest-client` for handling HTTP requests, ensuring all API interactions are asynchronous. While no explicit release cadence is provided, the project follows standard npm versioning practices. Its primary differentiation is being the officially supported client, providing a more robust and up-to-date integration compared to manually crafting API requests. It supports both JavaScript and TypeScript applications, shipping with its own type definitions.
Common errors
-
Error: MAILINATOR_API_KEY environment variable is not set. Please provide your API key.
cause The Mailinator API Key, essential for authentication, was not found in the environment variables when the application attempted to initialize the client.fixSet the `MAILINATOR_API_KEY` environment variable with your valid Mailinator API key. For local development, use a `.env` file or export it in your shell. For production, use secure secret management systems. -
Failed to retrieve messages: Invalid API Key
cause The provided Mailinator API Key is either incorrect, expired, or does not have sufficient permissions for the requested operation.fixVerify that your `MAILINATOR_API_KEY` is correct by logging into your Mailinator account and checking your API key settings. Ensure it has the necessary permissions for the operations you are performing (e.g., fetching inbox messages). -
TypeError: Cannot read properties of undefined (reading 'getInboxMessages') OR TypeError: mailinatorClient.inbox.getInboxMessages is not a function
cause The assumed structure of the client object or the method name for accessing specific API endpoints (e.g., `inbox.getInboxMessages`) is incorrect, or the method simply doesn't exist.fixConsult the official `REFERENCE.md` document or the Mailinator API documentation to confirm the exact client structure and method names. The client might expose methods directly, under a different namespace, or require different parameters.
Warnings
- deprecated Some 'Request classes' within the SDK are explicitly marked as deprecated and are slated for removal in future versions. Developers should consult the `ROADMAP.md` and `REFERENCE.md` files for details and plan for migration.
- gotcha Hardcoding your Mailinator API Key directly into source code is a significant security risk. API keys should be treated as sensitive credentials.
- gotcha Mailinator, like many external APIs, has rate limits. Exceeding these limits can lead to temporary blocks or throttled requests.
Install
-
npm install mailinator-client -
yarn add mailinator-client -
pnpm add mailinator-client
Imports
- MailinatorClient
const MailinatorClient = require('mailinator-client');import { MailinatorClient } from 'mailinator-client'; - Mailinator API Response Types
import type { InboxMessage, Inbox } from 'mailinator-client'; - CommonJS Import
import MailinatorClient from 'mailinator-client';
const { MailinatorClient } = require('mailinator-client');
Quickstart
import { MailinatorClient } from 'mailinator-client';
import type { InboxMessage } from 'mailinator-client';
async function fetchInboxMessages() {
// Ensure your Mailinator API Key is set in your environment variables (e.g., .env file)
// Get an API key from https://www.mailinator.com/docs/api
const apiKey = process.env.MAILINATOR_API_KEY ?? '';
if (!apiKey) {
console.error('Error: MAILINATOR_API_KEY environment variable is not set. Please provide your API key.');
process.exit(1);
}
const mailinatorClient = new MailinatorClient(apiKey);
const inboxName = 'my-test-inbox-123'; // Replace with an inbox you can control or create
try {
console.log(`Attempting to fetch messages for inbox: ${inboxName}@mailinator.com`);
// The exact path to 'getInboxMessages' may vary; check Mailinator's API docs/SDK reference.
const response = await mailinatorClient.inbox.getInboxMessages({ inbox: inboxName });
if (response.result && response.result.msgs && response.result.msgs.length > 0) {
console.log(`Successfully found ${response.result.msgs.length} messages.`);
response.result.msgs.forEach((msg: InboxMessage) => {
console.log(` - ID: ${msg.id}, Subject: ${msg.subject || 'No Subject'}, From: ${msg.fromfull || 'Unknown'}`);
});
} else {
console.log(`No messages found in inbox: ${inboxName}@mailinator.com.`);
}
} catch (error) {
console.error('Failed to retrieve messages:', error instanceof Error ? error.message : String(error));
if (error instanceof Error && error.message.includes('Invalid API Key')) {
console.error('Please verify your MAILINATOR_API_KEY is correct.');
}
}
}
fetchInboxMessages();