APIError(code=-2014): API-key format invalid.

Hello developers!

I am new (so be kind), and while i was writing my code to connect to the API test i had this error. APIError(code=-2014): API-key format invalid.

Searching on GitHub and Stackoverflow i didnt find nothing.

Can anyone help me to fix this issue?

image

# Importing libraries 
from binance.client import Client
import configparser

# Loading keys from config file
config = configparser.ConfigParser()
config.read_file(open(r'C:\\*mypath*\\secret.cfg'))
test_api_key = config.get('BINANCE', 'TEST_API_KEY')
test_secret_key = config.get('BINANCE', 'TEST_SECRET_KEY')

client = Client(test_api_key, test_secret_key)

client.API_URL = 'https://testnet.binance.vision/api'  # To change endpoint URL for test account  

info = client.get_account()  # Getting account info

print(info)

The only possible reason is you failed to get correct key/secret to your two vars

In my Cfg file I just wrote this lines of code

[BINANCE]

TEST_API_KEY= my key
TEST_SECRET_KEY = my secret

We’re 100 miles away from API usage question already…

This works just fine to me. You might want to check if your key/secret are correct

Ok lemme try to regen my keys

Still having the same problem

You didn’t define client object…

and how can I define it? there is documentation somewhere?

What happened to below lines that you included at first?

client = Client(test_api_key, test_secret_key)

client.API_URL = ‘https://testnet.binance.vision/api’ # To change endpoint URL for test account

info = client.get_account() # Getting account info

i edited with your suggestion

now i have this problem

[Running] python -u "c:\Users\ssida\OneDrive\Documenti\GitHub\AI7XF205SS\getting_account_info.py"
C:\Users\ssida\anaconda3\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.binance.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(
C:\Users\ssida\anaconda3\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.binance.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(
C:\Users\ssida\anaconda3\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.binance.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(
C:\Users\ssida\anaconda3\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.binance.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(
C:\Users\ssida\anaconda3\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.binance.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(
Traceback (most recent call last):
  File "c:\Users\ssida\OneDrive\Documenti\GitHub\AI7XF205SS\getting_account_info.py", line 13, in <module>
    info = client.get_account()  
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 1822, in get_account
    return self._get('account', True, data=params)
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 292, in _get
    return self._request_api('get', path, signed, version, **kwargs)
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 242, in _request_api
    return self._request(method, uri, signed, **kwargs)
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 237, in _request
    return self._handle_response()
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 285, in _handle_response
    raise BinanceAPIException(self.response)
binance.exceptions.BinanceAPIException: APIError(code=-2015): Invalid API-key, IP, or permissions for action.

[Done] exited with code=1 in 1.968 seconds

I figure out that visual studio code was broken, i tried this script on a different program (like spyder) and works fine.

Thanks

How is possible that on Spyder my code works but it does not in Visual Studio Code?

I faced the same issue, and the problem was that I was adding single quotes on each of the keys. Removing them made it work (even in VS Code).
Hope this helps.

1 Like

don’t use -
test_api_key = config.get(‘BINANCE’, ‘TEST_API_KEY’)
test_secret_key = config.get(‘BINANCE’, ‘TEST_SECRET_KEY’)

use-
import ast
test_api_key = ast.literal_eval(config.get(‘BINANCE’, ‘TEST_API_KEY’))
test_secret_key = ast.literal_eval(config.get(‘BINANCE’, ‘TEST_SECRET_KEY’))