EMX
The official Node.js library for the EMX API.
Features
- Public, Authenticated, and WebSocket API client libraries
- Built-in HMAC signing
Installation
npm install emx
You can learn about the API responses of each endpoint by reading our documentation.
Quick Start
The EMX API has both public and private endpoints. If you're only interested in
the public endpoints, you should use PublicClient
.
const EMX = ;const publicClient = ;
All methods, unless otherwise specified, can be used with either a promise or callback API.
Using Promises
publicClient ;
The promise API can be used as expected in async
functions in ES2017+
environments:
{ try const contracts = await publicClient; catch error /* ... */ }
Using Callbacks
Your callback should accept three arguments:
error
: contains an error message (string
), ornull
if no error was encounteredresponse
: a generic HTTP response abstraction created by therequest
librarydata
: contains data returned by the EMX API, orundefined
if an error was encountered
publicClient;
The Public API Client
const publicClient = apiURI;
apiURI
optional - defaults to 'https://api.testnet.emx.com' if not specified.
Public API Methods
These methods allow you to pull public contract information and market data. You may pass pagination and filtering parameters, as described in the EMX API documentation. These methods are subject to rate limiting.
// get metadata for all active contracts.publicClient;
getContractL2OrderBook()
getContractQuote()
getContractAuction(auctionCode)
,getContractAuctions(contractCode)
The Authenticated API Client
The private exchange API endpoints require you to authenticate with an EMX API key. You can create a new API key in your exchange account's settings. You can also specify the API URI (defaults to https://api.testnet.emx.com
).
const authedClient = 'your_api_key' 'your_b64_secret' 'https://api.testnet.emx.com';
Like PublicClient
, all API methods can be used with either callbacks or will return promises.
AuthenticatedClient
inherits all of the API methods from
PublicClient
, so if you're hitting both public and private API endpoints you only need to create a single client.
Private API Methods
// Buy 5.01 BTCH19 @ 3800 USDconst buyParams = contractCode: 'BTCH19' side: 'buy' size: '5.0125' // BTC price: '3800.00' // USD type: 'limit';authedClient; // Sell 2.5 BTCH19 @ 3800 USDconst sellParams = contractCode: 'BTCH19' side: 'sell' size: '2.5' // BTC price: '3800.00' // USD type: 'limit';authedClient;
getOrder(orderId)
,getOrders()
modifyOrder(orderId, mods)
cancelOrder(orderId)
,cancelOrders(contractCode)
getFills()
getPositions()
getAccount(traderId)
,getAccounts()
WebSocket Client
The WebSocketClient
allows you to connect and listen to (and send) the exchange
WebSocket messages.
const websocket = 'BTCH19'; websocket;websocket;websocket;
To access higher rate limits and/or to enable trading across the socket, you may authenticate with an EMX API key. You can create a new API key in your exchange account's settings. You can also specify the API URI (defaults to wss://api.testnet.emx.com
).
const websocket = 'your_api_key' 'your_b64_secret' 'wss://api.testnet.emx.com';
Once you create the WebSocketClient
object, you'll be connected to the socket and you may subscribe to channels:
websocket; websocket;
The following events will be emitted from the WebSocketClient
:
open
message
close
error
If you subscribe to the trading
channel, you may call the following order-management methods: