Futures Testnet APIError(code=-2015): Invalid API-key, IP, or permissions for action

I’m having issues executing a trade on Futures Testnet. I believe I’m using the correct links (see code below)
And I’m using the api keys provided in https://testnet.binancefuture.com/en/futures/ when logged into my account below the chart there is an ‘API Key’ tab. I believe they are automatically set up to allow trades? If not, I’m not sure how to adjust them to allow trades in testnet, as there is no option in profile to modify. Is there any other issue that you can spot from my code snippet below?

api_key_testnet = “xxx”
api_secret_testnet = “xxx”

limit_order_basis = True
mode = “test”
api_url = “https://testnet.binance.vision/api

client = Client(api_key=api_key, api_secret=api_secret, testnet = True)
client.API_URL = api_url # I have also tried commenting out this line BUT have the same issue regardless

def place_order(ticker, price, quantity, direction, stop_loss):
side = client.SIDE_BUY if direction == “LONG” else client.SIDE_SELL
stop_side = client.SIDE_SELL if direction == “LONG” else client.SIDE_BUY # Opposite for stop loss

# Place the initial order (market or limit)
if limit_order_basis:
    order = client.create_order(
        symbol=ticker,
        side=side,
        type=client.ORDER_TYPE_LIMIT,
        timeInForce=client.TIME_IN_FORCE_GTC,
        quantity=quantity,
        price=str(price)  # Ensure price is string to match API expectations
    )
else:
    order = client.create_order(
        symbol=ticker,
        side=side,
        type=client.ORDER_TYPE_MARKET,
        quantity=quantity
    )
if order and 'orderId' in order:
    stop_price = str(stop_loss)  # Ensure stop_price is string
    stop_order = client.create_order(
        symbol=ticker,
        side=stop_side,
        type=client.ORDER_TYPE_STOP_LOSS_LIMIT,
        timeInForce=client.TIME_IN_FORCE_GTC,
        quantity=quantity,
        price=stop_price,  # For STOP_LOSS_LIMIT, this is the limit price
        stopPrice=stop_price  # Trigger price
    )
    print("Stop loss order placed: ", stop_order)
    print("Stop loss:", stop_loss)
else:
    print("Failed to place the initial order, so no stop loss order was placed.")

return order

File “C:\Users\Eduards\AppData\Local\Programs\Python\Python311\Lib\site-packages\binance\client.py”, line 368, in _handle_response
raise BinanceAPIException(response, response.status_code, response.text)
binance.exceptions.BinanceAPIException: APIError(code=-2015): Invalid API-key, IP, or permissions for action.

I also had problems with non-binance libs, so I’d suggest using the official Binance Python library. GitHub - binance/binance-futures-connector-python

the repo has plenty of examples, but importing and using testnet would look something like this:

from binance.um_futures import UMFutures

um_futures_client = UMFutures(
    key='<>',
    secret='<>',
    base_url="https://testnet.binancefuture.com",
)

 trades = um_futures_client.trades(symbol=symbol, limit=limit)

EDIT: just realized that the URL you’re specifying might be wrong. https://testnet.binancefuture.com should work. You can try that first, and if no dice, switch to the official library.

Good luck!