Timeouts in API tests: set them on purpose, test what happens past them

Two different questions hide behind "timeouts" in API testing. How long should your test wait for a response before it gives up? And what does the API do when something behind it is slow? Both deserve a decision instead of a default.

Where the timeout comes from

When a request is sent, from the app, the runner, the CLI or an IDE, its timeout is the first one set in this order:

  1. the request's own timeout;
  2. the nearest folder's;
  3. the collection's;
  4. the default, 30 seconds (REQUESTARIUM_TIMEOUT_MS changes it for the CLI).

So set a sensible value once on the collection, and override it only where an endpoint is slow on purpose, for example a Reports folder with 120 seconds.

From the command line, --timeout replaces all of them for every request in the run:

Terminal
requestarium run-collection "Orders API" --env Staging --timeout 10000

That's useful in a smoke job after a deploy, where you'd rather fail fast than wait 30 seconds for each of 40 requests against a service that didn't come up.

Timeout or slow? Use both

A timeout says "stop waiting". It doesn't say "this is too slow". Keep them separate:

  • Timeout: generous, say 3 to 10 times the normal response time. Hitting it means something is stuck.
  • Response-time check: tight, close to what users would notice. Failing it means a regression.

The response-time check is an assertion on the request, folder or collection Tests tab:

SourceOperatorExpected
Response time (ms)less than800

For a whole run, gate on the 95th percentile instead of each request, so one slow outlier doesn't fail a build: --max-p95 800. See setting a performance budget with p95.

Reading a timeout in the results

A timed-out request is reported as an error (it couldn't complete), not as a failed test. In the CLI's JSON output it has "status": "error" and "error": {"code": "timeout", …}:

Terminal
requestarium run-collection "Orders API" --env Staging --json \
  | jq -r '.results[] | select(.error.code == "timeout") | .path'

That gives you the list of stuck endpoints, separate from the ones that answered wrongly.

Retries for flaky networks, not for slow endpoints

--retries N resends a request that couldn't be sent or got 502, 503 or 504, waiting 1 second and then twice as long each time. It's meant for a gateway that's briefly unavailable during a deploy. Don't use it to paper over an endpoint that's slow: if a request takes 25 seconds, three retries make it a 100-second test.

Testing your API's own timeouts

Your API calls databases and other services, and should time out on them rather than hang. Ways to provoke it:

  • A test hook: some teams add a ?delayMs= or X-Test-Delay header that only staging honours, to make a downstream call slow.
  • A mock server with a delay: point the downstream URL of your staging service at a mock server whose delay is longer than your service's timeout. A mock server can delay its answers by up to 10 seconds on paid plans.
  • Toxiproxy or a similar proxy in docker-compose, for local runs.

Then check what your API answers:

JavaScript
pm.test('downstream timeout becomes a 504, not a hang', () => {
  pm.expect(pm.response.code).to.equal(504);
});
pm.test('within our own budget', () => {
  pm.expect(pm.response.responseTime).to.be.below(6000);
});
pm.test('error says which dependency', () => {
  pm.expect(pm.response.json().error).to.match(/timeout/i);
});

The middle check matters most. An API that waits 60 seconds for a downstream service that never answers ties up its own workers, and turns a slow dependency into an outage.