Introduction
Public Binance market data needs no key at all. As soon as a script reads balances, places orders or opens a user data stream, it needs a Binance API key, and every private request has to be signed. This article walks through the whole path in Node.js: creating the key, keeping the secret out of your code, signing with either an HMAC secret or an Ed25519 key pair, and making sense of the errors Binance sends back when something is off.
The examples use the binance package on npm, a community maintained, MIT licensed SDK for the Binance REST APIs and WebSockets with TypeScript types included. All the code below was run on Node.js 24 with binance 3.6.15.
npm install binance
Step 1: Create the key on Binance
In your Binance account, open Profile, then Account, then API Management, and choose Create API. Binance asks for two-factor authentication, identity verification and a deposit of any amount to the Spot account before the first key can be created.
You then pick a key type:
System generated keys use HMAC. Binance shows you an API key and a secret key.
Self generated keys use Ed25519 or RSA. You create the key pair yourself, upload only the public key, and Binance gives you an API key that belongs to it.
Two settings matter most:
Permissions. Start with reading only. Turn on Spot or futures trading when the code is ready for it, and leave withdrawals off unless the script really withdraws.
IP restriction. Binance's own guidance is clear here: an HMAC key without an IP restriction only gets read permission. Add the IPv4 address of the server that runs your bot.
Step 2: Keep the secret out of the code
Put the values in a .env file that never goes into Git:
BINANCE_API_KEY=your-api-key
BINANCE_API_SECRET=your-api-secret
Node.js 24 reads it natively with node --env-file=.env script.mjs, so no extra package is needed.
Step 3: Check that the key works
Creating a client does not contact Binance, so a typo in the key only shows up on the first private call. A small check script saves a lot of guessing later. Save this as check-key.mjs:
import { readFileSync } from 'node:fs';
import { MainClient } from 'binance';
const api_key = process.env.BINANCE_API_KEY;
const api_secret = process.env.BINANCE_PRIVATE_KEY_PATH
? readFileSync(process.env.BINANCE_PRIVATE_KEY_PATH, 'utf8')
: process.env.BINANCE_API_SECRET;
if (!api_key || !api_secret) {
throw new Error('Set BINANCE_API_KEY and BINANCE_API_SECRET or BINANCE_PRIVATE_KEY_PATH.');
}
const client = new MainClient({ api_key, api_secret });
try {
const account = await client.getAccountInformation();
console.log('Key works. Can trade:', account.canTrade, 'Balances:', account.balances.length);
} catch (error) {
console.error('Binance said', error.code, error.message);
process.exitCode = 1;
}
Run it with node --env-file=.env check-key.mjs. The same script handles both key types, which the next step explains.
Step 4: Use an Ed25519 key instead of HMAC
Ed25519 keys keep the private half on your machine, and they are the key type Binance asks for when you log in a WebSocket API session once instead of signing every request. Node.js can create the pair with its built in crypto module. Save this as gen-keys.mjs:
import { generateKeyPairSync } from 'node:crypto';
import { writeFileSync } from 'node:fs';
const { publicKey, privateKey } = generateKeyPairSync('ed25519');
writeFileSync('binance-ed25519-public.pem', publicKey.export({ type: 'spki', format: 'pem' }));
writeFileSync('binance-ed25519-private.pem', privateKey.export({ type: 'pkcs8', format: 'pem' }), { mode: 0o600 });
console.log('Upload binance-ed25519-public.pem in API Management. Keep the private file on the server.');
The public file starts with -----BEGIN PUBLIC KEY-----. Paste it into the self generated key form on Binance, and keep the private file readable only by your user (the 0o600 mode does that). Binance also publishes its own key generator app if you prefer a desktop tool.
To use the key, point the check script at the private file instead of a secret:
BINANCE_API_KEY=your-ed25519-api-key
BINANCE_PRIVATE_KEY_PATH=binance-ed25519-private.pem
There is no separate setting for the key type. When api_secret holds a PEM private key, the package detects Ed25519 or RSA and signs with it. You can see the difference in the request: an HMAC signature is 64 hexadecimal characters, while the Ed25519 signature in my test was 88 characters of Base64.
Step 5: Read the errors Binance sends back
When a private call fails, the package rejects with an object that carries Binance's code and message, plus the request URL for debugging. These are the messages I got while testing on 2 December 2026:
| What I did | code | message | | --- | --- | --- | | Key that is too short | -2014 | API-key format invalid. | | Well formed key Binance does not know | -2015 | Invalid API-key, IP, or permissions for action. | | Ed25519 PEM with an unknown key | -2015 | Invalid API-key, IP, or permissions for action. | | No key passed to the client | none | Private endpoints require api and private keys to be set |
The -2015 message covers three different causes, so check them in order: the key itself (copied in full, right environment), the IP allow list (the server's public IPv4 address), and the permission the call needs (for example futures trading for a futures order).
Step 6: Testnet and demo keys are separate
Live, Spot testnet and demo trading keys are issued separately and do not work across environments. The package switches the base URL with one option:
import { MainClient } from 'binance';
const api_key = process.env.BINANCE_API_KEY;
const api_secret = process.env.BINANCE_API_SECRET;
const testnet = new MainClient({ api_key, api_secret, testnet: true }); // testnet.binance.vision
const demo = new MainClient({ api_key, api_secret, demoTrading: true }); // demo-api.binance.com
If a key works in one script and fails with -2015 in another, a mismatched environment is the first thing to check.
A note on timestamps
Every signed request carries a timestamp and a recvWindow. The package adds both for you, with a default window of 5000 ms. It uses your local clock unless you turn on its time sync with disableTimeSync: false, so it is worth checking the offset once:
import { MainClient } from 'binance';
const serverTime = await new MainClient().getServerTime();
console.log('offset ms', Date.now() - serverTime);
On my test machine the offset was about 650 ms, well inside the window. If yours is several seconds, fix the system clock first (for example with NTP) rather than raising recvWindow.
Summary
Public data needs no key; private calls need a key and a signature.
Keep permissions narrow, restrict the key to your server's IP, and keep the secret in environment variables or a protected file.
With the binance package, HMAC secrets and Ed25519 or RSA PEM keys go into the same api_secret option.
-2014 means the key is malformed; -2015 means the key, IP or permission does not match.
The package documentation has a longer guide on API key setup, including testnet and demo keys: https://sieblyio.github.io/docs/binance/guides/api-key.html
Join the conversation! Your thoughts help the community grow.