Client Configuration
Client Configuration
You create a client with createClient(config). It returns a RankHikerClient.
import { createClient } from '@rankhiker/sdk';
const rh = createClient({ apiKey: process.env.RANKHIKER_API_KEY! });
Config options
| Option | Type | Default | Notes |
|---|---|---|---|
apiKey | string | (required) | Your rh_export_... key. Throws a RankHikerError if missing or empty. |
baseUrl | string | https://rankhiker.com | API host. Trailing slashes are stripped. The default works in the browser too; do not use the www host (it redirects and can drop the key header). |
fetch | typeof fetch | global fetch | Custom fetch implementation: legacy Node, a proxy, or a caching layer. |
requestInit | RequestInit | {} | Merged into every request. Useful for Next.js cache hints. |
client.baseUrl, holding the resolved host.
baseUrl override and staging
// Point at a staging host
const staging = createClient({
apiKey: process.env.RANKHIKER_API_KEY!,
baseUrl: 'https://staging.rankhiker.com',
});
Custom fetch (proxy or cache)
Provide your own fetch to route requests through a proxy, add caching, or supply a polyfill on older Node runtimes:
import { createClient } from '@rankhiker/sdk';
import nodeFetch from 'node-fetch';
const rh = createClient({
apiKey: process.env.RANKHIKER_API_KEY!,
fetch: nodeFetch as unknown as typeof fetch,
});
requestInit for Next.js caching
requestInit is spread into every underlying request, so you can apply Next.js fetch cache directives globally.
Incremental Static Regeneration (revalidate every 5 minutes):
const rh = createClient({
apiKey: process.env.RANKHIKER_API_KEY!,
requestInit: { next: { revalidate: 300 } },
});
Always fetch fresh (no caching):
const rh = createClient({
apiKey: process.env.RANKHIKER_API_KEY!,
requestInit: { cache: 'no-store' },
});
Any other RequestInit field works too (custom headers, signals, and so on). Note that the SDK always sets the request method to GET and adds the X-API-Key and Accept headers, so those are not overridable through requestInit.
Was this article helpful?