Transport and retries
Timeouts, exponential backoff, jitter, and Retry-After handling in the Kelp SDK.
Timeouts
Every request has an independent per-request timeout controlled by timeoutMs (default 10000 ms). When the timeout fires the request is aborted and a KelpApiError with code: 'timeout' is thrown.
const kelp = createKelpClient({
searchUrl: 'https://{TENANT_ID}-search.kelp.click',
analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
apiKey: 'kelp_...',
timeoutMs: 5000, // 5 s
});Timeouts are independent of AbortSignal. If you pass a signal on the request and abort it first, a DOMException named AbortError is thrown instead - see Error handling.
Retries
By default the SDK makes one attempt and throws on failure (maxRetries: 0). Enable retries by setting maxRetries to the number of additional attempts after the initial try:
const kelp = createKelpClient({
searchUrl: 'https://{TENANT_ID}-search.kelp.click',
analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
apiKey: 'kelp_...',
maxRetries: 2, // 1 initial + 2 retries = 3 total attempts
retryBaseMs: 250, // base delay for backoff
retryMaxMs: 5000, // cap per-attempt delay
});Which statuses are retried
The SDK retries on the following conditions:
| Condition | Notes |
|---|---|
HTTP 429 (Too Many Requests) | Retry-After header is respected and clamped to retryMaxMs. |
HTTP 502 (Bad Gateway) | Transient upstream error. |
HTTP 503 (Service Unavailable) | Retry-After header is respected and clamped to retryMaxMs. |
HTTP 504 (Gateway Timeout) | Transient upstream error. |
| Network error (fetch rejected) | Surfaces as KelpApiError with code: 'network'. |
| Request timeout | Surfaces as KelpApiError with code: 'timeout'. |
4xx errors other than 429 (e.g. 400, 401, 403) are not retried - they indicate a problem with the request itself.
Exponential backoff with full jitter
The delay before each retry is computed as:
delay = random(0, min(retryBaseMs * 2^attempt, retryMaxMs))Full jitter distributes retry storms across the interval [0, cap] rather than clustering them at a fixed point, which reduces pressure on the server when many clients fail at the same time.
With the defaults (retryBaseMs: 250, retryMaxMs: 5000):
| Attempt | Max delay before jitter |
|---|---|
| 1st retry | 500 ms |
| 2nd retry | 1000 ms |
| 3rd retry | 2000 ms |
| 4th retry | 4000 ms |
| 5th retry+ | 5000 ms (capped) |
Actual delay is a random value between 0 and the values above.
Retry-After handling
When the server responds with 429 or 503 and includes a Retry-After header, the SDK parses it (supports both the delay-seconds and HTTP-date forms) and waits at least that long before retrying. The wait is still clamped to retryMaxMs.
After all retries are exhausted the error is thrown and onError is called (if configured). See Error handling for how to catch and inspect the error.
Custom fetch
Inject your own fetch implementation via the fetch option - useful for SSR environments that do not expose a global fetch, or for test mocking:
import { createKelpClient } from '@kelpclick/client';
import nodeFetch from 'node-fetch';
const kelp = createKelpClient({
searchUrl: process.env.KELP_SEARCH_URL!,
analyticsUrl: process.env.KELP_ANALYTICS_URL!,
apiKey: process.env.KELP_API_KEY!,
fetch: nodeFetch as typeof fetch,
});Related
- Client configuration - full options table
- Observability - log and hook into request lifecycle
- Error handling - inspect retry exhaustion errors