Search code examples
coinbase-api

Coinbase body format


My goal is to send a Post request by using Coinbase API.

In the documentation (coinbase), it is specified that the body of the request should be added to the prehash string for message signature.

I am wondering what is the format of this body that I have to sent. Possible way I think I could do that :

  • concatenation of only the values value1+value2+value3
  • key=value seperated with &
  • other way ?

Solution

  • The body should just be a stringified JSON added to the timestamp, method, and path, respectively. Here's the example from the docs:

    var crypto = require('crypto');
    
    var secret = 'PYPd1Hv4J6/7x...';
    
    var timestamp = Date.now() / 1000;
    var requestPath = '/orders';
    
    var body = JSON.stringify({
        price: '1.0',
        size: '1.0',
        side: 'buy',
        product_id: 'BTC-USD'
    });
    
    var method = 'POST';
    
    // create the prehash string by concatenating required parts
    var what = timestamp + method + requestPath + body;
    
    // decode the base64 secret
    var key = Buffer(secret, 'base64');
    
    // create a sha256 hmac with the secret
    var hmac = crypto.createHmac('sha256', key);
    
    // sign the require message with the hmac
    // and finally base64 encode the result
    return hmac.update(what).digest('base64');