ftx-api

Node.js connector for the FTX APIs and WebSockets, with TypeScript & browser support.
Installation
npm install --save ftx-api
Issues & Discussion
Documentation
Most methods accept JS objects, except for methods with only one parameter. These can be populated using parameters specified by FTX's API documentation.
Structure
This project uses typescript. Resources are stored in 3 key structures:
- src - the whole connector written in typescript
- lib - the javascript version of the project (compiled from typescript). This should not be edited directly, as it will be overwritten with each release.
- dist - the packed bundle of the project for use in browser environments.
Usage
Create API credentials at FTX
REST API Clients
Import and instance the RestClient
to access all REST API methods. All methods return promises.
Example
To use the REST APIs, import the `RestClient`. Click here to expand and see full sample:
const { RestClient } = require('ftx-api');
const restClientOptions = {
recv_window?: number;
sync_interval_ms?: number | string;
disable_time_sync?: boolean;
strict_param_validation?: boolean;
baseUrl?: string;
parse_exceptions?: boolean;
subAccountName?: string;
};
const API_KEY = 'xxx';
const PRIVATE_KEY = 'yyy';
const client = new InverseClient(
API_KEY,
PRIVATE_KEY,
);
client.getMarkets()
.then(result => {
console.log("getMarkets result: ", result);
})
.catch(err => {
console.error("getMarkets error: ", err);
});
client.setAccountLeverage(5)
.then(result => {
console.log("setAccountLeverage result: ", result);
})
.catch(err => {
console.error("setAccountLeverage error: ", err);
});
See rest-client.ts for further information.
WebSockets
Inverse & linear WebSockets can be used via a shared `WebsocketClient`. Click here to expand and see full sample:
const { WebsocketClient } = require('ftx-api');
const API_KEY = 'xxx';
const PRIVATE_KEY = 'yyy';
const wsConfig = {
key: API_KEY,
secret: PRIVATE_KEY,
};
const ws = new WebsocketClient(wsConfig);
ws.subscribe(['ticker', 'markets']);
ws.subscribe('trades');
ws.on('update', data => {
console.log('update', data);
});
ws.on('open', ({ wsKey, event }) => {
console.log('connection open for websocket with ID: ' + wsKey);
});
ws.on('response', response => {
console.log('response', response);
});
ws.on('close', () => {
console.log('connection closed');
});
ws.on('error', err => {
console.error('ERR', err);
});
See websocket-client.ts for further information.
Customise Logging
Pass a custom logger which supports the log methods silly
, debug
, notice
, info
, warning
and error
, or override methods from the default logger as desired.
Click here to expand and see full sample:
const { WebsocketClient, DefaultLogger } = require('ftx-api');
DefaultLogger.silly = () => {};
const ws = new WebsocketClient(
{ key: 'xxx', secret: 'yyy' },
DefaultLogger
);
Browser Usage
Build a bundle using webpack:
npm install
npm build
npm pack
The bundle can be found in dist/
. Altough usage should be largely consistent, smaller differences will exist. Documentation is still TODO.
Contributions & Thanks
Donations
tiagosiebler
If you found this project interesting or useful, create accounts with my referral links:
Or buy me a coffee using any of these:
- BTC:
1C6GWZL1XW3jrjpPTS863XtZiXL1aTK7Jk
- ETH (ERC20):
0xd773d8e6a50758e1ada699bb6c4f98bb4abf82da
Contributions & Pull Requests
Contributions are encouraged, I will review any incoming pull requests. See the issues tab for todo items.