⬡ SECURITY · TRADING BOT · UPDATED OCT 2026

I leaked my Binance API key. Now what?

Pushing a .env file to a public repo is the most common mistake people make when they start building trading bots. Here is the right order of what to do — starting with the step almost everyone does last.

By the RoboTraderIA team· published Oct 29, 2026· 0 affiliate links
Short answer:
Delete the key on Binance before anything else (profile → Account → API Management). Deleting the file or the commit does not help: bots scan GitHub and find secrets within seconds, and copies of the history live on in forks and clones. Then check orders, trades and withdrawals, change your password, and only then clean the repo and create a new key with an IP restriction and no withdrawal permission.

The search "binance_api_secret exposed .env" exists because the story repeats itself: a tutorial says to keep the key in a .env file, you run git add ., push to GitHub and only notice later — sometimes from an exchange e-mail, sometimes from orders you never placed. If that is you, follow the order below.

01The first 10 minutes, in this order

  1. Delete the key on Binance. On the website: profile icon → Account → API Management, and delete the exposed key. That kills it immediately. GitHub's own documentation says the first step is to revoke or rotate the secret, not to touch the code.
  2. Cancel open orders and check what happened. Review open orders, order history, trades and recent withdrawals. Watch for buys of thinly traded coins you did not make.
  3. If there are withdrawals or strange trades, disable the account. Profile → Account → Security → Account Management → Disable Account, and open a support ticket.
  4. Change your password and review devices under Security (Login Password, Devices & Activities, Account Activity). If the repo held other passwords or tokens, rotate them all.
  5. Only then clean the repo (section 04) and create a new key the right way (section 05).

"But the key had no withdrawal permission." Not enough. With trading permission, an attacker can use your balance to buy an illiquid coin at an inflated price and sell theirs to you. That is how the 3Commas API key leak (2022) worked: about 100,000 keys exposed and US$27.3 million in losses, mostly from Binance users.

02How fast a leaked key is found

Discovery
20 secondsmedian from push to detection (NDSS 2019)
Exploitation
< 5 minutesexposed AWS keys used by attackers (Unit 42, 2023)
Forgotten
81%of secrets found were never removed within 16 days

The academic study "How Bad Can It Git?" (NDSS 2019) monitored public GitHub and found over 200,000 unique secrets. The researchers' pipeline found a new secret a median of 20 seconds after the push — and attackers use the same technique. Deleting the commit an hour later is too late.

03Why GitHub did not warn you

GitHub has a system that detects secrets and notifies the provider so it can revoke the key automatically. But it only recognizes the formats on its official partner list — and no crypto exchange is on it: not Binance, not Bybit, not Coinbase. So neither the alert nor push protection catches a Binance key. Security is entirely on you.

04Cleaning the repo (after deleting the key)

Once the key is deleted, rewriting history is hygiene, not an emergency — GitHub itself says it is often unnecessary if the credential has been revoked. If you want to clean it, the recommended tool is git-filter-repo:

# requires git-filter-repo 2.47 or newer
git clone https://github.com/YOUR_USER/YOUR_REPO.git
cd YOUR_REPO
git-filter-repo --sensitive-data-removal --invert-paths --path .env
git remote add origin https://github.com/YOUR_USER/YOUR_REPO.git
git push --force --mirror origin
  • History is rewritten: hashes change and anyone with an old clone must re-clone (or rebase, never merge).
  • Forks and other people's clones keep the key. You cannot delete their copies — which is why step 1 is the one that matters.
  • To remove cached views and pull request references, open a GitHub Support ticket with the repository and the first changed commit.
  • Alternative: BFG Repo-Cleaner (bfg --replace-text passwords.txt), which by default does not touch your latest commit — clean HEAD first.

05The new key, done right

SettingWhy
A sub-account just for the botKeep only the balance the bot trades. Regular accounts can have up to 5 sub-accounts, each with up to 30 keys.
Reading + Spot trading, nothing elseNever enable withdrawals or transfers for a bot. One key per application.
IP restrictionSince 2023, an HMAC key without IP restriction only has read permission. With your VPS IP whitelisted, the key does not work anywhere else.
Ed25519 keyBinance considers HMAC keys (the ones with a "secret") deprecated and recommends Ed25519: the private key stays with you and never travels.
Regular rotationBinance recommends rotating keys every 30 to 90 days.

On Bybit? Keys can only be deleted on the website (not the app): username → Account & Security → API Management → Delete. Keys without a bound IP expire after 90 days, and 7 days after a password change.

06Prevent it in code: three layers

1. Secrets out of the code. Use environment variables with python-dotenv:

# bot.py
import os
from dotenv import load_dotenv

load_dotenv()  # reads .env from the folder; does not override existing variables
API_KEY = os.environ["BINANCE_API_KEY"]
API_SECRET = os.environ["BINANCE_API_SECRET"]

2. The .env never enters Git. GitHub's official Python .gitignore template already includes .env. Publish a .env.example with variable names and empty values:

# .gitignore
.env
.env.*
!.env.example

3. A guard before every commit. gitleaks (or detect-secrets) runs as a pre-commit hook and blocks the commit if it finds something that looks like a key:

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.24.2
    hooks:
      - id: gitleaks

And one habit that prevents half of all leaks: review what goes into the commit with git diff --cached instead of a blind git add .. A full bot built with these practices is in crypto bot in Python on Binance.

Sources, checked Oct 29, 2026: Binance — creating and restricting API keys, key types, 5 security tips, disabling an account, sub-accounts; GitHub — supported patterns, removing sensitive data; NDSS 2019, "How Bad Can It Git?"; Unit 42 (2023); ForkLog on the 3Commas case; Bybit; OWASP.

07Frequently asked questions

Does deleting the .env file from GitHub fix it?

No. The file stays in the Git history, in forks, clones and caches. The only thing that fixes it is deleting the key on Binance; cleaning the repo comes after.

How do I delete an API key on Binance?

On the Binance website, click the profile icon, go to Account and then API Management. Delete the exposed key there and it stops working immediately.

Does GitHub warn me when I push a Binance key?

No. Binance and the other crypto exchanges are not on the list of patterns GitHub secret scanning recognizes, so neither the alert nor push protection detects these keys.

If the key had no withdrawal permission, am I safe?

Not entirely. With trading permission, an attacker can use your balance to buy illiquid coins at inflated prices. That is what happened in the 3Commas case, with US$27.3 million in losses.

What permissions should a Binance bot key have?

Reading and Spot trading, with an IP restriction, on a sub-account used only by the bot. Never enable withdrawals or transfers.

Does a Binance key without IP restriction work?

Since 2023, an HMAC key without IP restriction only has read permission. To trade, you must whitelist the IP your bot runs from.

Related reading