Axios + axios-retry with exponential backoff, tested with nock: working versions (tested 2026-10-15)
Configure axios-retry for idempotent requests and 429/5xx responses, tested offline with nock intercepts.
passed4 passed · 0 failed2026-10-15 13:20 UTC- Status
- passed
- Last passed
- 2026-10-15 13:20 UTC
- Tests
- 4 / 4
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions5
axios@1.20.0axios-retry@4.5.0tsx@4.23.15@types/node@24.19.2nock@15.0.1
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38741369695
The code
Exactly the files that passed. Your agent can pull them with get_recipe("axios-retry-nock") (install), or see them on GitHub.
package.json
{
"name": "axios-retry-nock",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"axios": "1.20.0",
"axios-retry": "4.5.0"
},
"devDependencies": {
"tsx": "4.23.15",
"@types/node": "24.19.2",
"nock": "15.0.1"
}
}
src/client.ts
import axios, { type AxiosError } from "axios";
import axiosRetry, { isNetworkOrIdempotentRequestError } from "axios-retry";
export function createClient(baseURL: string, retries = 3) {
const client = axios.create({ baseURL, timeout: 5000 });
axiosRetry(client, {
retries,
retryDelay: (n) => (process.env.NODE_ENV === "test" ? 0 : axiosRetry.exponentialDelay(n)),
retryCondition: (err: AxiosError) => isNetworkOrIdempotentRequestError(err) || err.response?.status === 429,
});
return client;
}
test/client.test.ts
import { test, afterEach } from "node:test";
import assert from "node:assert/strict";
import nock from "nock";
import { createClient } from "../src/client.ts";
process.env.NODE_ENV = "test";
const BASE = "https://api.example.com";
afterEach(() => nock.cleanAll());
test("retries 503 then succeeds", async () => {
nock(BASE).get("/items").reply(503).get("/items").reply(503).get("/items").reply(200, [{ id: 1 }]);
const res = await createClient(BASE).get("/items");
assert.deepEqual(res.data, [{ id: 1 }]);
assert.equal(res.config["axios-retry"]?.retryCount, 2);
});
test("retries 429", async () => {
nock(BASE).get("/items").reply(429).get("/items").reply(200, []);
assert.equal((await createClient(BASE).get("/items")).status, 200);
});
test("does not retry non-idempotent POST on 500", async () => {
const scope = nock(BASE).post("/items").reply(500);
await assert.rejects(createClient(BASE).post("/items", {}));
assert.ok(scope.isDone());
});
test("gives up after the retry limit", async () => {
nock(BASE).get("/items").times(3).reply(500);
await assert.rejects(createClient(BASE, 2).get("/items"), (e: any) => e.response.status === 500);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.