Skip to content

How do I get a Binance API key and use it in Node.js? ​

Create the key in your Binance account under API Management, store the key and secret in environment variables, and pass them to MainClient from the binance SDK. A call to getAccountInformation() confirms that the key works. Public market data needs no key at all.

Create the key ​

  1. Log in to Binance and open Profile, then Account, then API Management.
  2. Choose Create API and pick a key type. A system-generated key uses HMAC and gives you a key and a secret. A self-generated key uses Ed25519 or RSA, and you register a public key that you generate yourself.
  3. Name the key and confirm with your two-factor device or passkey.
  4. Copy the secret straight away. Binance shows it once.

Binance asks for two-factor authentication, identity verification and a first deposit before it issues your first key. Its guide to creating API keys has the current steps.

Choose the permissions ​

  • Enable reading first. Add trading when you are ready to place orders, and leave withdrawals off unless you plan to withdraw through the API.
  • Restrict the key to the IP addresses your server uses. Binance limits an HMAC key with no IP restriction to reading. Adding an IP restriction, or using a self-generated key, lets you enable trading.
  • Keep the secret on a server you control. Keep it out of browser bundles, mobile apps and public repositories.

The API key setup guide covers permissions, Ed25519 keys and the Testnet and demo trading keys in more depth.

Use the key in Node.js ​

Install the SDK and put the key and secret in a .env file:

sh
npm install binance
sh
BINANCE_API_KEY=your-api-key
BINANCE_API_SECRET=your-api-secret

The SDK recognises HMAC, RSA and Ed25519 keys from the secret you pass, so the code is the same for every key type. This example reads the account and prints the number of balance records:

ts
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;
});

Run it with your environment variables loaded, for example npx tsx --env-file=.env account-state.ts. A working key prints Account access verified. followed by the number of balance records.

If the call fails ​

Binance codeMeaningWhere to look
-2014The key is not in the expected format, often an empty or truncated valueCheck that both environment variables are loaded
-2015The key, the IP address or the permissions were not acceptedWhy does Binance return error -2015?
-1022The signature did not matchCheck the secret and the authentication guide
-1021The request timestamp fell outside the receive windowSync the host clock, then see signing and timestamps

Built by Siebly. Open source SDKs for exchange APIs.