Skip to main content

Configuration

Nitro Fetch provides flexible configuration options that can be set globally (when creating an instance) or per request. This page details all available options in the RequestConfig interface.

Global vs. Per-Request Config​

  • Global config: Passed to create() and applies to all requests made by the instance.
  • Per-request config: Passed as the last argument to any request method (get, post, etc.) and overrides global config for that request.

RequestConfig Options​

baseURL​

The base URL for all requests.

const api = create({ baseURL: 'https://api.example.com' });
api.get('/users'); // Requests https://api.example.com/users

headers​

Custom headers for all requests.

const api = create({
headers: { 'Authorization': 'Bearer <token>' }
});

params​

Query parameters to append to the URL.

api.get('/search', {
params: { q: 'nitro', page: 2 }
});
// => /search?q=nitro&page=2

timeout​

Abort the request if it takes longer than the specified milliseconds.

api.get('/slow-endpoint', { timeout: 2000 }); // 2 seconds

retry​

Automatic retry logic for failed requests.

  • count: Number of retry attempts (default: 0)
  • delay: Delay between retries in ms (default: 1000)
  • onRetry: Callback on each retry (error, attempt) => void
  • shouldRetry: Function to determine if a retry should occur (error) => boolean
  • retryOnSuccess: Retry even on successful responses (default: false)
const api = create({
retry: {
count: 3,
delay: 500,
onRetry: (error, attempt) => {
console.log(`Retry #${attempt} after error:`, error);
},
shouldRetry: (error) => {
// Retry only on network errors
return error instanceof Error;
},
retryOnSuccess: false
}
});

Example: Overriding Per Request​

api.get('/users', { timeout: 1000, params: { active: true } });