APIError(code=-2015): Invalid API-key, IP, or permissions for action from the python-binance module

Hi,

I am currently trying to automate my Tradingview script to trigger trades automatically on Binance. To do so, I am following a tutorial that is using the python-binance module. It was quite simple as a new Python programmer. However, when I started testing it using my API, I’ve come across error “code=-2015” that I cannot get rid of. I know there are many references on this page as well as on the internet, but I am still facing this issue. Also, I would like to understand that library more.

So, I went through the bullet points listed here.

My code is really simple, and the main parts are:

client = Client(config.API_KEY, config.API_SECRET)

and then

order = client.create_order(symbol=symbol, side=side, type=order_type, quantity=quantity)

at this point create_order fails with code=-2015.

After further investigations, it seems the issue is related to the fact that the python-binance module is using the SPOT url (https://api.binance.com/api) instead of the Futures one (https://fapi.binance.com/fapi).

Note: I am using Futures, and my API key is correct as well as its permissions because it works fine on MT4 without any issues, but I would like to move to Tradingview. The only API options checked are: Enable Reading, Enable Futures, Unrestricted (Less Secure).

So, my logical thinking suggests that python-binance is using SPOT url by default, but I don’t know where to change that. How do I force it to using Futures ? Where is that configured ? How does the python-binance module know which environment to use based on the API key ?

A little bit longer text than initially thought but my aim is to fix this issue and then understand a little more about the module itself. Some threads suggest changing binance_f/constant/system.py, but I don’t have such a file at all. Also, others suggest adding the url parameter (url=‘https://fapi.binance.com’) to the Client call, but there is no such a parameter either. Files related to the Python module seem to be located under C:\Users\Administrator\AppData\Local\Programs\Python\Python310\Lib\site-packages\binance that is what I’ve established so far.

Thanks in advance for your inputs.

if you can change the library, you may have a try on this one:

Then this example can help with placing order:

1 Like

Thanks, I will try that library.

Finally, this is what has worked for me.
The binance-futures-connector library did the trick, however, the API key had expired after 90 days because I did not restrict assess for any particular IPs. These are Binance rules that I was not aware of. So, I did a dummy update (just edit and save without changing anything) and afterwards I was able to trigger trades on Binance Futures using the binance-futures-connector library. The initial library I was using (python-binance) did not work for me, not sure why. Perhaps it’s designed for Spot only something I doubt as there are Futures end points in the code. Thanks @dino for your suggestion.

1 Like

Great to hear it works.

I had the same issue and in my case was related to the Binance exchange my account is registered to.
Make sure pass the right tld when creating the client.

client = Client(config.API_KEY, config.API_SECRET, tld =‘com’)

or

client = Client(config.API_KEY, config.API_SECRET, tld =‘us’)

1 Like

OK, thanks a million for sharing your solution.