Binance API Keys

Crunk_Cat picture Crunk_Cat · Dec 27, 2017 · Viewed 21.9k times · Source

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 ?

Answer

toert picture toert · May 17, 2018

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 https://api.binance.com/api/v1/userDataStream
  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://stream.binance.com:9443/ws/{your listen key here}

Python example

import ssl
from websocket import create_connection

import requests

KEY = 'your-secret-key'
url = 'https://api.binance.com/api/v1/userDataStream'
listen_key = requests.post(url, headers={'X-MBX-APIKEY': KEY})['listenKey']
connection = create_connection('wss://stream.binance.com:9443/ws/{}'.format(KEY), 
                               sslopt={'cert_reqs': ssl.CERT_NONE})