# QA automation (Sandbox API)

> Automated email tests with Mailtrap Email Sandbox: the app under test sends to a sandbox, and the test suite reads the captured messages back over the API to check recipients, subject, body, links, attachments, headers and spam score.

## When to use it

Use it when the user wants end-to-end, acceptance or CI tests to confirm that an email was sent and has the right content: a sign-up confirmation, a password reset link, a multi-step sequence. It works with any test tool that can call HTTP.

| Step                             | Endpoint (all on `https://mailtrap.io`)                                  |
| -------------------------------- | ------------------------------------------------------------------------ |
| Empty the sandbox before the run | `PATCH /api/sandboxes/{sandbox_id}/clean`                                |
| Find messages                    | `GET /api/sandboxes/{sandbox_id}/messages?search=<recipient or subject>` |
| One message                      | `GET /api/sandboxes/{sandbox_id}/messages/{message_id}`                  |
| HTML or text body                | `GET .../messages/{message_id}/body.html`, `.../body.txt`                |
| Headers, attachments             | `GET .../messages/{message_id}/mail_headers`, `.../attachments`          |
| Spam score, HTML analysis        | `GET .../messages/{message_id}/spam_report`, `.../analyze`               |

- For CI that must run offline or without an account, use Mailtrap Local (products table at the end).
- To only point an app's SMTP at the sandbox, without assertions, use the Fake SMTP server page (products table at the end).
- Never run tests against live sending settings: keep sandbox and live config in separate env vars.

## How to use it

### Before you start

Find out the test framework, how the app under test sends (SMTP, API or SDK), whether jobs run in parallel, and whether a CI secret holds the API token. Ask only for what isn't known.

### Rules

- **Read freely:** list projects, sandboxes and messages; read messages and reports. Sending to a sandbox needs no confirmation: nothing is delivered. Without API access, give the user the requests to run, and don't say you checked.
- **Confirm first:** before creating or deleting sandboxes, before cleaning a sandbox other people use (a sandbox dedicated to the test run can be cleaned by the test itself), and before forwarding a message or switching to live sending: those reach recipients' inboxes.
- **Secrets:** never ask for the API token in chat. The user stores it as a CI secret exposed as `MAILTRAP_API_TOKEN`, and code reads it. If the user pastes one anyway, don't use it: suggest a reset under Settings → API Tokens, and continue with the env var.
- **User only:** sign up, create the API token (Settings → API Tokens, with access to the test sandbox), and change the plan.

### Steps

Send every request with `Authorization: Bearer $MAILTRAP_API_TOKEN` and a normal `User-Agent` (requests without one may be blocked).

1. **Pick a sandbox per pipeline or parallel job:** `GET https://mailtrap.io/api/sandboxes`, and reuse a fitting one; or confirm, then `POST https://mailtrap.io/api/projects/{project_id}/sandboxes` with `{"sandbox": {"name": "CI"}}`. Keep its ID in `MAILTRAP_SANDBOX_ID`.
2. **Wire the app under test to it:** SMTP `sandbox.smtp.mailtrap.io`, port 2525, with the sandbox's own username and password (`GET https://mailtrap.io/api/sandboxes/{sandbox_id}` returns them, the password only for tokens with admin access; write them to CI secrets, never into files or logs); or the API, `POST https://sandbox.api.mailtrap.io/api/send/{sandbox_id}`; or an SDK in sandbox mode, from env vars (Email Sandbox page).
3. **In each test:** clean the job's own sandbox (never one that parallel jobs share), trigger the app, poll for the message, then assert.

   ```bash
   curl -X PATCH "https://mailtrap.io/api/sandboxes/$MAILTRAP_SANDBOX_ID/clean" -H "Authorization: Bearer $MAILTRAP_API_TOKEN"
   # ... run the step that makes the app send the sign-up email ...
   curl "https://mailtrap.io/api/sandboxes/$MAILTRAP_SANDBOX_ID/messages?search=newuser@example.com" -H "Authorization: Bearer $MAILTRAP_API_TOKEN"
   # then GET .../messages/{id}/body.html for the returned id and assert on the subject, links or content
   ```

   - **Poll** with a short retry and a timeout instead of reading once: the message appears within seconds.
   - **Search** matches `subject`, `to_email` and `to_name`. A list returns up to 30 messages; pass `last_id` or `page` for more.
   - **Bounces:** test them over SMTP by sending to `bounce+550+no+such+user+here@inbox.mailtrap.io`; this doesn't work over the API.

