- Notifications
You must be signed in to change notification settings - Fork47
Node.js connector for the FTX.com & FTX.us APIs and WebSockets, with TypeScript & browser support.
License
tiagosiebler/ftx-api
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Node.js connector for the FTX APIs and WebSockets, with TypeScript & browser support.
npm install --save ftx-api
- Issues? Check theissues tab.
- Discuss & collaborate with other node devs? Join ourNode.js Algo Traders engineering community on telegram.
Check out my related projects:
- Try my connectors:
- Try my misc utilities:
- Check out my examples:
Most methods accept JS objects, except for methods with only one parameter. These can be populated using parameters specified by FTX's API documentation.
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.
- examples - demonstrations on various workflows using this library
Create API credentials at FTX
Import and instance theRestClient
to access all REST API methods.
- All methods return promises.
- Supports subaccounts.
To use the REST APIs, import the `RestClient`. Click here to expand and see full sample:
const{ RestClient}=require('ftx-api');constrestClientOptions={// override the max size of the request window (in ms)recv_window?:number;// how often to sync time drift with FTX serverssync_interval_ms?:number|string;// Default: false. Disable above sync mechanism if true.disable_time_sync?:boolean;// Default: false. If true, we'll throw errors if any params are undefinedstrict_param_validation?:boolean;// Optionally override API protocol + domain// e.g 'https://ftx.us/api'baseUrl?:string;// Default: true. whether to try and post-process request exceptions.parse_exceptions?:boolean;// Subaccount nickname URI-encodedsubAccountName?:string;};constAPI_KEY='xxx';constPRIVATE_KEY='yyy';constclient=newRestClient(API_KEY,PRIVATE_KEY,// restClientOptions,// requestLibraryOptions);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);});
Seerest-client.ts for further information.
- Automatically connect to FTX websockets
- Automatically authenticate, if key & secret are provided.
- Automatically checks connection integrity. If connection stale (no response to pings), automatically reconnects, re-authenticates and resubscribes to previous topics.
- Supports subaccounts.
WebSocket channels can be subscribed to via the `WebsocketClient`. Click here to expand and see full sample:
const{ WebsocketClient}=require('ftx-api');constAPI_KEY='xxx';constPRIVATE_KEY='yyy';constwsConfig={key:API_KEY,secret:PRIVATE_KEY,/* The following parameters are optional: */// Subaccount nickname// subAccountName: 'sub1',// how long to wait (in ms) before deciding the connection should be terminated & reconnected// pongTimeout: 1000,// how often to check (in ms) that WS connection is still alive// pingInterval: 10000,// how long to wait before attempting to reconnect (in ms) after connection is closed// reconnectTimeout: 500,// config options sent to RestClient (used for time sync). See RestClient docs.// restOptions: { },// config for axios used for HTTP requests. E.g for proxy support// requestOptions: { }// override which URL to use for websocket connections// wsUrl: 'wss://example.ftx.com/ws'};constws=newWebsocketClient(wsConfig);// subscribe to multiple topics at oncews.subscribe(['fills','orders']);// and/or subscribe to individual topics on demandws.subscribe('fills');// and/or subscribe to complex topics on demand, one at a timews.subscribe({channel:'trades',market:'BTC-PERP'});// or as a list of complex topicsws.subscribe([{channel:'trades',market:'BTC-PERP'},{channel:'orderbookGrouped',market:'BTC-PERP',grouping:500}]);// Listen to events coming from websockets. This is the primary data sourcews.on('update',data=>{console.log('update',data);});// Optional: Listen to websocket connection open event (automatic after subscribing to one or more topics)ws.on('open',({ event})=>{console.log('connection opened');});// Optional: Listen to responses to websocket queries (e.g. the response after subscribing to a topic)ws.on('response',response=>{console.log('response',response);});// Optional: Listen to connection close event. Unexpected connection closes are automatically reconnected.ws.on('close',()=>{console.log('connection closed');});// Optional: Listen to raw error events.// Note: responses to invalid topics are currently only sent in the "response" event.ws.on('error',err=>{console.error('ERR',err);});
Seewebsocket-client.ts for further information.
More demonstrations can be found in theexamples folder.
Pass a custom logger which supports the log methodssilly
,debug
,notice
,info
,warning
anderror
, or override methods from the default logger as desired.
Click here to expand and see full sample:
const{ WebsocketClient, DefaultLogger}=require('ftx-api');// Disable all logging on the silly levelDefaultLogger.silly=()=>{};constws=newWebsocketClient({key:'xxx',secret:'yyy'},DefaultLogger);
Build a bundle using webpack:
npm install
npm build
npm pack
The bundle can be found indist/
. Altough usage should be largely consistent, smaller differences will exist. Documentation is still TODO.
This client also supports the US FTX exchange. Simply set the "domain" to "ftxus" in both the RestClient and the WebsocketClient. Seeexamples/ftxus.ts for a demonstration.
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 are encouraged, I will review any incoming pull requests. See the issues tab for todo items.
About
Node.js connector for the FTX.com & FTX.us APIs and WebSockets, with TypeScript & browser support.