1. Prepare cryptocurrency exchanges account
In this post, I’d like to share a sample of trying to get your account information from the Cryptocurrency Exchange account via their API.
This time, I tried at following two Japanese exchanges. bitFlyer is starting their service in United States. (I’m gonna share the info about other global exchanges some day, such as Poloniex, Bitfinex, Liqui, etc.)
・bitFlyer
・coincheck
2. API Documents
Please refer these API documents.
・bitFlyer Lightning API Document (bitFlyer Lightning API Playground)
・coincheck API Document
3. Build development environment on Cloud9 easily
Cloud9 is one of the cloud services for developers. Even if you don’t have your own server or local environment on PC, you can develop and deploy your web application with only web browser! You can choose many environments, Node.js, Python(Django), Ruby, PHP, and so on. And also, you are able to use CLI(Command Line Interface, so it’s available to install additional libraries on your demand. (Although you need to input your Credit/Debit card information, it’s to avoid spams, maybe. Basically, it’s free to normal use :))
First of all, register for Cloud9, create a new workspace.
This time, we will try Node.js(JavaScript).
If you finish to make your workspace for Node.js, delete all resources(files and folders) in the project. To do it, select all resources and show the context menu with right-button-click, choose “Delete” from “File” menu on Tool bar.
After that, run following command in your shell(Terminal). “npm init”.
Then, you can see package.json file. And next, run “npm install –save request crypto”.
“–save” enable to add dependencies information into package.json and install the necessary libraries into “node_modules”.
Next, edit package.json like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "name": "workspace", "version": "1.0.0", "description": "", "main": "main.js", "dependencies": { "crypto": "^1.0.1", "request": "^2.81.0" }, "devDependencies": {}, "scripts": { "start": "node main.js" }, "author": "", "license": "ISC" } |
And, make a new file named “main.js” in the project’s root. (“File” -> “New File”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
var request = require('request'); var crypto = require('crypto'); var key = process.env.bfkey; var secret = process.env.bfsecret; var timestamp = Date.now().toString(); var method = 'GET'; var path = '/v1/me/getbalance'; var text = timestamp + method + path; var sign = crypto.createHmac('sha256', secret).update(text).digest('hex'); var options = { url: 'https://api.bitflyer.jp' + path, method: method, headers: { 'ACCESS-KEY': key, 'ACCESS-TIMESTAMP': timestamp, 'ACCESS-SIGN': sign } }; request(options, function (err, response, payload) { console.log(payload); }); |
Now you are done for checking your account balance! But, did you notice the variables named “key” and “secret”? You need these information.
Public API & Private API
The APIs which don’t need authentication to call information such as market board are called “Public API”, while ones which need to authenticate are called “Private API”.
And this time, we’re using Private API.
For that, you need your own “API KEY” and “API SECRET”.
To get those, if you’re using bitFlyer, you can get from “bitFlyer Lightning” page.
Select the hamburger icon (Ξ), open “API” from menu.
And, you would be able to see like following page, then grant the permission you need. Then, keep those key and secret.
Important!! DO NOT show those info to other people!! Keep those secret!!
Keep Going!
After that, set those parameters so that you can use process.env.bfkey, and process.env.bfsecret in main.js. For that, run export command.
Kept you waiting, huh? Let’s run!!
You have a command in package.json to run, you just need run “npm start”.
Oh, wow! 80!! yen…
How pity, but you can get your trade balance!
Let’s try out coincheck too!
Alright, let’s move on to the next… How about take a look your withdrawals history.
The code is bit different, it’s similar to bitFlyer’s.
Request headers are different from bitFler’s.
ACCESS-SIGNATURE is concatenated sringified ACCESS-NONCE URL Request’s body, and signed it with HMAC-SHA256 Hash form by API Key.
I show you the code to get withdrawal history here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
var request = require('request'); var crypto = require('crypto'); var key = process.env.cckey; var secret = process.env.ccsecret; var timestamp = Date.now().toString(); var method = 'GET'; var path = 'https://coincheck.com/api/withdraws'; var text = timestamp + path; var sign = crypto.createHmac('sha256', secret).update(text).digest('hex'); var options = { url: 'https://coincheck.com/api/withdraws', method: method, headers: { "ACCESS-KEY": key, "ACCESS-NONCE": timestamp, "ACCESS-SIGNATURE": sign } }; request(options, function (err, response, payload) { console.log(payload); }); |
And, result is…
Like this!
That’s all! We tried to get our own information with cryptocurrency exchanges’ Private APIs. And now, you might get some idea such as system trade(Algorithmic Trade)!
For more details about JavaScript(Node.js), trading, refer to some books and Udemy courses.
App Development course on Udemy
I noticed that I have registered 75 courses on Udemy… Wow.
My Recommendation is “React and Redux”, “Data Analytics”, “Deep Learning”, “Ethereum”, etc…
I’m planning to write an article about Getting board info and show it on your web browser!
Please stay tuned!!
Cheers,
Leave your comment.