Test batch API endpoints when some items fail
Batch endpoints (POST /contacts/batch, POST /events, bulk imports) save round trips. They also add a question single-item endpoints don't have: what happens when some items are valid and some aren't?
There are two honest answers and one dangerous one.
Build a batch with known failures
Generate the batch in a pre-request script, so its size and the position of bad items are easy to change:
const size = 50;
const bad = new Set([0, 24, 49]); // first, middle, last
const runId = pm.variables.replaceIn('{{$randomUUID}}').slice(0, 8);
const items = Array.from({ length: size }, (_, i) => ({
externalId: `batch-${runId}-${i}`,
email: bad.has(i) ? 'not-an-email' : `user${i}.${runId}@example.test`,
}));
pm.environment.set('batchRunId', runId);
pm.environment.set('badIndexes', JSON.stringify([...bad]));
pm.request.body.raw = JSON.stringify({ items });POST {{baseUrl}}/contacts/batch
Content-Type: application/jsonIf the API is all or nothing
pm.test('a batch with invalid items is rejected', () => {
pm.expect(pm.response.code).to.be.oneOf([400, 422]);
});
pm.test('the error points at the bad items', () => {
const body = pm.response.text();
for (const i of JSON.parse(pm.environment.get('badIndexes'))) {
pm.expect(body).to.include(`items[${i}]`);
}
});Then a second request checks that nothing was stored:
GET {{baseUrl}}/contacts?externalIdPrefix=batch-{{batchRunId}}pm.test('nothing from the rejected batch was stored', () => {
pm.expect(pm.response.json().data).to.have.lengthOf(0);
});This catches a handler that validates item by item while inserting, and stops at the first bad one after storing the ones before it.
If the API allows partial success
The response should say what happened to each item, in the same order:
{
"results": [
{ "index": 0, "status": "error", "error": { "field": "email", "message": "invalid" } },
{ "index": 1, "status": "created", "id": "con_…" }
]
}const results = pm.response.json().results;
const bad = new Set(JSON.parse(pm.environment.get('badIndexes')));
pm.test('one result per item', () => pm.expect(results).to.have.lengthOf(50));
pm.test('bad items failed, good items were created', () => {
results.forEach((r, i) => {
pm.expect(r.index).to.equal(i);
pm.expect(r.status).to.equal(bad.has(i) ? 'error' : 'created');
});
});And the stored count is exactly the good ones:
pm.test('47 contacts stored', () => pm.expect(pm.response.json().data).to.have.lengthOf(47));The status code
For partial success, APIs use 200 with per-item results, or 207 Multi-Status. Either works if clients are told. What doesn't work is 201 when some items failed, because clients that only read the status will assume everything was created.
Limits
- Empty batch:
{"items": []}should be a400or a no-op200, not a500. - Too large: one item over the documented maximum should be rejected with the limit in the message. See request size limits.
- Duplicates inside one batch: two items with the same
externalId. One created and one rejected, or both rejected? Decide.
Clean up
Batch tests create a lot of data. Put a clean-up request after them that deletes everything with the run's prefix, and in a test suite give it the Clean up role, so it runs even when a check failed.