Binance API key setup with the SDK
Public market data needs no key. Account queries and trading on the Binance API need an API key and secret.
Create a key
In the Binance account, open API Management and create a key for the environment you will call (production, Spot Testnet or demo trading). Each environment issues its own key. A production key will not work on Testnet, and the reverse is also true.
When you create the key:
- Enable only the permissions you need. Start with read access if you are only querying the account. Enable trading when you are ready to place orders. Leave withdrawals disabled unless you intend to withdraw through the API.
- Restrict the key to known IP addresses where the exchange allows it.
- Store the secret as soon as it is shown. The secret is not shown again.
The secret must stay on a server you control. Do not put it in a browser bundle, a mobile app, or a public repository.
Use the key with the SDK
Supply the key and secret through environment variables, then construct the client:
import { MainClient } from 'binance';
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.');
export const client = new MainClient({ api_key, api_secret });Creating the client does not verify the key. Use a private read, such as account information, to check it:
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 });
const account = await client.getAccountInformation();
console.log('Account access verified.');
console.log('Returned balance records:', account.balances.length);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Save that example as account.mjs and run it with Node.js 24:
node --env-file=.env account.mjsA successful request prints Account access verified. and the number of returned balances. See getAccountInformation() and Error handling if the request fails.
Ed25519 keys
Binance also accepts a self-generated Ed25519 key. In API Management, choose a self-generated key, register the public key, and keep the private key PEM on the server.
Pass the PEM contents as api_secret. The SDK recognises an Ed25519 or RSA private key from the PEM and signs with it. Load the key from a file so the PEM stays out of .env:
import { readFileSync } from 'node:fs';
import { MainClient } from 'binance';
const api_key = process.env.BINANCE_API_KEY;
const secretPath = process.env.BINANCE_API_SECRET_PATH;
if (!api_key || !secretPath) {
throw new Error('Set BINANCE_API_KEY and BINANCE_API_SECRET_PATH.');
}
export const client = new MainClient({
api_key,
api_secret: readFileSync(secretPath, 'utf8'),
});The same api_secret option works on USDMClient, WebsocketClient and WebsocketAPIClient. session.logon on the WebSocket API uses an Ed25519 key.
Testnet and demo keys
- Spot Testnet: create the key at Spot Testnet and set
testnet: trueonMainClientand any Spot WebSocket clients. See Spot Testnet. - Demo trading, including futures demo (often called a futures testnet in Binance documentation): create the key in Binance Demo Trading and set
demoTrading: true. See demo trading and Futures Testnet.
Common failures
| Symptom | What to check |
|---|---|
| Signature or key error | The key, secret, product (Spot vs futures) and environment all match, and the secret was copied in full |
| IP or permission error | IP restrictions and the permissions granted to the key |
| Timestamp outside receive window | Host clock sync, then recvWindow if needed. See authentication |
For a step by step check of -2015, see Why does Binance return error -2015?
Authentication and signatures · Testnet and demo trading · Troubleshooting