# Add Request Timeouts to an Axios API Client

Yes. An agent sets a timeout on your shared Axios instance, turns the timeout into a readable error, tests it against a stalled mock, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-request-timeouts/axios/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from the one place every request goes through. In a React app laid out like alan2207/bulletproof-react, `apps/react-vite/src/lib/api-client.ts` creates the instance with `Axios.create({ baseURL: env.API_URL })` and nothing else: no `timeout`, so a request to an upstream that stops answering hangs until the browser gives up on the socket. A request interceptor sets `Accept` and `withCredentials`; the response interceptor unwraps `response.data`, pushes `error.response?.data?.message || error.message` into the notifications store as an error toast, and sends a 401 to the login page. `package.json` pins `axios` at `^1.6.8` and `vitest` at `^2.1.4`, and `src/lib/react-query.ts` sets `retry: false` for every query, so a hung request is never retried either: the spinner simply stays.

It adds the limit where the instance is made. `src/config/env.ts` already validates `VITE_APP_*` variables with a zod schema, so the agent adds an `API_TIMEOUT_MS` entry there with a default, reads it in `api-client.ts` as `timeout`, and sets `transitional: { clarifyTimeoutError: true }` so a timeout rejects with `AxiosError.ETIMEDOUT` rather than the older `ECONNABORTED`, which Axios also uses for other aborted requests. A `timeoutErrorMessage` replaces the library's default, which names the limit in milliseconds, with a sentence a person can read.

The response interceptor then gets one branch: when `error.code` is `ETIMEDOUT`, the notification says the server took too long and the request was not completed, and the promise still rejects so the calling `useQuery` or `useMutation` sees a failure. The agent leaves the 401 redirect and the generic message path as they are. A call that genuinely needs longer, such as a report export, can pass its own `timeout` in the per-request config, and the pull request shows that form once so the team does not reach for a global increase.

It proves the change with a test the suite already knows how to run. `src/testing/setup-tests.ts` starts an MSW server with `onUnhandledRequest: 'error'`, so the agent adds `src/lib/__tests__/api-client.test.ts` with a handler that uses MSW's `delay()` for longer than a deliberately short `timeout` passed in the request config, and asserts both the rejection's `code` and the message the notifications store received. It runs `yarn test`, `yarn lint` and `yarn check-types`, the three checks `.github/workflows/react-vite-ci.yml` runs in its `all-cli-checks` job, and the new test passes alongside the twelve files already under `__tests__`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a request timeout to the shared Axios client in apps/react-vite. Read package.json, src/lib/api-client.ts, src/config/env.ts, src/lib/react-query.ts, src/testing/setup-tests.ts and .github/workflows/react-vite-ci.yml first. Add an API_TIMEOUT_MS variable to the env schema with a default, set timeout and transitional.clarifyTimeoutError on the instance with a readable timeoutErrorMessage, and make the response interceptor show a clear notification when error.code is ETIMEDOUT while still rejecting. Keep the 401 redirect as it is. Add a Vitest test that uses an MSW handler with delay() longer than a short per-request timeout and asserts the code and the notification. Run yarn test, yarn lint and yarn check-types, and open a pull request that explains the default and how one call can override it.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that touches `src/config/env.ts`, `src/lib/api-client.ts`, `.env.example` and one new test file. The description names the default limit, quotes the interceptor branch, and pastes the test run with the new file listed among the suite's results, plus the lint and type-check output.

The run's record keeps every command: the install, the three checks, and the test that waited on the delayed mock and saw the timeout fire. A reviewer reads the assertion rather than trusting a sentence.

Before merging, a reviewer decides the default: long enough for the slowest real endpoint, short enough that a stalled upstream shows a message instead of a spinner. The pull request also lists any call site that should carry its own longer limit, so that decision is made per call and not by raising the global value.

## Run it on every new API call in a pull request

A team that keeps adding features through `src/features/*/api` can start a run whenever a pull request adds a call that should not share the default, naming the endpoint in `input` and the repository in `resources` with `POST /o/{org}/sessions`. Each run reads the same client and the same env schema, so an app that already has the limit gets only the per-call override and its test.

Send a `request_id` that is a UUID v5 of the pull request's URL so a retried call returns the run already started. A `webhook_url` on the API key delivers `run.completed` with the `session_url` to read the pull request from.

## Why run it on Rush?

A missing timeout is the kind of gap nobody notices until the API stalls in production and every screen shows a spinner. The fix is small but spread over three files, and the only honest proof is a request that actually waits on a slow server. The agent writes that test against the app's own MSW setup in its sandbox, runs the whole suite, and keeps the output.

The pull request is a few lines of config, one interceptor branch and a test, so review takes minutes. A team with several Vite apps on the same client pattern runs them all at once, each in its own sandbox with its own pull request.

## Frequently asked questions

### Why ETIMEDOUT instead of ECONNABORTED?

Axios historically used `ECONNABORTED` for both a timeout and other aborted requests. Setting `transitional.clarifyTimeoutError` makes a timeout reject with `ETIMEDOUT`, so the interceptor can tell the two apart without parsing the message. The agent sets it on the instance, so every call gets the clearer code.

### Will React Query retry a timed-out request?

Not in this app: `src/lib/react-query.ts` sets `retry: false` for every query, and the agent leaves that alone. A timeout surfaces once as a notification and a failed query, and the screen's existing error state handles it. Turning retries on for specific queries is a separate decision the pull request mentions.

### What about a call that legitimately takes longer?

Pass a `timeout` in that call's request config; Axios merges it over the instance default. The pull request shows the form once. Keeping the global limit tight and overriding per call is better than a long global limit that hides a stalled upstream everywhere.

### Does the test slow the suite down?

Only by the delay it waits on, which is short because the test passes its own small timeout in the request config rather than waiting for the real default. It runs under the same MSW server the other tests use, and `onUnhandledRequest: 'error'` still fails any request the handler does not cover.

## More Axios jobs

- [Add Retry Logic to a Shared Axios Client](https://getrush.ai/run/add-retry-logic/axios/)
- [Fix "Package subpath './lib/adapters/http' is not defined by \"exports\"" after upgrading axios](https://getrush.ai/run/fix-package-subpath-is-not-defined-by-exports/axios/)
- [Replace Axios With Native fetch in a Node.js Library](https://getrush.ai/run/migrate-axios-to-fetch/axios/)

## The same job on other stacks

- [Add Request Timeouts to an Axum API](https://getrush.ai/run/add-request-timeouts/axum/)
- [Add Request Timeouts to an Echo App](https://getrush.ai/run/add-request-timeouts/echo/)
- [Add Request Timeouts to a Fiber App](https://getrush.ai/run/add-request-timeouts/fiber/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