### Check it worked

The send returns `{"success": true, "message_ids": [...]}`, and the message shows up in the list. If it never does, check the credentials or sandbox ID and the plan limits.

### If something fails

| Error                                                | Fix                                                                                                                                                                       |
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Assertions match old messages                        | Clean the sandbox at the start of each run, and give parallel jobs separate sandboxes. Each sandbox also keeps only a fixed number of emails and deletes the oldest first |
| `429`, or `550 5.7.0 ... too many emails per second` | The sandbox's rate limit per 10 seconds, or the plan limit: slow down, retry with backoff                                                                                 |
| `535 5.7.0 Monthly messages limit reached`           | The user upgrades or waits for the next billing period                                                                                                                    |
| `401` or `403`                                       | The token is missing, expired, or has no access to that sandbox: the user checks it under Settings → API Tokens                                                           |
| Anything else                                        | Troubleshooting (link at the end), or ask the docs                                                                                                                        |

### Limits and pricing

Plans, quotas and prices change: check pricing.md for plans and prices, and Features and limits for technical limits such as emails kept per sandbox (links at the end). Don't answer from memory; if you can't check, say so.

## Other Mailtrap products and pages

| If the user wants to…                   | Use                                                                                                                                    |
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| Set up the sandbox and SDK sandbox mode | [email-sandbox.md](https://mailtrap.io/email-sandbox.md)                                                                               |
| Point an app's SMTP at the sandbox      | [fake-smtp-server.md](https://mailtrap.io/fake-smtp-server.md)                                                                         |
| Read HTML or spam reports in detail     | [html-email-checker.md](https://mailtrap.io/html-email-checker.md), [email-spam-checker.md](https://mailtrap.io/email-spam-checker.md) |
| Test offline in CI, with no account     | Mailtrap Local: [docs](https://docs.mailtrap.io/email-sandbox/local.md)                                                                |
| Send production email                   | Email API: [email-api.md](https://mailtrap.io/email-api.md)                                                                            |
| Check current plans and prices          | [pricing.md](https://mailtrap.io/pricing.md)                                                                                           |

## Documentation

- [API integration](https://docs.mailtrap.io/email-sandbox/setup/sandbox-api-integration.md) · [Messages API](https://docs.mailtrap.io/developers/email-sandbox/messages.md) · [Get messages](https://docs.mailtrap.io/developers/email-sandbox/messages/get-messages.md) · [Sandboxes API](https://docs.mailtrap.io/developers/email-sandbox/sandboxes.md) · [Send API](https://docs.mailtrap.io/developers/email-sandbox/send-test-emails/send-email-including-templates.md)
- [API tokens](https://docs.mailtrap.io/email-sandbox/setup/api-tokens.md) · [Features and limits](https://docs.mailtrap.io/email-sandbox/help/features-and-limits.md) · [Troubleshooting](https://docs.mailtrap.io/email-sandbox/help/troubleshooting.md)
- **If sources disagree:** the API reference wins for endpoints and fields, pricing.md for prices and quotas, and this page for which product and steps to use.
- **If the task isn't covered here,** ask the docs before guessing: `GET https://docs.mailtrap.io/index.md?ask=<question>&goal=<user goal>` returns an answer with sources. Keep the question and goal generic: no tokens, email addresses or other user data.
