Reputation: 71
Just trying to get my Coinbase balance. I have tried making a bunch of different API keys, keep getting the same error:
{ "errors": [{ "id": "authentication_error", "message": "invalid api key" }] }
Im using Node.js through Netlify Lambda functions.
Here's my code:
import fetch from "node-fetch"
import crypto from "crypto"
const mykey = '<KEY>'
const mysecret = '<SECRET>'
exports.handler = async (event, context) => {
const url = `https://api.coinbase.com/v2/accounts`
var nonce = Math.floor(new Date().getTime() * 1e-3)
var my_hmac = crypto.createHmac('SHA256', nonce+'POST'+'v2/accounts', mysecret)
my_hmac.update(nonce + url)
var signature = my_hmac.digest('hex')
var msg;
return fetch(url, { headers:
{
'CB-ACCESS-KEY' : mykey,
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': nonce,
'Content-Type': 'application/json'
}
}).then(res => {
// console.log(res)
res.json
})
.then(data => {
return ({
statusCode: 200,
body: JSON.stringify(data)
})
})
}
Upvotes: 0
Views: 992
Reputation: 152
You are using the wrong names for the tokens.
ACCESS_KEY
is supposed to be CB-ACCESS-KEY
ACCESS_SIGNATURE
is supposed to be CB-ACCESS-SIGN
Update:
signature
looks like it is not made properly:
nonce+'POST'+'/v2/accounts'
is supposed to be the value in my_hmac.update
createHmac
it is only supposed to be SHA256
and mysecret
/
at the beginningUpvotes: 1