MoeMail
Back to Blog

Receive Email Programmatically in Node.js

Receiving email in Node.js usually means running an SMTP/IMAP server or wiring up Gmail's API. For tests, automation, and most app flows there's a simpler path: a disposable inbox API you talk to over HTTP. This is part of email testing for developers.

Two ways to receive

Either poll the inbox for new messages, or register a webhook and let the service push each message to you. Polling is simplest to start; webhooks are better for latency and scale (see inbound email webhooks).

Create & poll (fetch)

const API = 'https://moemail.app/api'
const H = { 'X-API-Key': process.env.MAIL_KEY }

async function createInbox() {
  const res = await fetch(API + '/emails/generate', { method: 'POST', headers: H })
  return res.json() // { id, address }
}

async function readLatest(id) {
  const res = await fetch(API + '/emails/' + id + '/messages', { headers: H })
  const data = await res.json()
  return data.messages[0] // newest first
}

Node 18+ has fetch built in. Loop readLatest with a short delay until a message appears, then read .text / .html.

Or take a webhook (Express)

import express from 'express'
const app = express()
app.use(express.json())

app.post('/inbound', (req, res) => {
  const mail = req.body // verify the signature first in production
  console.log('got mail for', mail.to, '-', mail.subject)
  res.sendStatus(200) // respond fast; non-2xx triggers retries
})

app.listen(3000)

Keep the handler fast and idempotent. The same primitives power Playwright tests and the Python equivalent, and run unchanged in CI.

Get started

Grab a key from your profile and read the OpenAPI docs, then create a mailbox to try it.