Skip to content

Instantly share code, notes, and snippets.

@mardlin
Created November 8, 2015 03:43
Show Gist options
  • Save mardlin/090c5b4d3986c1990363 to your computer and use it in GitHub Desktop.
Save mardlin/090c5b4d3986c1990363 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// npm install crypto
var crypto = require('crypto');
// npm install request
var request = require('request');
// Set these in your ENVironment, or replace them with the actual string
// sandbox:
// var apiKey = '';
// var apiSecret = '';
// var baseUrl = 'https://api.sandbox.coinbase.com/';
// production:
var apiKey = '';
var apiSecret = '';
var baseUrl = 'https://api.coinbase.com/';
//get unix time in seconds
var timestamp = Math.floor(Date.now() / 1000);
// Make a deposit
var req = {
method: 'POST',
path: '/v2/accounts/ACCOUNT_ID/deposits',
body: JSON.stringify({
amount: '11',
currency: 'USD',
payment_method: 'PAYMENT_METHOD_ID',
commit: false
})
};
var message = timestamp + req.method + req.path + req.body;
console.log(message);
//create a hexedecimal encoded SHA256 signature of the message
var signature = crypto.createHmac("sha256", apiSecret).update(message);
var hexSig = signature.digest("hex");
console.log(hexSig);
//create the request options object
var options = {
baseUrl: baseUrl,
url: req.path,
method: req.method,
body: req.body,
headers: {
'CB-ACCESS-SIGN': hexSig,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': apiKey,
'CB-VERSION': '2015-07-22'
}
};
request(options,function(err, response, request){
if (err) console.log(err);
var jsonString = response.body;
var jsonPretty = JSON.stringify(JSON.parse(jsonString),null,2);
console.log(jsonPretty);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment