├── .env.example
├── .github
└── workflows
│ └── npm-publish.yml
├── .gitignore
├── .prettierignore
├── .prettierrc.js
├── BROWSER.md
├── README.md
├── bug_report_template.md
├── changelog.txt
├── deno.json
├── examples
├── browser-test.html
├── exchange_api_testing.cjs
├── info_api_testing.cjs
└── ws_test.cjs
├── mod.ts
├── package-lock.json
├── package.json
├── pnpm-lock.yaml
├── src
├── browser.ts
├── global.d.ts
├── index.ts
├── rest
│ ├── custom.ts
│ ├── exchange.ts
│ ├── info.ts
│ └── info
│ │ ├── general.ts
│ │ ├── perpetuals.ts
│ │ └── spot.ts
├── types
│ ├── constants.ts
│ └── index.ts
├── utils
│ ├── environment.ts
│ ├── errors.ts
│ ├── helpers.ts
│ ├── rateLimiter.ts
│ ├── signing.ts
│ └── symbolConversion.ts
└── websocket
│ ├── connection.ts
│ └── subscriptions.ts
├── tests
└── test1.js
├── tsconfig.json
├── tsup.config.ts
└── update_instructions.txt
/.env.example:
--------------------------------------------------------------------------------
1 | private_key=private_key
2 | user_address=xxx
3 | cloid=cloid
--------------------------------------------------------------------------------
/.github/workflows/npm-publish.yml:
--------------------------------------------------------------------------------
1 | name: Publish Package to NPM
2 | on:
3 | push:
4 | tags:
5 | - 'v*'
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v3
12 | - uses: actions/setup-node@v3
13 | with:
14 | node-version: '18.x'
15 | registry-url: 'https://registry.npmjs.org'
16 | - run: npm ci
17 | - run: npm run build
18 | - run: npm publish
19 | env:
20 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Dependency directories
2 | node_modules/
3 | *.js.map
4 | src/**/*.js
5 | **/venv/** */
6 | .env
7 | .vscode/
8 | /dist
9 | *.env
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | # Build outputs
2 | dist/
3 | build/
4 | coverage/
5 |
6 | # Package files
7 | node_modules/
8 | package-lock.json
9 | pnpm-lock.yaml
10 | yarn.lock
11 |
12 | # Generated files
13 | *.min.js
14 | *.bundle.js
15 |
16 | # Specific directories
17 | .github/
18 | .vscode/
19 |
20 | # Misc
21 | .DS_Store
22 | .env
23 | .env.*
24 |
--------------------------------------------------------------------------------
/.prettierrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | printWidth: 100,
3 | tabWidth: 2,
4 | useTabs: false,
5 | semi: true,
6 | singleQuote: true,
7 | quoteProps: 'as-needed',
8 | jsxSingleQuote: false,
9 | trailingComma: 'es5',
10 | bracketSpacing: true,
11 | bracketSameLine: false,
12 | arrowParens: 'avoid',
13 | endOfLine: 'lf',
14 | overrides: [
15 | {
16 | files: '*.ts',
17 | options: {
18 | parser: 'typescript',
19 | },
20 | },
21 | ],
22 | };
23 |
--------------------------------------------------------------------------------
/BROWSER.md:
--------------------------------------------------------------------------------
1 | # Browser Usage Guide
2 |
3 | The Hyperliquid SDK supports browser environments and can be used in several ways:
4 |
5 | ## Direct Script Include
6 |
7 | ```html
8 |
9 |
15 | ```
16 |
17 | ## ES Module Import
18 |
19 | ```javascript
20 | import { Hyperliquid } from 'hyperliquid';
21 |
22 | const sdk = new Hyperliquid({
23 | testnet: true,
24 | enableWs: true
25 | });
26 | ```
27 |
28 | 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
29 |
30 | ## Features and Limitations
31 |
32 | ### Supported Features
33 | - REST API calls
34 | - WebSocket real-time updates
35 | - Wallet integration via ethers.js
36 | - Asset conversion and management
37 | - Real-time market data subscriptions
38 |
39 | ### Browser-Specific Considerations
40 |
41 | 1. **WebSocket Support**
42 | - WebSocket functionality is automatically enabled in supported browsers
43 | - Falls back gracefully if WebSocket is not supported
44 |
45 | 2. **Private Key Handling**
46 | - Never store private keys in browser storage
47 | - Consider using Web3 wallet providers for key management
48 | - Use environment variables for server-side private keys
49 |
50 | 3. **CORS**
51 | - All API endpoints support CORS
52 | - No additional configuration needed for browser usage
53 |
54 | 4. **Performance**
55 | - Rate limiting is handled automatically
56 | - WebSocket connections are managed with automatic reconnection
57 | - Memory usage is optimized for browser environments
58 |
59 | ## Example Usage
60 |
61 | ```javascript
62 | async function init() {
63 | const sdk = new Hyperliquid({
64 | testnet: true,
65 | enableWs: true
66 | });
67 |
68 | // Connect to the API
69 | await sdk.connect();
70 |
71 | // Get market data
72 | const assets = await sdk.info.getAllAssets();
73 | console.log('Available assets:', assets);
74 |
75 | // Subscribe to real-time updates
76 | sdk.subscriptions.subscribeToAllMids((data) => {
77 | console.log('Price update:', data);
78 | });
79 |
80 | // Clean up when done
81 | // sdk.disconnect();
82 | }
83 | ```
84 |
85 | ## Error Handling
86 |
87 | ```javascript
88 | try {
89 | await sdk.connect();
90 | } catch (error) {
91 | if (error.message.includes('WebSocket')) {
92 | console.error('WebSocket connection failed:', error);
93 | } else if (error.message.includes('network')) {
94 | console.error('Network error:', error);
95 | } else {
96 | console.error('Unknown error:', error);
97 | }
98 | }
99 | ```
100 |
101 | ## Best Practices
102 |
103 | 1. Always call `connect()` before using the SDK
104 | 2. Handle WebSocket reconnection events
105 | 3. Implement proper error handling
106 | 4. Clean up resources using `disconnect()` when done
107 | 5. Don't store sensitive data in browser storage
108 | 6. Use appropriate security measures for private keys
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Hyperliquid API SDK
2 |
3 | Typescript SDK to more easily interact with Hyperliquid's API
4 |
5 | All info on the Hyperliquid API can be found here: [HyperLiquid API Documentation](https://hyperliquid.gitbook.io/hyperliquid-docs)
6 |
7 | ## Features
8 |
9 | - Complete API coverage for both REST and WebSocket endpoints
10 | - TypeScript support with comprehensive type definitions
11 | - Browser and Node.js compatibility
12 | - Automatic handling of trailing zeros in price and size fields
13 | - Rate limiting support
14 | - Comprehensive error handling
15 |
16 | ## Installation
17 |
18 | Choose your preferred installation method:
19 |
20 | ### Package Managers
21 |
22 | ```bash
23 | # npm
24 | npm i --save hyperliquid
25 |
26 | # yarn
27 | yarn add hyperliquid
28 |
29 | # pnpm
30 | pnpm add hyperliquid
31 |
32 | # bun
33 | bun i hyperliquid
34 | ```
35 |
36 | ### Node.js Version Requirements for WebSocket Functionality
37 |
38 | This SDK uses native WebSocket implementation which requires Node.js version 22 or higher. If you're using an earlier version of Node.js, you'll need to install the `ws` package to use the WebSocket functionality:
39 |
40 | ```bash
41 | npm install ws
42 | ```
43 |
44 | ### Direct Web Usage
45 |
46 | ```html
47 |
48 |
49 |
50 |
51 |
54 |
55 | ```
56 |
57 | The SDK provides two browser bundles:
58 | - `browser.global.js`: UMD bundle that exposes the SDK globally as `HyperliquidSDK`. Use this with regular `