Binance trading bot: API keys, code and a safe setup
Binance is the largest crypto exchange by volume, and its API is one of the best-documented in the business — which is why it's the default venue for first bots. This guide shows exactly how to create safe, restricted API keys, connect with the open-source ccxt library, place your first spot order on the testnet, and respect the rate limits that get careless bots banned.
Step 1 — create restricted API keys
In Binance, go to API Management and create a key. The security settings here matter more than any line of code:
Enable Spot Trading only. Disable withdrawals. Restrict access to your server's IP. Store the secret in an environment variable. A withdrawal-enabled key that leaks empties your account instantly and irreversibly.
Step 2 — connect with ccxt
python · binance_connect.pyimport ccxt, os
ex = ccxt.binance({
'apiKey': os.environ['BINANCE_KEY'],
'secret': os.environ['BINANCE_SECRET'],
'enableRateLimit': True, # ccxt throttles for you
})
ex.set_sandbox_mode(True) # testnet
bal = ex.fetch_balance()
print(bal['USDT']['free'])
Step 3 — place a spot order
python · binance_order.py# market buy 0.001 BTC with the signal from your strategy
if signal() == 'buy':
order = ex.create_market_buy_order('BTC/USDT', 0.001)
print(order['id'], order['status'])
Where does signal() come from? Test the exact rule on our backtester with BTC and ETH series first — if it doesn't beat buy-and-hold after fees there, it won't on Binance either.
Step 4 — respect rate limits
Binance uses a weight system: every endpoint costs “weight” against a per-minute budget, and breaching it triggers a temporary IP ban. Set enableRateLimit: True in ccxt, batch requests, and poll the slowest timeframe your strategy allows. A 1-hour bot needs to call the API far less than a 1-minute scalper.
Step 5 — testnet before mainnet
Binance offers a full Spot Testnet with fake balances. Run your bot there for weeks: confirm orders fill, errors are handled, and the equity curve roughly matches your backtest before risking a cent.
Go-live checklist
- Strategy beats buy-and-hold after fees on the backtester.
- Keys are trade-only, no withdrawals, IP-whitelisted.
- Tested for weeks on the Spot Testnet.
- Position size from the calculator, risk ≤1%.
- Max-drawdown kill switch wired in.
Frequently asked questions
Is it safe to use a trading bot on Binance?
It is safe if you lock down the API keys: enable trading only, disable withdrawals, and whitelist your server IP. The biggest real-world risk is a leaked withdrawal-enabled key, not the bot logic itself.
Does Binance allow trading bots?
Yes. Binance officially supports API trading and even offers built-in bots. Using ccxt or your own code against the API is permitted as long as you respect the rate limits and terms of service.
How do I test a Binance bot without risking money?
Use the Binance Spot Testnet, which gives you fake balances and a separate API endpoint. ccxt's set_sandbox_mode(True) points your bot at the testnet so you can validate execution before going live.
What happens if my bot hits Binance rate limits?
Binance returns 429/418 errors and can temporarily ban your IP. Set enableRateLimit to True in ccxt, use the slowest timeframe your strategy needs, and avoid hammering the API with tight polling loops.