user8277316
user8277316

Reputation:

Binance REST API Timestamp for this request is outside of the recvWindow

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

Upvotes: 9

Views: 19273

Answers (1)

Alfredo Casanova
Alfredo Casanova

Reputation: 134

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

Upvotes: 11

Related Questions