> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/unjs/ofetch/llms.txt
> Use this file to discover all available pages before exploring further.

# createFetch

> Create custom ofetch instances with default configuration

## Overview

The `createFetch` function allows you to create customized instances of ofetch with predefined defaults and global options.

## Type Signature

```typescript theme={null}
export function createFetch(globalOptions?: CreateFetchOptions): $Fetch
```

## Usage

```typescript theme={null}
import { createFetch } from 'ofetch'

const apiFetch = createFetch({
  defaults: {
    baseURL: 'https://api.example.com',
    headers: {
      Authorization: 'Bearer token'
    }
  },
  fetch: customFetch
})

const users = await apiFetch('/users')
```

## Parameters

<ParamField path="globalOptions" type="CreateFetchOptions">
  Global configuration for the fetch instance
</ParamField>

<ResponseField name="return" type="$Fetch">
  A new ofetch instance. See [ofetch](/api/ofetch).
</ResponseField>

## CreateFetchOptions

```typescript theme={null}
export interface CreateFetchOptions {
  defaults?: FetchOptions;
  fetch?: Fetch;
}
```

### defaults

<ParamField path="defaults" type="FetchOptions">
  Default [FetchOptions](/api/options/fetch-options) to apply to all requests made with this instance. These can be overridden on a per-request basis.
</ParamField>

```typescript theme={null}
const apiFetch = createFetch({
  defaults: {
    baseURL: 'https://api.example.com',
    headers: {
      'Content-Type': 'application/json'
    },
    retry: 3,
    timeout: 10000
  }
})
```

### fetch

<ParamField path="fetch" type="Fetch">
  Custom fetch implementation to use instead of `globalThis.fetch`. Useful for providing polyfills or custom fetch behavior.
</ParamField>

```typescript theme={null}
import { fetch } from 'undici'

const customFetch = createFetch({
  fetch: fetch as typeof globalThis.fetch
})
```

## Examples

### API Client with Authentication

```typescript theme={null}
const authenticatedFetch = createFetch({
  defaults: {
    baseURL: 'https://api.example.com',
    headers: {
      Authorization: `Bearer ${token}`
    },
    onRequest({ options }) {
      console.log('Making request:', options)
    },
    onResponseError({ response }) {
      console.error('Request failed:', response.status)
    }
  }
})
```

### Custom Retry Configuration

```typescript theme={null}
const resilientFetch = createFetch({
  defaults: {
    retry: 5,
    retryDelay: 500,
    retryStatusCodes: [408, 409, 425, 429, 500, 502, 503, 504],
    timeout: 30000
  }
})
```

### Node.js with Custom Fetch

```typescript theme={null}
import { fetch } from 'node-fetch-native'

const nodeFetch = createFetch({
  fetch: fetch as typeof globalThis.fetch,
  defaults: {
    baseURL: 'http://localhost:3000'
  }
})
```

## Related

* [ofetch.create()](/api/ofetch#ofetchcreate) - Create instances from existing ofetch
* [FetchOptions](/api/options/fetch-options) - Available default options
* [\$Fetch](/api/ofetch) - Return type interface
