9

I started testing out the Binance REST API recently but have been unable to get any account info. I consistently receive the same error Code -1021 Timestamp for this request is outside of the recvWindow. Not sure what the issue could be. I have triple checked that my system is synced with NTP and it matches that of other systems. I have tried increasing the default recvWindow to 50 seconds instead of the default 5 seconds but no luck. I should receive a JSON response as shown in the API documentation. I pasted my bash script below.

https://www.binance.com/restapipub.html

Updated Working Code

#!/bin/bash

APIKEY="<PUBLIC-KEY-HERE>"
APISECRET="<SECRET-KEY-HERE>"
RECVWINDOW=5000 # 5 seconds    

RECVWINDOW='recvWindow='$RECVWINDOW
TIMESTAMP=$(( $(date +%s) *1000))
TIMESTAMP='timestamp='$TIMESTAMP

QUERYSTRING=$RECVWINDOW"&"$TIMESTAMP

SIGNATURE=`echo -n $QUERYSTRING | openssl dgst -sha256 -hmac $APISECRET`
SIGNATURE=`echo $SIGNATURE|cut -c 10-`
SIGNATURE='signature='$SIGNATURE


curl -H "X-MBX-APIKEY: $APIKEY" -X GET 'https://api.binance.com/api/v3/account?'$RECVWINDOW'&'$TIMESTAMP'&'$SIGNATURE

echo
echo RecvWindow:$RECVWINDOW
echo Timestamp:$TIMESTAMP
echo Signature:$SIGNATURE
echo
6
  • It seems likely the API, like any well considered API, would use UTC dates. Does your system?
    – erik258
    Jan 23, 2018 at 23:35
  • System is in PST timezone. That should not matter because $(date +%s) is used which gets the time in milliseconds. The output matches the below: epochconverter.com/clock
    – user8277316
    Jan 24, 2018 at 1:41
  • Good point, %s is always in UTC apparently. I didn't realize that. However, it's seconds, not milliseconds. Does the API take milliseconds?
    – erik258
    Jan 24, 2018 at 14:38
  • Also had to disable "SIGNATURE=echo $SIGNATURE|cut -c 10-" line in your code to make it work
    – Mukul Jain
    Nov 13, 2020 at 8:30
  • the accepted answer is confusing please have a look at the discussion: github.com/ccxt/ccxt/issues/936
    – alper
    Jun 27, 2021 at 16:50

1 Answer 1

11

You'll need to multiply date +%s times 1000 so you get miliseconds, because:

man date
%s     seconds since 1970-01-01 00:00:00 UTC

and that's seconds.

So I took liberty to edit your code:

#!/bin/bash
read APIKEY APISECRET <<< $(cat apibinance | cut -f2 -d " "); 
RECVWINDOW=50000
RECVWINDOW="recvWindow=$RECVWINDOW"
TIMESTAMP="timestamp=$(( $(date +%s) *1000))"
QUERYSTRING="$RECVWINDOW&$TIMESTAMP"

SIGNATURE=$(echo -n "$QUERYSTRING" | openssl dgst -sha256 -hmac $APISECRET | cut -c 10-)
SIGNATURE="signature=$SIGNATURE"


curl -s -H "X-MBX-APIKEY: $APIKEY" "https://api.binance.com/api/v3/account?$RECVWINDOW&$TIMESTAMP&$SIGNATURE" | jq '.'
echo

consider that "apibinance" is a file and contains:

cat apibinance

key yourkey
sec yoursecret
1
  • Nice script improvements. For the config file though, what about something that doesn't depend on data's "position" in the file? declare -A conf; while read -r k v; do conf["$k"]="$v"; done < <(grep '^[^#]' apibinance.conf) .. then you can use the $conf[key] and $conf[sec]
    – ghoti
    Jan 25, 2018 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.