Mailinator API Client

1.1.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Initializes the Mailinator client with an API key from environment variables and fetches messages from a specified inbox, logging basic details.

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();

view raw JSON →