Getting Started
Introduction
Welcome to the Econobit Exchange API! You can use our API to access the endpoints, which can get information on tickers, orderbooks, trade history, account balances, deposits, withdraws, post and cancel orders, and more.
We have language bindings in TypeScript and JavaScript! You can view code examples in the dark area to the right, and you can switch the programming language of the examples with the tabs in the top right.
This API documentation page was created with Slate open-source project.
Endpoints
There are two working environments: livenet
for production and testnet
for testing purporses.
Some features are accessed publicly and others require an API key based authentication. All data messages and responses are in JSON format.
REST API
testnet
base URL endpoint: https://testnet.econobit.io/api
livenet
base URL endpoint: https://econobit.io/api
WebSocket
testnet
base websocket endpoint: wss://testnet.econobit.io/ws
livenet
base websocket endpoint: wss://econobit.io/ws
Authentication
To authorize, use this code:
import { Econobit } from 'econobit'
const auth = async () => {
try {
const econobit = new Econobit({
testnet: true, // for livenet comment this line
// debug: true
})
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.connect()
console.log('connected:', econobit.isConnected)
await econobit.login({ key, secret })
console.log('logged:', econobit.isLogged)
await econobit.logout()
console.log('logged:', econobit.isLogged)
} catch (err) {
console.error('ERROR:', err)
}
}
auth()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onResponse(ws)
}
const logout = async (ws) => {
ws.send(JSON.stringify({ type: 'ApiKey.Logout' }))
return onResponse(ws)
}
const auth = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
console.log('connected:', ws.readyState === ws.OPEN)
try {
await login(ws, { key, secret })
console.log('logged:', true)
await logout(ws)
console.log('logged:', false)
} catch (err) {
console.error('ERROR:', err)
}
})
}
auth()
Make sure to replace
API_KEY
andAPI_SECRET
with yours.
Econobit uses API Keys to allow access to the Private WebSocket methods. You can register a new Econobit API Key at our plataform.
Public REST API
Ticker
import fetch from 'node-fetch' // only for Node.js
const baseUrl = 'https://testnet.econobit.io/api'
const getTickers = async (symbol: string) => {
const resp = await fetch(`${baseUrl}/ticker/${(symbol || '')}`)
const tickers = await resp.json()
console.table(tickers)
}
try {
getTickers() // log all tickers
getTickers('btc-brl') // log only BTC-BRL market
} catch (err) {
console.error(err)
}
const fetch = require('node-fetch') // only for Node.js
const baseUrl = 'https://testnet.econobit.io/api'
const getTickers = async (symbol) => {
const resp = await fetch(`${baseUrl}/ticker/${(symbol || '')}`)
const tickers = await resp.json()
console.table(tickers)
}
try {
getTickers() // log all tickers
getTickers('btc-brl') // log only BTC-BRL market
} catch (err) {
console.error(err)
}
The above commands returns JSON structured like this:
{
"BTC-BRL": {
"marketId": 1,
"symbol": "BTC-BRL",
"lastPrice": 98965.32,
"delta24h": 0.0773,
"high24h": 100030.21,
"low24h": 94004.57,
"volume24h": 12.9115,
"trades24h": 2325,
"makerFee": 0,
"takerFee": 0.005,
"askPrice": 100000.01,
"bidPrice": 99995.00,
"date": "2020-12-04T00:10:03.604Z"
},
...
}
This endpoint retrieves all ticker information.
HTTP Request
GET /api/ticker/[symbol]
Query Parameters
Parameter | Description |
---|---|
symbol (optional) | If passed, only that symbol will be returned, if not, all symbols are returned |
Example
https://testnet.econobit.io/api/ticker/BTC-BRL
Order Book
import fetch from 'node-fetch' // only for Node.js
const baseUrl = 'https://testnet.econobit.io/api'
const getOrderbook = async (symbol: string) => {
const resp = await fetch(`${baseUrl}/orderbook/${symbol}`)
const orderbook = await resp.json()
console.log(orderbook)
}
try {
getOrderbook('btc-brl')
} catch (err) {
console.error(err)
}
const fetch = require('node-fetch') // only for Node.js
const baseUrl = 'https://testnet.econobit.io/api'
const getOrderbook = async (symbol) => {
const resp = await fetch(`${baseUrl}/orderbook/${symbol}`)
const orderbook = await resp.json()
console.log(orderbook)
}
try {
getOrderbook('btc-brl')
} catch (err) {
console.error(err)
}
The above command returns JSON structured like this:
{
"timestamp": "2021-03-03T14:44:42.469Z",
"bids": {
"255434.00": 0.0034,
"250000.00": 0.001,
"243567.00": 0.0012,
"240000.00": 0.001,
"233343.00": 0.0031,
"230000.00": 0.001,
"225434.00": 0.003,
"220000.00": 0.001,
"210000.00": 0.001,
"200000.00": 0.001
},
"asks": {
"310000.00": 0.001,
"320000.00": 0.002,
"340000.00": 0.002,
"355435.00": 0.0033,
"363255.00": 0.0012,
"370500.00": 0.001,
"373555.00": 0.001,
"380540.00": 0.0012
},
"symbol": "BTC-BRL"
}
This endpoint retrieves the orderbook from a requested market.
HTTP Request
GET /api/orderbook/<symbol>
Query Parameters
Parameter | Description |
---|---|
symbol (required) | Only the orderbook of the passed market symbol will be returned |
Example
https://testnet.econobit.io/api/orderbook/BTC-BRL
Trade History
import fetch from 'node-fetch' // only for Node.js
const baseUrl = 'https://testnet.econobit.io/api'
const getTrades = async (symbol: string) => {
const resp = await fetch(`${baseUrl}/trades/${symbol}`)
const trades = await resp.json()
console.log(trades)
}
try {
getTrades('btc-brl')
} catch (err) {
console.error(err)
}
const fetch = require('node-fetch') // only for Node.js
const baseUrl = 'https://testnet.econobit.io/api'
const getTrades = async (symbol) => {
const resp = await fetch(`${baseUrl}/trades/${symbol}`)
const trades = await resp.json()
console.log(trades)
}
try {
getTrades('btc-brl')
} catch (err) {
console.error(err)
}
The above command returns JSON structured like this:
{
"trades": [
{
"amount": 0.0001,
"date": "2021-03-09T06:29:19.381Z",
"price": 300000,
"side": "buy",
"tradeId": 24
},
{
"amount": 0.02,
"date": "2021-03-09T06:29:19.340Z",
"price": 285000,
"side": "buy",
"tradeId": 23
},
...
],
"symbol": "BTC-BRL"
}
This endpoint retrieves the trade history from a requested market.
HTTP Request
GET /api/trades/<symbol>
Query Parameters
Parameter | Description |
---|---|
symbol (required) | Only the last trades of the passed market symbol will be returned |
Example
https://testnet.econobit.io/api/trades/BTC-BRL
Public WebSocket
Ticker
import { Econobit } from 'econobit'
const ticker = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const { list, onUpdate } = await econobit.subscribe.ticker()
console.log(list)
onUpdate(update => console.log('ticker update:', update))
} catch (err) {
console.error('ERROR:', err)
}
}
ticker()
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const ticker = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
ws.send(JSON.stringify({ type: 'Subscribe.Tickers' }))
emitter.once('Ticker.Load', (action) => console.log(action.tickers.list))
emitter.on('Ticker.Update', (action) => console.log(action.ticker))
} catch (err) {
console.error('ERROR:', err)
}
})
}
ticker()
The above command returns JSON structured like this:
{
"BTC-BRL": {
"marketId": 1,
"symbol": "BTC-BRL",
"lastPrice": 98965.32,
"delta24h": 0.0773,
"high24h": 100030.21,
"low24h": 94004.57,
"volume24h": 12.9115,
"trades24h": 2325,
"makerFee": 0,
"takerFee": 0.005,
"askPrice": 100000.01,
"bidPrice": 99995.00,
"date": "2020-12-04T00:10:03.604Z"
},
...
}
This subscription retrieves and updates all ticker information.
Subscribe Parameters
Parameter | Description |
---|---|
symbol (optional) | The market symbol of the ticker to retrieve |
Order Book
import { Econobit } from 'econobit'
export const orderbook = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const { orderbook, onUpdate } = await econobit.subscribe.orderbook('BTC-BRL')
console.log(orderbook)
onUpdate(update => {
console.log('orderbook update:', update)
const { side, price, amount } = update
if (amount > 0) {
orderbook[side][price] = amount
orderbook[side] = Object.entries(orderbook[side])
.sort((a, b) => side === 'asks' ? +a[0] - +b[0] : +b[0] - +a[0])
.reduce((r: { [price: string]: number }, o) => { r[o[0]] = o[1]; return r }, {})
} else {
delete orderbook[side][price]
}
console.log('updated orderbook:', orderbook)
})
} catch (err) {
console.error('ERROR:', err)
}
}
orderbook()
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const orderbook = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
ws.send(JSON.stringify({ type: 'Subscribe.Orderbook', symbol: 'BTC-BRL' }))
emitter.once('Orderbook.Load', (action) => console.log(action.orderbook))
emitter.on('Orderbook.Update', (action) => console.log('orderbook update:', action.order))
emitter.on('Orderbook.Add', (action) => console.log('orderbook add:', action.order))
emitter.on('Orderbook.Delete', (action) => console.log('orderbook delete:', action.order))
} catch (err) {
console.error('ERROR:', err)
}
})
}
orderbook()
The above command returns JSON structured like this:
{
"timestamp": "2021-03-03T14:44:42.469Z",
"bids": {
"255434.00": 0.0034,
"250000.00": 0.001,
"243567.00": 0.0012,
"240000.00": 0.001,
"233343.00": 0.0031,
"230000.00": 0.001,
"225434.00": 0.003,
"220000.00": 0.001,
"210000.00": 0.001,
"200000.00": 0.001
},
"asks": {
"310000.00": 0.001,
"320000.00": 0.002,
"340000.00": 0.002,
"355435.00": 0.0033,
"363255.00": 0.0012,
"370500.00": 0.001,
"373555.00": 0.001,
"380540.00": 0.0012
},
"symbol": "BTC-BRL"
}
{
bids: [
{
orderId: 168,
market: 'BTC-BRL',
userId: 201,
side: 'buy',
type: 'limit',
price: 301000,
amount: 0.0001,
filled: 0,
date: '2021-03-31T00:31:19.432Z'
},
...
],
asks: [
{
orderId: 169,
market: 'BTC-BRL',
userId: 198,
side: 'sell',
type: 'limit',
price: 314000,
amount: 0.0002,
filled: 0,
date: '2021-03-31T00:32:11.037Z'
},
...
]
}
This subscription retrieves and updates all orderbook information from a requested market.
Subscribe Parameters
Parameter | Description |
---|---|
symbol (required) | The market symbol of the orderbook to retrieve |
Trade History
import { Econobit } from 'econobit'
export const lastTrades = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const { list, onAdd } = await econobit.subscribe.lastTrades('BTC-BRL')
console.log(list)
onAdd(trade => {
console.log('lastTrades add:', trade)
})
} catch (err) {
console.error('ERROR:', err)
}
}
lastTrades()
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const lastTrades = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
const symbol = 'BTC-BRL'
ws.send(JSON.stringify({ type: 'Subscribe.LastTrades', symbol }))
emitter.once('LastTrades.Load', (action) => console.log(action.lastTrades.list[symbol]))
emitter.on('LastTrades.Add', (action) => console.log('lastTrades add:', action.trade))
} catch (err) {
console.error('ERROR:', err)
}
})
}
lastTrades()
The above command returns JSON structured like this:
[
{
"tradeId": 76,
"side": "buy",
"amount": 0.0001,
"price": 315000,
"market": "BTC-BRL",
"date": "2021-03-28T01:41:17.751Z"
},
{
"tradeId": 75,
"side": "buy",
"amount": 0.0001,
"price": 315000,
"market": "BTC-BRL",
"date": "2021-03-28T01:35:30.913Z"
},
...
]
This subscription retrieves and updates all trade history information from a requested market.
Subscribe Parameters
Parameter | Description |
---|---|
symbol (required) | The market symbol of the trade history to retrieve |
Private WebSocket
Balances
import { Econobit } from 'econobit'
export const balances = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { list, onUpdate } = await econobit.subscribe.balances()
console.log(list)
onUpdate(update => console.log('balance update:', update))
} catch (err) {
console.error('ERROR:', err)
}
}
balances()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const balances = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.Balances' }))
emitter.once('Wallet.List', (action) => console.log(action.wallets.list))
emitter.on('Wallet.Update', (action) => console.log('balance update:', action.wallet))
} catch (err) {
console.error('ERROR:', err)
}
})
}
balances()
The above command returns JSON structured like this:
{
"BTC": {
"amount": 0.98232651,
"inOrders": 0.001,
"coin": "BTC",
"name": "Bitcoin",
"fiat": false,
"address": "2MzEp1rfRz5DAU21DK6yZ31JQvb1pDszo9W"
},
"BRL": {
"amount": 2449.69,
"inOrders": 0,
"coin": "BRL",
"name": "Real Brasileiro",
"fiat": true,
"address": ""
},
...
}
This subscription retrieves and updates all wallet balances information.
Open Orders
import { Econobit } from 'econobit'
export const openOrders = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { list, onAdd, onUpdate, onDelete } = await econobit.subscribe.openOrders()
console.log(list)
onAdd(order => console.log('openOrders add:', order))
onUpdate(order => console.log('openOrders update:', order))
onDelete(orderId => console.log('openOrders delete:', orderId))
} catch (err) {
console.error('ERROR:', err)
}
}
openOrders()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const openOrders = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.OpenOrders' }))
emitter.once('OpenOrders.Load', (action) => console.log(action.list))
emitter.on('OpenOrders.Add', (action) => console.log(new Date(), 'openOrders add:', action.order))
emitter.on('OpenOrders.Update', (action) => console.log(new Date(), 'openOrders update:', action.order))
emitter.on('OpenOrders.Delete', (action) => console.log(new Date(), 'openOrders delete:', action.orderId))
} catch (err) {
console.error('ERROR:', err)
}
})
}
openOrders()
The above command returns JSON structured like this:
[
{
"orderId": 19,
"market": "BTC-USDC",
"userId": 201,
"side": "buy",
"type": "limit",
"price": 50000,
"amount": 0.0002,
"filled": 0,
"date": "2021-03-01T23:27:22.875Z"
},
{
"orderId": 18,
"market": "BTC-USDC",
"userId": 201,
"side": "sell",
"type": "limit",
"price": 60000,
"amount": 0.001,
"filled": 0,
"date": "2021-03-01T23:26:47.388Z"
}
]
This subscription retrieves and updates all open orders information.
Send Order
import { Econobit } from 'econobit'
export const orderSend = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { onAdd } = await econobit.subscribe.openOrders()
onAdd(order => console.log(new Date(), 'order created:', order))
const success = await econobit.order.send({
type: 'limit',
amount: 0.0001,
side: 'buy',
market: 'BTC-BRL',
price: 200000
})
console.log(success)
} catch (err) {
console.error('ERROR:', err)
}
}
orderSend()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const orderSend = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.OpenOrders' }))
emitter.on('OpenOrders.Add', (action) => console.log(new Date(), 'order created:', action.order))
emitter.on('Message.Success', (action) => console.log(action))
ws.send(JSON.stringify({
type: 'Order.Insert',
order: {
type: 'limit',
amount: 0.0001,
side: 'buy',
market: 'BTC-BRL',
price: 200000
}
}))
} catch (err) {
console.error('ERROR:', err)
}
})
}
orderSend()
The above command returns JSON structured like this:
{
"type": "Message.Success",
"message": "order_submitted"
}
This subscription creates a specific order.
Subscribe Parameters
Parameter | Description |
---|---|
type (required) | string, example: 'limit' or 'market' |
amount (required) | number, example: 0.0001 |
side (required) | string, example: 'buy' or 'sell' |
market (required) | string, example: 'BTC-BRL' |
price (optional) | number, example: 200000.00 (required when type is 'limit') |
Cancel Order
import { Econobit } from 'econobit'
export const orderCancel = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { list, onDelete } = await econobit.subscribe.openOrders()
onDelete(orderId => console.log(new Date(), 'order canceled:', orderId))
if (!list[0]) {
console.warn('no orders to cancel')
return
}
const success = await econobit.order.cancel({
orderId: list[0].orderId
})
console.log(success)
} catch (err) {
console.error('ERROR:', err)
}
}
orderCancel()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const orderCancel = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.OpenOrders' }))
emitter.on('OpenOrders.Delete', (action) => console.log(new Date(), 'order canceled:', action.orderId))
emitter.on('Message.Success', (action) => console.log(action))
emitter.once('OpenOrders.Load', (action) => {
const { list } = action
if (!list[0]) {
console.warn('no orders to cancel')
return
}
ws.send(JSON.stringify({
type: 'Order.Delete',
order: {
orderId: list[0].orderId
}
}))
})
} catch (err) {
console.error('ERROR:', err)
}
})
}
orderCancel()
The above command returns JSON structured like this:
{
"type": "Message.Success",
"message": "order_cancelled"
}
This subscription cancel a specific order.
Subscribe Parameters
Parameter | Description |
---|---|
orderId (required) | number, example: 394 |
Orders History
import { Econobit } from 'econobit'
export const ordersHistory = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { list, onAdd } = await econobit.subscribe.ordersHistory()
console.log(list)
onAdd(order => console.log('ordersHistory add:', order))
} catch (err) {
console.error('ERROR:', err)
}
}
ordersHistory()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const ordersHistory = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.OrdersHistory' }))
emitter.once('OrdersHistory.Load', (action) => console.log(action.list))
emitter.on('OrdersHistory.Add', (action) => console.log(new Date(), 'ordersHistory add:', action.order))
} catch (err) {
console.error('ERROR:', err)
}
})
}
ordersHistory()
The above command returns JSON structured like this:
[
{
"orderId": 195,
"market": "BTC-BRL",
"userId": 201,
"side": "buy",
"type": "limit",
"price": 310000,
"amount": 0.0001,
"filled": 0,
"date": "2021-04-04T19:13:43.085Z"
},
{
"orderId": 194,
"market": "BTC-BRL",
"userId": 201,
"side": "buy",
"type": "limit",
"price": 310000,
"amount": 0.0005,
"filled": 0,
"date": "2021-04-04T19:12:28.556Z"
},
...
]
This subscription retrieves and updates all orders history information.
Deposits
import { Econobit } from 'econobit'
export const deposits = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { list, onAdd, onUpdate } = await econobit.subscribe.deposits()
console.log(list)
onAdd(deposit => console.log('deposits add:', deposit))
onUpdate(deposit => console.log('deposits update:', deposit))
} catch (err) {
console.error('ERROR:', err)
}
}
deposits()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const deposits = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.Deposits' }))
emitter.once('Deposit.List', (action) => console.log(action.deposits.list))
emitter.on('Deposit.Add', (action) => console.log(new Date(), 'deposits add:', action.deposit))
emitter.on('Deposit.Update', (action) => console.log(new Date(), 'deposits update:', action.deposit))
} catch (err) {
console.error('ERROR:', err)
}
})
}
deposits()
The above command returns JSON structured like this:
[
{
"id": 772,
"date": "2021-03-12T23:26:21.000Z",
"coin": "BTC",
"amount": 0.09975,
"status": "Confirmed",
"record": "51bb9c7ce942bdd9f9d304e6030755fff7d572527049374747e30ef2c6cf1057"
},
{
"id": 724,
"date": "2020-12-03T23:55:32.000Z",
"coin": "BTC",
"amount": 1,
"status": "Confirmed",
"record": "2334081edfd3d804191c7f6d9fa06fa50df14d6b9588b315d396df04c1ada23c"
},
...
]
This subscription retrieves and updates all deposits information.
Withdrawals
import { Econobit } from 'econobit'
export const withdrawals = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { list, onAdd, onUpdate } = await econobit.subscribe.withdrawals()
console.log(list)
onAdd(withdraw => console.log('withdrawals add:', withdraw))
onUpdate(withdraw => console.log('withdrawals update:', withdraw))
} catch (err) {
console.error('ERROR:', err)
}
}
withdrawals()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const withdrawals = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.Withdrawals' }))
emitter.once('Withdraw.List', (action) => console.log(action.withdrawals.list))
emitter.on('Withdraw.Add', (action) => console.log(new Date(), 'withdrawals add:', action.withdraw))
emitter.on('Withdraw.Update', (action) => console.log(new Date(), 'withdrawals update:', action.withdraw))
} catch (err) {
console.error('ERROR:', err)
}
})
}
withdrawals()
The above command returns JSON structured like this:
[
{
"id": 61,
"date": "2021-04-18T00:18:33.489Z",
"coin": "BRL",
"amount": 500,
"fee": 15.55,
"status": "pending",
"address": "",
"txid": "",
"auth": ""
},
{
"id": 52,
"date": "2021-03-12T23:26:19.643Z",
"coin": "BTC",
"amount": 0.1,
"fee": 0.00025,
"status": "sent",
"address": "2MzEp1rfRz5DAU21DK6yZ31JQvb1pDszo9W",
"txid": "51bb9c7ce942bdd9f9d304e6030755fff7d572527049374747e30ef2c6cf1057",
"auth": ""
},
...
]
This subscription retrieves and updates all withdrawals information.
Trades
import { Econobit } from 'econobit'
export const trades = async () => {
try {
const econobit = new Econobit({ testnet: true })
await econobit.connect()
const key = 'API_KEY'
const secret = 'API_SECRET'
await econobit.login({ key, secret })
const { list, onAdd } = await econobit.subscribe.trades()
console.log(list)
onAdd(trade => console.log('trades add:', trade))
} catch (err) {
console.error('ERROR:', err)
}
}
trades()
const crypto = require('crypto')
const WebSocket = require('ws')
const EventEmitter = require('events')
const url = 'wss://testnet.econobit.io/ws'
const key = 'API_KEY'
const secret = 'API_SECRET'
const emitter = new EventEmitter()
const onMessage = (msg) => {
const data = JSON.parse(msg)
const actions = Array.isArray(data) ? data : [data]
for (const action of actions) {
emitter.emit(action.type, action)
}
}
const makeApiLogin = ({ key, secret }) => {
const nonce = Date.now()
const signature = crypto.createHash('sha256')
.update(secret).update(nonce.toString()).digest('base64')
return { key, nonce, signature }
}
const onLoginResponse = async (ws) => {
return new Promise((resolve, reject) => {
emitter.once('ApiKey.Success', () => resolve())
emitter.once('ApiKey.Fail', () => reject())
})
}
const login = async (ws, apiSign) => {
const apiLogin = makeApiLogin(apiSign)
ws.send(JSON.stringify({ type: 'ApiKey.Login', apiLogin }))
await onLoginResponse(ws)
}
const trades = async () => {
const ws = new WebSocket(url)
ws.on('error', console.error)
ws.on('message', onMessage)
ws.on('open', async () => {
try {
await login(ws, { key, secret })
ws.send(JSON.stringify({ type: 'Subscribe.Trades' }))
emitter.once('Trades.List', (action) => console.log(action.trades))
emitter.on('Trades.Add', (action) => console.log(new Date(), 'trades add:', action.trade))
} catch (err) {
console.error('ERROR:', err)
}
})
}
trades()
The above command returns JSON structured like this:
[
{
"tradeId": 94,
"market": "BTC-BRL",
"buyerUserId": 201,
"sellerUserId": 201,
"buyerOrderId": "175",
"sellerOrderId": "190",
"side": "sell",
"type": "market",
"amount": 0.0001,
"price": 310000,
"buyerFee": 0,
"sellerFee": 0.08,
"date": "2021-04-04T18:58:24.831Z"
},
{
"tradeId": 93,
"market": "BTC-BRL",
"buyerUserId": 201,
"sellerUserId": 201,
"buyerOrderId": "182",
"sellerOrderId": "189",
"side": "sell",
"type": "market",
"amount": 0.0001,
"price": 315000,
"buyerFee": 0,
"sellerFee": 0.08,
"date": "2021-04-04T18:56:26.971Z"
},
...
]
This subscription retrieves and updates all trades information.
Errors
The Econobit API uses the following error codes:
Errors returns JSON structured like this:
{
"code": 404,
"msg" : "Not found"
}
Error Code | Meaning |
---|---|
400 | Bad Request -- Your request is invalid. |
401 | Unauthorized -- Your API key is wrong. |
403 | Forbidden -- The info requested is hidden for administrators only. |
404 | Not Found -- The specified info could not be found. |
405 | Method Not Allowed -- You tried to access an info with an invalid method. |
406 | Not Acceptable -- You requested a format that isn't json. |
410 | Gone -- The info requested has been removed from our servers. |
418 | I'm a teapot. |
429 | Too Many Requests -- You're requesting too many info! Slow down! |
500 | Internal Server Error -- We had a problem with our server. Try again later. |
503 | Service Unavailable -- We're temporarily offline for maintenance. Please try again later. |