Why does Binance return error -2015?
Binance returns -2015, "Invalid API-key, IP, or permissions for action", when it does not accept the API key for that request. The cause is one of three things: the key is wrong or belongs to a different environment, the IP address you call from is not on the key's allow list, or the key lacks the permission the call needs.
With the binance SDK the rejection arrives as a thrown error. Its code field holds -2015 and its message holds the Binance text.
Check the key and its environment
- Log the length of the key your process loaded. A stray space, a newline from copying, or an environment variable that did not load all produce a rejected key.
- Use a key that matches the endpoint. Production, Spot Testnet and demo trading each issue their own keys, so a Testnet key fails on production and the reverse is also true. Set
testnet: trueordemoTrading: trueonly for keys made in those environments. See Testnet and demo trading. - Use a key created on the same Binance site you call.
- Check that the key still exists in API Management. A deleted or regenerated key stops working straight away.
A malformed key value returns -2014 instead, so a -2015 response means the format was accepted and the key was not.
Check the IP address
If the key has IP restrictions, every request has to come from a listed address. A new deployment, a different container host, a VPN or a proxy can all change the address Binance sees. Add the outbound address your server really uses. If you need a fixed address, route requests through a proxy with a stable IP. See Proxies.
Check the permissions
Each call needs the matching permission on the key: reading for account queries, Spot and margin trading for Spot orders, and futures for futures calls. A futures permission also needs a futures account on Binance. Binance limits an HMAC key with no IP restriction to reading, so a trading call returns -2015 until you add an IP restriction or use a self-generated key. Binance's guide to creating API keys lists the permission rules.
Let the SDK tell you what the key allows
This example makes an account read. If Binance answers -2015, it asks Binance for the key's permissions with getApiKeyPermissions():
import { MainClient } from 'binance';
async function main() {
const api_key = process.env.BINANCE_API_KEY;
const api_secret = process.env.BINANCE_API_SECRET;
if (!api_key || !api_secret) {
throw new Error('Set BINANCE_API_KEY and BINANCE_API_SECRET.');
}
const client = new MainClient({ api_key, api_secret });
try {
await client.getAccountInformation();
console.log('The key, IP address and permissions work for account reads.');
} catch (error) {
if ((error as { code?: number }).code !== -2015) throw error;
console.log('Binance returned -2015. Checking what the key allows.');
try {
const permissions = await client.getApiKeyPermissions();
console.log('IP restrictions on:', permissions.ipRestrict);
console.log('Reading enabled:', permissions.enableReading);
console.log('Spot and margin trading enabled:', permissions.enableSpotAndMarginTrading);
console.log('Futures enabled:', permissions.enableFutures);
} catch {
console.log('The key itself was rejected. Check the key, its environment and the IP address you call from.');
}
}
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : 'Request failed. See the troubleshooting guide.');
process.exitCode = 1;
});Read the result like this:
- If
getApiKeyPermissions()also fails, Binance is rejecting the key or the IP address. Work through the key and IP checks above. - If it succeeds, compare the flags with the call that failed. Enable the missing permission in API Management and try again.
- Spot, USD-M futures and COIN-M futures are separate products with separate clients (
MainClient,USDMClientandCoinMClient). Test with the client for the product you trade.
Similar errors
-1022: the signature did not match. Check the secret. See authentication.-1021: the timestamp fell outside the receive window. See signing and timestamps.