How to test API search, filtering and sorting

List endpoints with ?status=, ?q= and ?sort= look simple and hide a surprising number of bugs. A misspelled filter is ignored and returns everything. Sorting by a field with duplicates gives a different order on each call, so pagination skips items. A search for O'Brien returns 500. The good news: these tests are generic, so one set of patterns covers every list endpoint you have.

Filters: check every item

HTTP
GET {{baseUrl}}/orders?status=shipped&createdAfter=2026-11-01
JavaScript
const items = pm.response.json().items;

pm.test('found something', () => pm.expect(items).to.not.be.empty);
pm.test('every order is shipped', () => {
  const wrong = items.filter((order) => order.status !== 'shipped').map((order) => order.id);
  pm.expect(wrong).to.be.empty; // a failure lists the ids that aren't shipped
});
pm.test('every order is recent enough', () => {
  const after = new Date('2026-11-01T00:00:00Z');
  pm.expect(items.every((order) => new Date(order.createdAt) >= after)).to.be.true;
});

The "found something" test matters: a filter on an empty result passes every other check.

Bad filters should be loud

RequestGood answerBad answer
?stauts=shipped (typo)400 naming the unknown parameter200 with every order
?status=shiped400, or 200 with an empty list200 with every order
?createdAfter=yesterday400 with the expected format500
?limit=-1, ?limit=1000000400, or clamped to a maximum500, or a million rows

An ignored filter is the dangerous one. A script that deletes "all shipped orders" after filtering deletes all orders.

Sorting: test ties

HTTP
GET {{baseUrl}}/orders?sort=-total
JavaScript
const totals = pm.response.json().items.map((order) => order.total);
pm.test('sorted by total, descending', () => {
  const sorted = [...totals].sort((a, b) => b - a);
  pm.expect(totals).to.eql(sorted);
});

Then sort by a field with many equal values (status, country) and call it twice. If the order of items with the same value changes between calls, the API needs a tiebreaker such as id. Without one, page 2 can repeat items from page 1 and skip others.

Also check: an unknown sort field returns 400, and sorting is the same for ?sort=name with mixed case and accents as your users expect (Émile near Emma, not after Zoe).

Search: the inputs that break things

q=Why
(empty)everything, nothing, or 400? Decide and test
O'Brienquote handling, SQL built by string concatenation
50% and a_b% and _ are wildcards in SQL LIKE
<script>echoed back in a "no results for…" message?
Łódź, 日本, caféUnicode and accent folding
2,000 charactersa limit with 400, not a timeout
spaces trimmed or not, consistently

Every one of these must return 200 or 400, never 500:

JavaScript
pm.test('no server error', () => pm.expect(pm.response.code).to.be.below(500));

Filters plus pagination

Fetch every page with a filter applied and check the totals add up:

JavaScript
const ids = JSON.parse(pm.environment.get('seenIds') || '[]');
const page = pm.response.json();
const duplicates = page.items.filter((order) => ids.includes(order.id));
pm.test('no order on two pages', () => pm.expect(duplicates).to.be.empty);
pm.environment.set('seenIds', JSON.stringify(ids.concat(page.items.map((order) => order.id))));

At the end, the number of collected IDs should equal the total the API reported. See How to test API pagination for walking the pages.