The Hyperliquid SDK supports browser environments and can be used in several ways:
<script src="https://unpkg.com/hyperliquid/dist/browser.global.js"></script>
<script>
const sdk = new HyperliquidSDK.Hyperliquid({
testnet: true,
enableWs: true
});
</script>
import { Hyperliquid } from 'hyperliquid';
const sdk = new Hyperliquid({
testnet: true,
enableWs: true
});
Note: See /examples/browser-test.html for a complete example of how to use the SDK in a browser with all methods supported. P.s. Try running it in a local server to just test the SDK/API in general
- REST API calls
- WebSocket real-time updates
- Wallet integration via ethers.js
- Asset conversion and management
- Real-time market data subscriptions
-
WebSocket Support
- WebSocket functionality is automatically enabled in supported browsers
- Falls back gracefully if WebSocket is not supported
-
Private Key Handling
- Never store private keys in browser storage
- Consider using Web3 wallet providers for key management
- Use environment variables for server-side private keys
-
CORS
- All API endpoints support CORS
- No additional configuration needed for browser usage
-
Performance
- Rate limiting is handled automatically
- WebSocket connections are managed with automatic reconnection
- Memory usage is optimized for browser environments
async function init() {
const sdk = new Hyperliquid({
testnet: true,
enableWs: true
});
// Connect to the API
await sdk.connect();
// Get market data
const assets = await sdk.info.getAllAssets();
console.log('Available assets:', assets);
// Subscribe to real-time updates
sdk.subscriptions.subscribeToAllMids((data) => {
console.log('Price update:', data);
});
// Clean up when done
// sdk.disconnect();
}
try {
await sdk.connect();
} catch (error) {
if (error.message.includes('WebSocket')) {
console.error('WebSocket connection failed:', error);
} else if (error.message.includes('network')) {
console.error('Network error:', error);
} else {
console.error('Unknown error:', error);
}
}
- Always call
connect()
before using the SDK - Handle WebSocket reconnection events
- Implement proper error handling
- Clean up resources using
disconnect()
when done - Don't store sensitive data in browser storage
- Use appropriate security measures for private keys