Skip to main content
This guide will help you make your first HTTP request with ofetch.
1

Install ofetch

Install ofetch using your preferred package manager:
2

Import ofetch

Import ofetch in your JavaScript or TypeScript file:
You can also use the $fetch alias: import { $fetch } from 'ofetch'
3

Make your first request

Make a simple GET request. ofetch automatically parses JSON responses:
The response is automatically parsed as JSON and returned directly - no need to call .json() manually.
4

Send data with POST

Send data to an API endpoint. ofetch automatically stringifies objects and sets the correct headers:
ofetch automatically:
  • Converts the body object to JSON
  • Sets Content-Type: application/json
  • Sets Accept: application/json
5

Add query parameters

Add query parameters using the query option:
The URL will automatically become https://api.github.com/repos/unjs/ofetch/tags?per_page=2.
6

Handle errors

ofetch automatically throws errors for failed requests with helpful error messages:
The error includes:
  • error.data - Parsed response body
  • error.status - HTTP status code
  • error.statusText - HTTP status message
  • A clean stack trace with internals hidden

Common patterns

Using a base URL

When working with an API, you can set a base URL to avoid repeating it:

Adding headers

Add custom headers to your requests:

Accessing raw response

If you need access to response headers or other metadata, use ofetch.raw:

Next steps

You now know the basics of ofetch. Explore more features:
  • Configure automatic retries for failed requests
  • Use interceptors to modify requests and responses
  • Set timeouts to prevent hanging requests
  • Handle different response types (blob, stream, text)
  • Work with TypeScript for full type safety