Package: blockfacts-sdk
BlockFacts Python SDKOfficial BlockFacts Python SDK including Rest and WebSocket API support.FeaturesREST API client with a function wrapper for easy API accessWebSocket API client for real-time data gatheringNote: In order to read more and get richer details regarding our REST and WebSocket APIs, please refer to our official docs:https://docs.blockfacts.io.InstallationQuick startUsing Rest API ClientAsset endpointsBlockFacts endpointsExchange endpointsUsing WebSocket API ClientInstallation$pipinstallblockfacts-sdkQuick startTo start using our SDK just import the package and pass theAPI-KEYandAPI-SECRETin the constructor.fromblockfactsimportRestClientfromblockfactsimportWebsocketClientkey='your-api-key'secret='your-api-secret'restClient=RestClient(key,secret)websocketClient=WebsocketClient(key,secret)Using Rest API ClientIn the examples below, you can see which method is mapped to call it's predefined endpoint. You can also read more about authorization and how to obtain an API Key here:https://docs.blockfacts.io/?python#authorizationAsset endpointsList all assetsGet all assets that we support.listAllAssets()jsonResponse=restClient.assets.listAllAssets()Get specific assetGet specific asset by ticker ID.getSpecificAsset(tickerId)jsonResponse=restClient.assets.getSpecificAsset("BTC")BlockFacts endpointsExchanges in normalizationList exchanges that go into the normalization for specific asset-denominator pair.getExchangesInNormalization(pairs)jsonResponse=restClient.blockfacts.getExchangesInNormalization(["BTC-USD","ETH-USD"])# ORjsonResponse=restClient.blockfacts.getExchangesInNormalization("BTC-USD, ETH-USD")Current dataGet current normalization data for specific asset-denominator pair.getCurrentData(assets, denominators)jsonResponse=restClient.blockfacts.getCurrentData(["BTC","ETH"],["USD","EUR"])# ORjsonResponse=restClient.blockfacts.getCurrentData("BTC, ETH","USD, EUR")Snapshot dataGet last 600 BlockFacts normalized prices for provided asset-denominator pairs.getSnapshotData(assets, denominators)jsonResponse=restClient.blockfacts.getSnapshotData(["BTC","ETH"],["USD","EUR"])# ORjsonResponse=restClient.blockfacts.getSnapshotData("BTC, ETH","USD, EUR")OHLCV Snapshot dataGets the snapshot of Blockfacts OHLCV data for provided asset-denominator pairs and intervals.getOHLCVSnapshotData(assets, denominators, intervals)jsonResponse=restClient.blockfacts.getOHLCVSnapshotData(["BTC","ETH"],["USD","EUR"],["30s","1m","1h"])# ORjsonResponse=restClient.blockfacts.getOHLCVSnapshotData("BTC, ETH","USD, EUR","30s, 1m, 1h")Note:You can find all supported intervals on our official documentation here:https://docs.blockfacts.io/?python#data-snapshot-ohlcv-blockfactsHistorical dataGet historical normalization data by asset-denominator, date, time and interval.getHistoricalData(asset, denominator, date, time, interval, page)jsonResponse=restClient.blockfacts.getHistoricalData("BTC","USD","2.9.2019","14:00:00",20)# OR with page parameter (optional)jsonResponse=restClient.blockfacts.getHistoricalData("BTC","USD","2.9.2019","14:00:00",20,3)OHLCV Historical dataGet historical OHLCV data by asset-denominator, date, time and interval.getHistoricalOHLCVData(asset, denominator, interval, dateStart, timeStart, dateEnd, timeEnd, page)jsonResponse=restClient.blockfacts.getHistoricalOHLCVData("BTC","USD","1d","5.8.2020","14:00:00","10.8.2020","14:00:00")# OR with page parameter (optional)jsonResponse=restClient.blockfacts.getHistoricalOHLCVData("BTC","USD","1d","5.8.2020","14:00:00","10.8.2020","14:00:00",2)Specific historical dataGet historical normalized price by specific point in time.getSpecificHistoricalData(asset, denominator, date, time)jsonResponse=restClient.blockfacts.getSpecificHistoricalData("BTC","USD","2.9.2019","14:00:00")Normalization pairsGet all running normalization pairs. Resulting in which asset-denominator pairs are currently being normalized inside our internal system.getNormalizationPairs()jsonResponse=restClient.blockfacts.getNormalizationPairs()Period moversGet the moving percentage, and difference in price over a certain time period.getPeriodMovers(denominator, date, interval, sort)jsonResponse=restClient.blockfacts.getPeriodMovers("USD","11.8.2020","sevenDay",-1)Exchange endpointsList all exchangesList all exchanges that we support.listAllExchanges()jsonResponse=restClient.exchanges.listAllExchanges()Specific exchange dataGet information about a specific exchange by its name. Returns information such as which assets are supported, asset ticker info, etc.getSpecificExchangeData(exchange)jsonResponse=restClient.exchanges.getSpecificExchangeData("KRAKEN")Current trade dataGet current trade data for specific asset-denominator pair, from specific exchange(s).getCurrentTradeData(assets, denominators, exchanges)jsonResponse=restClient.exchanges.getCurrentTradeData(["BTC","ETH"],["USD","GBP"],["KRAKEN","COINBASE"])# ORjsonResponse=restClient.exchanges.getCurrentTradeData("BTC, ETH","USD, GBP","KRAKEN, COINBASE")Pair infoGet the Blockfacts pair representation of the provided exchange pair.getPairInfo(exchange, pair)jsonResponse=restClient.exchanges.getPairInfo("BITSTAMP","BTCUSD")Snapshot trade dataGet 600 latest trades that happened on the requested exchange(s) and pairs.getSnapshotTradeData(assets, denominators, exchanges)jsonResponse=restClient.exchanges.getSnapshotTradeData(["BTC","ETH"],["USD","GBP"],["KRAKEN","COINBASE"])# ORjsonResponse=restClient.exchanges.getSnapshotTradeData("BTC, ETH","USD, GBP","KRAKEN, COINBASE")Snapshot OHLCV dataGets the snapshot of provided exchange(s) OHLCV data for provided asset-denominator pairs and intervals.getOHLCVSnapshotData(assets, denominators, exchanges, intervals)jsonResponse=restClient.exchanges.getOHLCVSnapshotData(["BTC","ETH"],["USD","GBP"],["KRAKEN","COINBASE"],["30s","1m","1h"])# ORjsonResponse=restClient.exchanges.getOHLCVSnapshotData("BTC, ETH","USD, GBP","KRAKEN, COINBASE","30s, 1m, 1h")Note:You can find all supported intervals on our official documentation here:https://docs.blockfacts.io/?python#data-snapshot-ohlcv-exchangeHistorical trade dataGet exchange historical price by asset-denominator, exchange, date, time and interval.getHistoricalTradeData(asset, denominator, exchanges, date, time, interval, page)jsonResponse=restClient.exchanges.getHistoricalTradeData("BTC","USD",["KRAKEN","COINBASE"],"2.9.2019","14:00:00",20)# OR with page parameter (optional)jsonResponse=restClient.exchanges.getHistoricalTradeData("BTC","USD","KRAKEN, COINBASE","2.9.2019","14:00:00",20,3)OHLCV Historical dataGet historical OHLCV data by asset-denominator, exchange, date, time and interval.getHistoricalOHLCVData(asset, denominator, exchanges, interval, dateStart, timeStart, dateEnd, timeEnd, page)jsonResponse=restClient.exchanges.getHistoricalOHLCVData("BTC","USD",["KRAKEN","COINBASE"],"1d","5.8.2020","14:00:00","10.8.2020","14:00:00")# OR with page parameter (optional)jsonResponse=restClient.exchanges.getHistoricalOHLCVData("BTC","USD","KRAKEN, COINBASE","1d","5.8.2020","14:00:00","10.8.2020","14:00:00",1)Specific trade dataGet historical exchange trades in specific second.getSpecificTradeData(asset, denominator, exchanges, date, time)jsonResponse=restClient.exchanges.getSpecificTradeData("BTC","USD",["KRAKEN","COINBASE"],"2.9.2019","14:00:00")# ORjsonResponse=restClient.exchanges.getSpecificTradeData("BTC","USD","KRAKEN, COINBASE","2.9.2019","14:00:00")Total trade volumeGet the total traded volume on all exchanges by asset-denominator and interval.getTotalTradeVolume(asset, denominator, interval)jsonResponse=restClient.exchanges.getTotalTradeVolume("BTC","USD","1d")Period moversGet the moving percentage, and difference in price over a certain time period.getPeriodMovers(exchange, denominator, date, interval, sort)jsonResponse=restClient.exchanges.getPeriodMovers("KRAKEN","USD","11.8.2020","sevenDay",-1)Using WebSocket API ClientOur WebSocket feed provides real-time market data streams from multiple exchanges at once and the BlockFacts normalized price stream for each second. The WebSocket feed uses a bidirectional protocol, and all messages sent and received via websockets are encoded in aJSONformat.Getting started and connectingTo get started simply create a new instance of the WebsocketClient class, and create handler functions for websocket events. You can then call theconnect()function and open a connection with the BlockFacts websocket server.fromblockfactsimportWebsocketClientimportjsonkey='your-api-key'secret='your-api-secret'defon_open():print("BlockFacts websocket server connection open")defon_message(message):data=json.loads(message)print(data)# Handle websocket server messagesdefon_close():print("BlockFacts websocket server connection closed")defon_error(err):print(err)websocketClient=WebsocketClient(key,secret)websocketClient.onOpen=on_openwebsocketClient.onMessage=on_messagewebsocketClient.onClose=on_closewebsocketClient.onError=on_errorwebsocketClient.connect()SubscribingIn order to subscribe to specific channels or asset-pairs you must send out asubscribetype message. The subscribe message must be sent out after the connection with the websocket has been established. You can call thesubscribe()function right after theconnect()function, or in theon_open()event handler and pass it a list of channels:websocketClient.connect()websocketClient.subscribe([{"name":"BLOCKFACTS","pairs":["BTC-USD"]},{"name":"HEARTBEAT"}])# ORdefon_open():print("BlockFacts websocket server connection open")websocketClient.subscribe([{"name":"BLOCKFACTS","pairs":["BTC-USD"]},{"name":"HEARTBEAT"}])Thesubscribetype message supports two more optional parameters which areidandsnapshot. You can pass those parameters after the listed channels dictionary in thesubscribe()function.websocketClient.subscribe([{"name":"BLOCKFACTS","pairs":["BTC-USD"]},{"name":"HEARTBEAT"}],"some-id",True)To read more about thesnapshottype message, please refer to our official documentation:https://docs.blockfacts.io/?python#snapshotUnsubscribingIf you wish to unsubscribe from certain channels or pairs, you can do so by sending theunsubscribetype message.websocketClient.unsubscribe([{"name":"BLOCKFACTS","pairs":["BTC-USD","ETH-USD"]},{"name":"HEARTBEAT"},{"name":"KRAKEN"}],12345)Theunsubscribetype message supports one more optional parameter which isid. You can pass this parameter after the listed channels dictionary in theunsubscribe()function.PingClients can sendpingtype messages to determine if the server is online.websocketClient.ping()Thepingtype message supports one optional parameter which isid. You can pass this parameter in theping()function.PongClients must respond topingtype messages sent from the server with apongtype message.defon_message(message):data=json.loads(message)if(data["type"]=="ping"):websocketClient.pong()Theon_message()event handler can also be used to handle all message types from the websocket, for example:defon_msg(message):data=json.loads(message)ifdata["type"]=='subscribed':# Subscribed type messageifdata["type"]=='snapshot':# Snapshot type messageifdata["type"]=='unsubscribed':# Unsubscribed type messageifdata["type"]=='exchangeTrade':# ExchangeTrade type messageifdata["type"]=='blockfactsPrice':# BlockfactsPrice type messageifdata["type"]=='ping':# Ping type messagewebsocketClient.pong()ifdata["type"]=='pong':# Pong type messageifdata["type"]=='status':# Status type messageifdata["type"]=='heartbeat':# Heartbeat type messageifdata["type"]=='error':# Error type messageDisconnectClients can disconnect from the BlockFacts websocket by calling thedisconnect()function. The disconnect function will work only if the connection has already been established.websocketClient.disconnect()In order to have a better understanding of our server responses, please refer to:https://docs.blockfacts.io/?python#server-messages