> ## 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.

# ofetch

> The main ofetch function for making HTTP requests

## Overview

`ofetch` is the main function exported by the library. It provides a modern, type-safe interface for making HTTP requests with automatic response parsing, error handling, and retry logic.

The `$fetch` alias is also available and functionally identical to `ofetch`.

## Type Signature

```typescript theme={null}
export interface $Fetch {
  <T = any, R extends ResponseType = "json">(
    request: FetchRequest,
    options?: FetchOptions<R>
  ): Promise<MappedResponseType<R, T>>;
  raw<T = any, R extends ResponseType = "json">(
    request: FetchRequest,
    options?: FetchOptions<R>
  ): Promise<FetchResponse<MappedResponseType<R, T>>>;
  native: Fetch;
  create(defaults: FetchOptions, globalOptions?: CreateFetchOptions): $Fetch;
}
```

## Usage

### Basic Request

<ParamField path="request" type="FetchRequest" required>
  The URL or Request object to fetch
</ParamField>

<ParamField path="options" type="FetchOptions<R>">
  Request options and configuration. See [FetchOptions](/api/options/fetch-options) for details.
</ParamField>

<ResponseField name="return" type="Promise<MappedResponseType<R, T>>">
  Returns a promise that resolves to the parsed response data based on the `responseType`
</ResponseField>

```typescript theme={null}
const data = await ofetch('/api/users')
```

## Methods

### ofetch()

The main function that returns parsed response data.

```typescript theme={null}
const users = await ofetch<User[]>('/api/users')
```

<ParamField path="request" type="FetchRequest" required>
  URL string or Request object
</ParamField>

<ParamField path="options" type="FetchOptions<R>">
  Request configuration options
</ParamField>

<ResponseField name="return" type="Promise<MappedResponseType<R, T>>">
  Parsed response data (JSON by default)
</ResponseField>

### ofetch.raw()

Returns the full Response object with parsed data available in `_data` property.

```typescript theme={null}
const response = await ofetch.raw('/api/users')
console.log(response.status) // 200
console.log(response._data) // parsed data
```

<ParamField path="request" type="FetchRequest" required>
  URL string or Request object
</ParamField>

<ParamField path="options" type="FetchOptions<R>">
  Request configuration options
</ParamField>

<ResponseField name="return" type="Promise<FetchResponse<MappedResponseType<R, T>>>">
  Full Response object with `_data` property containing parsed response. See [FetchResponse](/api/types/response-types#fetchresponse).
</ResponseField>

### ofetch.native()

Access to the native `fetch` function used internally.

```typescript theme={null}
const response = await ofetch.native('/api/users')
```

<ParamField path="...args" type="Parameters<typeof globalThis.fetch>">
  Standard fetch API arguments
</ParamField>

<ResponseField name="return" type="Promise<Response>">
  Native fetch Response object
</ResponseField>

### ofetch.create()

Create a new `ofetch` instance with custom default options.

```typescript theme={null}
const apiFetch = ofetch.create({
  baseURL: 'https://api.example.com',
  headers: {
    Authorization: 'Bearer token'
  }
})

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

<ParamField path="defaults" type="FetchOptions" required>
  Default options to apply to all requests made with this instance. See [FetchOptions](/api/options/fetch-options).
</ParamField>

<ParamField path="globalOptions" type="CreateFetchOptions">
  Global configuration options. See [createFetch](/api/create-fetch).
</ParamField>

<ResponseField name="return" type="$Fetch">
  A new ofetch instance with the provided defaults
</ResponseField>

## Type Parameters

<ParamField path="T" type="any" default="any">
  The expected type of the response data
</ParamField>

<ParamField path="R" type="ResponseType" default="json">
  The response type: `"json"`, `"text"`, `"blob"`, `"arrayBuffer"`, or `"stream"`. See [ResponseType](/api/types/response-types#responsetype).
</ParamField>

## Related Types

* [FetchOptions](/api/options/fetch-options) - Request configuration options
* [FetchResponse](/api/types/response-types#fetchresponse) - Response interface
* [ResponseType](/api/types/response-types#responsetype) - Available response types
* [CreateFetchOptions](/api/create-fetch) - Instance creation options
