baseURL option allows you to set a base path for all requests, making it easy to work with APIs without repeating the full URL.
Basic Usage
Smart Slash Handling
ofetch intelligently handles trailing and leading slashes, so you don’t have to worry about double slashes or missing separators:Creating a Configured Instance
The most common pattern is to create a configured instance with$fetch.create():
Nested Paths
baseURL works with nested API paths:With Query Parameters
baseURL combines seamlessly with query parameters:Overriding baseURL
You can override the baseURL for specific requests by providing a full URL:Environment-Based Configuration
Common pattern for different environments:Multiple API Clients
Create different clients for different services:Type Definition
Fromsrc/types.ts:25:
Implementation Details
The baseURL logic is handled atsrc/fetch.ts:114-117 using the withBase function from src/utils.url.ts:40-51:
- If the request already starts with the baseURL, it’s returned as-is
- Trailing slashes on baseURL are removed
- The request path is joined with proper slash handling
- Double slashes:
https://api.example.com//users - Missing slashes:
https://api.example.comusers