Binance API Keys

匿名 (未验证) 提交于 2018-07-07 21:15:41


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:**key**&signature=**s-key**

The documentation for Binance API can be found here: What am I doing wrong ?


You should set the API key in the request header, not as a parameter in the request url. Please provide more information on your request procedure (language, etc.).


X-MBX-APIKEY should be set as a field in the HTTP header, and not as a HTTP parameter. See this page for more information on HTTP header fields. However, I tried the same with Excel and could not get it running until now.

Another open question is how to use the secret key.


This worked for me:




queryString="timestamp=$(date +%s)" #$(python3 must sync

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


Binance's websocket API kinda tricky to use. Also there is no way to use a secret key.

Common usage

  1. Send HTTP POST request with your secret API key as a X-MBX-APIKEY header to
  2. You will get listen key which should be used for websocket connection. It will be available 1 hour.

    {"listenKey": "your listen key here"}

  3. Use it when connecting to Binance's websocket

wss://{your listen key here}

Python example

import ssl
from websocket import create_connection

import requests

KEY = 'your-secret-key'
url = ''
listen_key =, headers={'X-MBX-APIKEY': KEY})['listenKey']
connection = create_connection('wss://{}'.format(KEY), 
                               sslopt={'cert_reqs': ssl.CERT_NONE})


Using this Binance Exchange API python implementation : can simplify websocket development.

from binance.client import Client
client = Client(api_key, api_secret)

# start aggregated trade websocket for BNBBTC
def process_message(msg):
    print("message type: {}".format(msg['e']))
    # do something

from binance.websockets import BinanceSocketManager
bm = BinanceSocketManager(client)
bm.start_aggtrade_socket('EOSBTC', process_message)


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 = "{$query}&signature={$signature}";

$res = json_decode(file_get_contents($endpoint, false, $context), true);

文章来源: Binance API Keys