Upyo
Learn how to integrate Mailtrap with Upyo, a cross-runtime TypeScript email library, to send transactional or sandbox emails using the Mailtrap transport.
Upyo is a TypeScript library for composing and sending email. You build a message once with createMessage() and hand it to a transport, which handles delivery.
This guide covers @upyo/mailtrap, the Upyo transport for Mailtrap. It wraps Mailtrap's Email API and Email Sandbox, so you can send production or test emails through Mailtrap using Upyo's message format.
Prerequisites
Before you start, make sure you have:
A verified sending domain (required for production sending; not needed if you're only sending to the Email Sandbox)
Node.js, Deno, or Bun installed, since Upyo runs across all three
Installation
Install the @upyo/mailtrap package with your package manager of choice:
npm add @upyo/mailtrappnpm add @upyo/mailtrapyarn add @upyo/mailtrapdeno add jsr:@upyo/mailtrapbun add @upyo/mailtrapSend emails using Upyo and Mailtrap
Create a MailtrapTransport with your API token, then build a message with createMessage() and send it:
import { createMessage } from "@upyo/core";
import { MailtrapTransport } from "@upyo/mailtrap";
const transport = new MailtrapTransport({
apiToken: "your-mailtrap-api-token",
sandbox: true,
inboxId: 12345,
});
const message = createMessage({
from: "support@example.com",
to: "customer@example.com",
subject: "Welcome to our service",
content: { text: "Thank you for signing up!" },
});
const receipt = await transport.send(message);
if (receipt.successful) {
console.log("Message sent with ID:", receipt.messageId);
} else {
console.error("Send failed:", receipt.errorMessages.join(", "));
}Once you copy the script, make sure to:
Replace
YOUR-MAILTRAP-API-KEY-HEREwith your actual Mailtrap API tokenReplace
YOUR-EMAIL-HEREwith your verified sender addressReplace
RECIPIENT-EMAIL-HEREwith the recipient's email address
The transport handles the conversion to Mailtrap's JSON format automatically, including HTML/text alternatives, CC, BCC, reply-to, custom headers, priority, attachments, and inline Content-ID attachments so you don't need to build the request payload by hand.
Email API and Email Sandbox
A single Mailtrap API token works for both Email API and Email Sandbox environments. To choose where your messages go, toggle the sandbox option:
sandbox: false(default) – Sends through the production Email APIsandbox: truewith aninboxId– Captures messages in an Email Sandbox inbox instead of delivering them, so you can test without reaching real recipients
For example:
Note: You can find your sandbox inbox ID in the URL of the sandbox in your Mailtrap account, e.g. mailtrap.io/sandboxes/{id}.
Categories, tags, and metadata
Mailtrap classifies messages with a category field, which is what powers filtering in Email Categories. Upyo bridges its own generic tagging system to this:
The first entry in a message's
tagsarray becomes the MailtrapcategoryIf no tags are set, the transport falls back to
defaultCategory(default:transactional)Any additional tags are sent as custom variables, prefixed with
tag_
Set transport-level metadata when every message sent through that transport should carry the same tracking fields:
In this example, the message is categorized as billing in Mailtrap, and invoice is attached as a tag_invoice custom variable.
Batch sending
To send multiple messages in one pass, use sendMany(). The transport automatically batches up to 500 messages per Mailtrap API call:
Configuration options
apiToken
Your Mailtrap API token. Required.
sandbox
Use the Email Sandbox instead of the Email API. Default: false.
inboxId
Sandbox inbox ID. Required when sandbox is true.
sendBaseUrl
Email API base URL. Default: https://send.api.mailtrap.io
sandboxBaseUrl
Sandbox API base URL. Default: https://sandbox.api.mailtrap.io
defaultCategory
Category applied when a message has no tags. Default: transactional
metadata
Key-value pairs merged into Mailtrap's custom_variables for every message sent through the transport.
userAgent
User-Agent header sent with requests. Default: @upyo/mailtrap
timeout
Request timeout, in milliseconds. Default: 30000
retries
Number of retry attempts for transient failures. Default: 3
headers
Additional HTTP headers included on every request.
Last updated
Was this helpful?

