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.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
Add query parameters
Add query parameters using the The URL will automatically become
query option:https://api.github.com/repos/unjs/ofetch/tags?per_page=2.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, useofetch.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