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