Temp Mail API Libraries for Python and JavaScript (pip and npm)

Published

TempMailo now has two official client libraries for its API: tempmailo for Python on PyPI and tempmailo-client for JavaScript on npm. With either of them, a test or a script creates a temporary email address, waits for the letter and pulls the code out of it in a few lines, with no HTTP requests of your own.

The addresses are the same as in our temp mail on the homepage: they only receive mail, never send it, and live 10 or 60 minutes. Both libraries are open source under the MIT license.

Install

pip install tempmailo
npm install tempmailo-client

The Python package needs Python 3.10 or newer and nothing beyond the standard library. The JavaScript package has no dependencies, ships ES modules with TypeScript types and runs on Node.js 22 or newer, Deno, Bun and Cloudflare Workers.

What the libraries do

  • Create an address that lives 10 or 60 minutes: create.
  • Wait for the first new letter that matches your condition: wait_for_message in Python, waitForMessage in JavaScript. They check every 5 seconds by default, well below the limit of 60 requests a minute.
  • Read the plain text, the HTML, the raw source and the attachments of a message.
  • Delete the address and its mail when the test is over: delete.
  • Check the signature of webhook requests with verify_webhook or verifyWebhook (the Standard Webhooks scheme).
  • Raise typed errors (AuthError, ExpiredError, NotFoundError, RateLimitError) and retry on their own when the API asks to wait 60 seconds or less.

A confirmation code in a few lines

Python:

import re
from tempmailo import TempMailo

mail = TempMailo()  # reads TEMPMAILO_API_KEY
inbox = mail.create(lifetime=600)
# ... sign up a user with inbox.email here ...
letter = mail.wait_for_message(inbox.token, timeout=120)
code = re.search(r"\b\d{6}\b", letter.plain or letter.html).group()
mail.delete(inbox.token)

JavaScript:

import { TempMailo } from 'tempmailo-client';

const mail = new TempMailo(); // reads TEMPMAILO_API_KEY
const inbox = await mail.create({ lifetime: 600 });
// ... sign up a user with inbox.email here ...
const letter = await mail.waitForMessage(inbox.token, { timeout: 120_000 });
const code = (letter.plain || letter.html).match(/\b\d{6}\b/)?.[0];
await mail.delete(inbox.token);

The pattern \b\d{6}\b finds a six-digit code. If your codes look different, change the pattern.

What you need

An API key. Create a free account, confirm its email and make a key on the API page of your account. Put the key in the TEMPMAILO_API_KEY environment variable or pass it to the client, and keep it out of public code.

The free limits: 100 new addresses a day, 60 requests a minute, webhooks up to 1000 events a day. When the daily limit is used up, the client reports it at once instead of waiting.

Call the API from a server, a test or a worker, not from a web page: it sends no CORS headers. On npm the package is called tempmailo-client; other packages and services with similar names are not related to tempmailo.co.

Guides and reference

Read next

All articles of the blog

We use cookies to keep your inbox open in this browser, to show ads and to count visits. Cookie policy