-4

I need to build a code to extract data from Binance using API (not using Python library).

For some requests its not required to apply API key. For instance:

import requests

market = 'ETHUSDT'
tick_interval = '1m'
startTime = 1513375200000
endTime = 1656489800000
limit = 1000

url = "https://api.binance.com/api/v3/klines"

params = {
    'symbol': market,
    'interval': tick_interval,
    "startTime" : startTime,
    "endTime" : endTime,
    "limit" : limit
}

data = requests.get(url, params = params).json()

But also I want to use another endpoint to get all the trades of the account with symbol provided.

GET /api/v3/myTrades (HMAC SHA256) https://binance-docs.github.io/apidocs/spot/en/#account-information-user_data

But I dont understand how to pass API key, what would be the url with API/secret in order to get this info? I would much appreciate any help in that.

Olga
  • 5
  • 3

1 Answers1

-1

But I dont understand how to pass API key

This is documented in the Binance docs you've linked, albeit under the General info§Endpoint security type section.

  • API-keys are passed into the Rest API via the X-MBX-APIKEY header.

Add headers by passing them into your call to requests.get() with the guidance from the associated documentation and this Stack Overflow thread:

headers = { 'X-MBX-APIKEY': MY_API_KEY }
requests.get(url, headers=headers)

The SIGNED (TRADE, USER_DATA, AND MARGIN) Endpoint security section also contains information pertinent to your specific use case (considering the API method you've indicated you wish to use has the HMAC SHA256 indicator attached to it:

  • SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body.
  • Endpoints use HMAC SHA256 signatures. The HMAC SHA256 signature is a keyed HMAC SHA256 operation. Use your secretKey as the key and totalParams as the value for the HMAC operation.
  • The signature is not case sensitive.
  • totalParams is defined as the query string concatenated with the request body.

Timing security

  • A SIGNED endpoint also requires a parameter, timestamp, to be sent which should be the millisecond timestamp of when the request was created and sent.
  • An additional parameter, recvWindow, may be sent to specify the number of milliseconds after timestamp the request is valid for. If recvWindow is not sent, it defaults to 5000.

Once you've got your request almost ready to go, you'll need to do as this section in the docs mentions and concatenate the query string and the request body, hash it with your private key, then tack it on to the parameters with the key signature. Doing this in Python is relatively straightforward and detailed in this Stack Overflow thread:

import hmac
import hashlib
import urllib

API_SECRET = 'some_secret_key'

params = {
    'symbol': market,
    'interval': tick_interval,
    "startTime" : startTime,
    "endTime" : endTime,
    "limit" : limit
}

body = {
    'whatever': 'your',
    'request': 'has'
}

params['signature'] = hmac.new(bytes(API_SECRET , 'utf-8'), msg = bytes(urllib.parse.encode(params) + urllib.parse.encode(body), 'utf-8'), digestmod = hashlib.sha256).hexdigest().upper()

# make the request using params and body variables as you normally would in your request.get() invocation
esqew
  • 42,425
  • 27
  • 92
  • 132