Testing the Postmark API with the test server token
Postmark is popular for transactional email because it's fast and strict. Strictness is good for tests: a bad request fails clearly, with a numeric ErrorCode you can assert on. And there's a built-in test token that validates requests without sending anything.
Step 1: Environment
| Variable | Value |
|---|---|
baseUrl | https://api.postmarkapp.com |
postmarkToken | POSTMARK_API_TEST in the test environment, the real server token elsewhere (secret) |
fromEmail | a confirmed sender signature |
toEmail | a test inbox |
Use API key auth: header name X-Postmark-Server-Token, value {{postmarkToken}}. Postmark also wants Accept: application/json.
Step 2: Send
POST {{baseUrl}}/email
Accept: application/json
Content-Type: application/json
{
"From": "{{fromEmail}}",
"To": "{{toEmail}}",
"Subject": "Postmark API test",
"TextBody": "Hello from the API tests",
"MessageStream": "outbound",
"Tag": "api-test"
}pm.test('sent', () => {
pm.expect(pm.response.code).to.equal(200);
const body = pm.response.json();
pm.expect(body.ErrorCode).to.equal(0);
pm.expect(body.MessageID).to.be.ok;
});With POSTMARK_API_TEST, Postmark validates the request and answers like a real send, without sending. Note the capitalised field names: Postmark uses PascalCase throughout.
Step 3: Templates
POST {{baseUrl}}/email/withTemplate
Accept: application/json
Content-Type: application/json
{
"From": "{{fromEmail}}",
"To": "{{toEmail}}",
"TemplateAlias": "welcome",
"TemplateModel": {"name": "Ann", "product_name": "Requestarium"}
}To test a template's rendering without sending, POST /templates/validate takes the template content and a test model and returns what it renders, with any errors.
Step 4: ErrorCode, not just the status
Most Postmark errors are 422 Unprocessable Entity, and the reason is in ErrorCode:
| ErrorCode | Meaning |
|---|---|
0 | success |
10 | bad or missing server token (with HTTP 401) |
300 | invalid email request: a missing or malformed field |
400 | the sender signature isn't confirmed |
406 | the recipient is inactive: it bounced or complained earlier |
// "Send to an invalid address": expected failure
pm.test('rejected with a reason', () => {
pm.expect(pm.response.code).to.equal(422);
const body = pm.response.json();
pm.expect(body.ErrorCode).to.equal(300);
pm.expect(body.Message).to.be.a('string');
});406 is worth a test of its own in your application: code that treats every 422 as "bad request, fix the code" will mishandle a recipient who simply bounced last week.
Step 5: Batches
POST /email/batch takes an array of up to 500 messages and returns an array of results, one per message. A batch can partly fail: each item has its own ErrorCode. Check every item, not the HTTP status. See testing batch endpoints.