Option 1: TypeScript import (recommended)
This is the modern approach. Import the SDK directly into your frontend project with full TypeScript support. It requires polyfilling a couple of Node.js built-ins that browsers do not provide natively.{
"compilerOptions": {
"paths": {
"crypto": ["./node_modules/crypto-browserify"],
"stream": ["./node_modules/stream-browserify"]
}
}
}
Add this in the global context of your application (for example, in Angular’s
polyfills.ts, or at the top of your entry file):import { RestClient } from 'okx-api';
// Public endpoints only — do not include private credentials in frontend code
const client = new RestClient();
(async () => {
try {
const instruments = await client.getInstruments({ instType: 'SPOT' });
console.log('SPOT instruments:', instruments);
} catch (e) {
console.error('Request failed:', e);
}
})();
Option 2: Webpack bundle (script tag)
This is the legacy approach for including the SDK on a plain HTML page via a<script> tag. It produces a minified UMD bundle in the dist/ directory.
// webpack/webpack.config.cjs (excerpt)
module.exports = {
entry: './dist/cjs/index.js',
output: {
filename: 'okxapi.js',
library: 'okxapi',
libraryTarget: 'umd',
},
resolve: {
alias: {
// Swap Node.js internals for browser-safe stubs
[path.resolve(__dirname, '../dist/cjs/util/node-support.js')]:
path.resolve(__dirname, '../dist/cjs/util/browser-support.js'),
},
fallback: {
http: false,
https: false,
},
},
};
Caveats
The
keepAlive option on RestClientOptions uses Node.js’s https.Agent, which is not available in browsers. This option is silently ignored in browser environments.- No private keys in the browser. Use a server-side proxy if you need to make authenticated requests from a web application.
- CORS: OKX’s API endpoints may not allow direct browser requests from all origins. Check their CORS policy before building browser-based integrations.
- Bundle size: The webpack bundle includes the full SDK. Use tree-shaking via the TypeScript import approach if bundle size is a concern.

