Postman GET request to Binance API

Viewed 9033

I'm trying to send a GET request to Binance's API, but I don't exactly know how to. Here is the documentation page: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data

I have a private apiKey and secretKey. I can do a general request to Binance, but I cannot get my private data, using my private keys.

First try: For the GET request in Postman I use this string: https://api.binance.com/api/v3/account?timestamp=1499827319559&signature=here_I_put_my_secret_key

And I pass as a header as Danny suggested the apiKey.

But I get:

    {
    "code": -1021,
    "msg": "Timestamp for this request is outside of the recvWindow."
    }

Thanks.

4 Answers

You can try this. This is working for me. Just Replace your API_KEY and SECRET

You need to retrieve serverTime time from https://api.binance.com/api/v3/time and need to use that serverTime to sign the request.

GET : https://api.binance.com/api/v3/account?timestamp={{timestamp}}&signature={{signature}}

Header:

Content-Type:application/json
X-MBX-APIKEY:YOUR_API_KEY

Pre-request Script :

pm.sendRequest('https://api.binance.com/api/v3/time', function (err, res) {
        console.log('Timestamp Response: '+res.json().serverTime);
        pm.expect(err).to.not.be.ok;
        var timestamp = res.json().serverTime;

        postman.setEnvironmentVariable('timestamp',timestamp)  
        postman.setGlobalVariable('timestamp',timestamp) 

        let paramsObject = {};

        const binance_api_secret = 'YOUR_API_SECRET';

        const parameters = pm.request.url.query;

        parameters.map((param) => {
            if (param.key != 'signature' && 
                param.key != 'timestamp' && 
                !is_empty(param.value) &&
                !is_disabled(param.disabled)) {
                    paramsObject[param.key] = param.value;
            }
        })
        
        Object.assign(paramsObject, {'timestamp': timestamp});

        if (binance_api_secret) {
            const queryString = Object.keys(paramsObject).map((key) => {
                return `${encodeURIComponent(key)}=${paramsObject[key]}`;
            }).join('&');
            console.log(queryString);
            const signature = CryptoJS.HmacSHA256(queryString, binance_api_secret).toString();
            pm.environment.set("signature", signature);
        }

        function is_disabled(str) {
            return str == true;
        }

        function is_empty(str) {
            if (typeof str == 'undefined' ||
                !str || 
                str.length === 0 || 
                str === "" ||
                !/[^\s]/.test(str) ||
                /^\s*$/.test(str) ||
                str.replace(/\s/g,"") === "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
); 

Get the official Postman collections for the Binance API from here:

https://github.com/binance/binance-api-postman

Import the desired collection and environment in Postman, for instance binance_spot_api_v1.postman_collection.json and binance_com_spot_api.postman_environment.json

Add your API key to the binance-api-key environment variable and your secret key to the binance-api-secret variable.

CAUTION: Limit what the key can do in Binance key management. Do not use this key for production, only for testing. Create new key for production.

For the signed requests calculate the signature in a Pre-request Script then set the signature environment variable.

Example Pre-request Script:

function resolveQueryString() {
  const query = JSON.parse(JSON.stringify(pm.request.url.query)) 
  const keyPairs = []
  for (param of query) {
    if (param.key === 'signature') continue
    if (param.disabled) continue
    if (param.value === null) continue
    const value = param.value.includes('{{') ? pm.environment.get(param.key) : param.value
    keyPairs.push(`${param.key}=${value}`)
  }
  return keyPairs.join('&')
}

const signature = CryptoJS.HmacSHA256(
  resolveQueryString(),
  pm.environment.get('binance-api-secret')
).toString(CryptoJS.enc.Hex)
pm.environment.set('signature', signature)
Related