[NEED HELP] Create A New Order With Binance Futures API

Hi there,

I’m new to Binance API, I want to create a new order on Binance Futures but I get an error like this:

Here is what I tried to do (Actually, I followed this example: https://github.com/binance-exchange/binance-signature-examples/blob/master/python/futures.py):

import hmac
import time
import hashlib
import requests
import json
from urllib.parse import urlencode

KEY = API_KEY
SECRET = API_SECRET
BASE_URL = 'https://fapi.binance.com' # production base url

def hashing(query_string):
    return hmac.new(SECRET.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()

def get_timestamp():
    return int(time.time() * 1000)

def dispatch_request(http_method):
    session = requests.Session()
    session.headers.update({
        'Content-Type': 'application/json;charset=utf-8',
        'X-MBX-APIKEY': KEY
    })
    return {
        'GET': session.get,
        'DELETE': session.delete,
        'PUT': session.put,
        'POST': session.post,
    }.get(http_method, 'GET')

# used for sending request requires the signature
def send_signed_request(http_method, url_path, payload={}):
    query_string = urlencode(payload)
    # replace single quote to double quote
    query_string = query_string.replace('%27', '%22')
    if query_string:
        query_string = "{}&timestamp={}".format(query_string, get_timestamp())
    else:
        query_string = 'timestamp={}'.format(get_timestamp())

    url = BASE_URL + url_path + '?' + query_string + '&signature=' + hashing(query_string)
    print("{} {}".format(http_method, url))
    params = {'url': url, 'params': {}}
    response = dispatch_request(http_method)(**params)
    return response.json()

# used for sending public data request
def send_public_request(url_path, payload={}):
    query_string = urlencode(payload, True)
    url = BASE_URL + url_path
    if query_string:
        url = url + '?' + query_string
    print("{}".format(url))
    response = dispatch_request('GET')(url=url)
    return response.json()

### public data endpoint, call send_public_request #####
# get klines
response = send_public_request('/fapi/v1/klines' , {"symbol": "BTCUSDT", "interval": "1d"})
print(response)

# get account informtion
# if you can see the account details, then the API key/secret is correct
response = send_signed_request('GET', '/fapi/v2/account')
print(response)

### USER_DATA endpoints, call send_signed_request #####
# place an order
# if you see order response, then the parameters setting is correct
# if it has response from server saying some parameter error, please adjust the parameters according the market.

params = {
    "symbol": "BNBUSDT",
    "side": "BUY",
    "type": "LIMIT",
    "timeInForce": "GTC",
    "quantity": 1,
    "price": "15"
}
response = send_signed_request('POST', '/fapi/v1/order', params)
print(response)

I tried to Google that error but still didn’t know why I encountered it and how to fix it. I’m sure that the API_KEY is accurate and valid. I also don’t restrict IP address.
I tested on testnet and it worked well

Hope you can give me some help. Thanks a lot

  1. Did you use the same key you tried on prod? If it works on testnet, it won’t work in prod.
  2. Have you enabled your futures account in prod?

Hi @MJW,

  1. I created another account on testnet, and of course I used a different key (which along with the testnet account) but with that code (changed API key, BASE_URL to testnet), then it worked well.
  2. I already enabled my futures account in prod:

Did you create your API key before you enabled your futures account? If yes, create a new one

Oh, thanks @MJW a lot, It turned out that I created my API before enabling my futures account as you pointed out. After creating a new one, it worked perfectly now.
Thank you so much