Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binance API Keys

I have set up a read-only API key on Binance to access account information like currency balances but I can't see the JSON data. The string query I put into the URL returns the following error:

{"code":-2014,"msg":"API-key format invalid."}

The URL I am using is this: https://api.binance.com/api/v3/account?X-MBX-APIKEY=**key**&signature=**s-key**

The documentation for Binance API can be found here: https://www.binance.com/restapipub.html. What am I doing wrong ?

like image 358
Crunk_Cat Avatar asked Dec 26 '17 23:12

Crunk_Cat


People also ask

What is a Binance API Key?

An API Key can be considered as a username that is generating to allow access to data. In this example, the Binance API is generated by the exchange and you then pass it on to another application. The application will then import your data based on the permissions you allow for.

Is there an API for Binance?

Yes. Binance API is compatible with your favorite languages, such as Python, Java, Node. js, DotNET, Ruby and more.


2 Answers

This worked for me:

base_url="https://api.binance.com"
account_info="/api/v3/account"

url="${base_url}${account_info}"

apikey="your_apikey"
secret="your_secret"

queryString="timestamp=$(date +%s)" #$(python3 binance_time.py) must sync
requestBody=""

signature="$(echo -n "${queryString}${requestBody}" | openssl dgst -sha256 -hmac $secret)"
signature="$(echo $signature | cut -f2 -d" ")"

req=$(curl -H "X-MBX-APIKEY: $apikey" -X GET "$url?$queryString&signature=$signature")
echo $req
like image 171
Alksentrs Avatar answered Nov 15 '22 20:11

Alksentrs


You put it in the header. Following is tested working PHP example borrowed from jaggedsoft binance PHP library, it's a signed request that will return the account status.

$api_key = "cool_key";
$secret = "awesome_secret";

$opt = [
  "http" => [
    "method" => "GET",
    "header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$api_key}\r\n"
  ]
];
$context = stream_context_create($opt);
$params['timestamp'] = number_format(microtime(true)*1000,0,'.','');
$query = http_build_query($params, '', '&');
$signature = hash_hmac('sha256', $query, $secret);
$endpoint = "https://api.binance.com/wapi/v3/accountStatus.html?{$query}&signature={$signature}";

$res = json_decode(file_get_contents($endpoint, false, $context), true);
like image 30
Robert Sinclair Avatar answered Nov 15 '22 18:11

Robert Sinclair