@shurr sorry, didn’t mean to come off like that… I really am just dabbling in this for the very first time. I have built a couple of scripts before but nothing as complex as this… and the doc for the api is lacking big time.
that being said… the code I posted above is my entire code.
the variables:
var cb_access_passphrase = process.env.cb_access_passphrase;
var cb_api_key = process.env.cb_api_key;
var cb_secret = process.env.cb_secret;
are defined in a .env file
I can test and console.log() them just fine
timestamp is:
var cb_access_timestamp = Date.now() / 1000;
which returns something like this:
1649197824.804
then, I build the message like so:
var message = cb_access_timestamp + method + path + body;
where method is:
var method = 'POST';
path is:
var path = '/orders';
and body is:
var body = JSON.stringify({
"product_id":"BTC-USD",
"side":"buy",
"type":"limit",
"size":"0.0001",
"price":"44415.51"
});
then I use either the crypto pkg to build the signature or the CriptoJS
using crypto pkg:
var key = Buffer.from(cb_secret, 'base64');
var hmac = crypto.createHmac('sha256', key);
var sign = hmac.update(message).digest('base64');
using CryptoJS:
var key = CryptoJS.enc.Base64.parse(cb_secret);
var hash = CryptoJS.HmacSHA256(message, key);
var sign = hash.toString(CryptoJS.enc.Base64);
and then I pass that to the headers section:
const options = {
hostname: 'api.exchange.coinbase.com',
//hostname: 'api.pro.coinbase.com',
port: 443,
path: path,
method: method,
headers: {
'Content-Type': 'application/json',
'Content-Length': 0,
'User-Agent': 'MyTradingBot',
'CB-ACCESS-KEY': cb_api_key,
'CB-ACCESS-PASSPHRASE': cb_access_passphrase,
'CB-ACCESS-SIGN': sign,
'CB-ACCESS-TIMESTAMP': cb_access_timestamp
}
}
and then submit the request:
const req = https.request(options, res => {
//console.log(cb_access_timestamp);
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
I’m sorry if I misunderstood your question… but that’s all I have so far… no other functions/classes/anything.
If I console.log(message) something like this is returned:
1649198401.549POST/orders{"product_id":"BTC-USD","side":"buy","type":"limit","size":"0.0001","price":"44415.51"}
I was just trying to build my very first script for Coinbase and then build on that.
And… I believe I ate up all my posts for the day and don’t feel like hijacking other people’s threads.
I shall be back here tomorrow.
I appreciate all your help and time.