fetch implementation and provide custom request options for advanced use cases like proxy configuration, custom headers, and runtime-specific networking.
Custom Fetch Function
Using a Custom Fetch Implementation
By default, the SDK uses the globalfetch function. You can override this with your own implementation:
Polyfilling Global Fetch
Alternatively, you can polyfill the globalfetch:
Fetch Options
Client-Level Options
Set default fetch options for all requests:Per-Request Options
Override fetch options for individual requests:Proxy Configuration
Different JavaScript runtimes have different proxy mechanisms:Node.js (using undici)
Bun
Deno
Cloudflare Workers
Cloudflare Workers don’t support proxies directly, but you can use a custom fetch:Advanced Fetch Customization
Request Interceptor
Create a wrapper to intercept all requests:Retry Logic
Add custom retry logic:maxRetries option instead:
Request/Response Logging
SSL/TLS Configuration
Node.js Custom Certificates
Disable SSL Verification (NOT RECOMMENDED)
Timeout Configuration
Client-Level Timeout
Per-Request Timeout
AbortController
Custom Headers
Global Headers
Per-Request Headers
Remove Default Header
Keep-Alive and Connection Pooling
Node.js (undici)
Environment-Specific Examples
Next.js API Route
Cloudflare Workers
Deno Deploy
Best Practices
- Reuse client instances: Create one client and reuse it across requests
- Use connection pooling: Configure agents for better performance
- Set appropriate timeouts: Balance between user experience and reliability
- Secure proxy credentials: Never hardcode credentials
- Log strategically: Don’t log sensitive data like API keys
- Handle fetch errors: Implement proper error handling
- Test custom configurations: Verify behavior in your target environment
Troubleshooting
”fetch is not defined”
Ensure you’re on a supported runtime or provide a fetch implementation:Proxy Not Working
Verify you’re using the correct proxy configuration for your runtime:- Node.js: Use
dispatcherwith undici’sProxyAgent - Bun: Use
proxyoption - Deno: Use
clientwithDeno.createHttpClient
SSL Certificate Errors
For custom certificates, ensure the file paths are correct and the certificates are valid.Timeout Issues
If requests are timing out:- Increase the timeout value
- Check network connectivity
- Verify proxy configuration
- Enable logging to debug