Jacob Tepperman
Jacob Tepperman

Reputation: 71

Coinbase web API through Node fetch "invalid API key" (not Coinbase pro)

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

Answers (1)

Aarav
Aarav

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
    I couldn't find info about the nonce. I found this over here.

Update:
signature looks like it is not made properly:

  • The nonce+'POST'+'/v2/accounts' is supposed to be the value in my_hmac.update
  • In turn for the createHmac it is only supposed to be SHA256 and mysecret
  • The signature pre-hash value is supposed to have a / at the beginning
  • A useful reference is here (be sure to click node.js at the top).

Upvotes: 1

Related Questions