Zerodha API 2026: automate Kite Connect with Python
A practical setup guide for traders who want broker-ready Python automation without leaking tokens, duplicating orders, or confusing backtest code with live execution code.
Summary
- Use Kite Connect for authenticated order, portfolio, margin, GTT, quote, historical, and WebSocket workflows.
- Start with a clean token flow, then add order validation and a duplicate-order guard before going live.
- For realtime ticks and historical candle data, use Zerodha's Connect plan; for basic order APIs, the Personal plan may be enough.
Prerequisites
Account and app
Zerodha trading account, Kite developer app, API key, API secret, and redirect URL.
Plan choice
Personal is free for order and portfolio APIs. Connect adds WebSocket and historical data for INR 500/month per app.
Risk checks
Define allowed symbols, max quantity, product type, order variety, and one-order-per-signal protection before live trades.
Step-by-step Kite Connect flow
- Create a Kite Connect app and save the API key and secret outside your source code.
- Open the official login URL and complete Zerodha login.
- Capture the request_token from the redirect URL.
- Exchange the request_token for an access_token with the Python client.
- Store the access token securely for the trading session.
- Place only validated orders, then log order_id, status, symbol, price, and source signal.
Working Python starter code
Replace placeholders with your own credentials. Do not commit API secrets to GitHub or paste live keys into shared chats.
from kiteconnect import KiteConnect
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
REQUEST_TOKEN = "request_token_from_redirect"
kite = KiteConnect(api_key=API_KEY)
session = kite.generate_session(REQUEST_TOKEN, api_secret=API_SECRET)
kite.set_access_token(session["access_token"])
order_id = kite.place_order(
variety=kite.VARIETY_REGULAR,
exchange=kite.EXCHANGE_NSE,
tradingsymbol="INFY",
transaction_type=kite.TRANSACTION_TYPE_BUY,
quantity=1,
product=kite.PRODUCT_CNC,
order_type=kite.ORDER_TYPE_MARKET,
validity=kite.VALIDITY_DAY,
)
print("Order placed:", order_id)Common errors and fixes
| Error | Likely cause | Fix |
|---|---|---|
| 403 or token invalid | Expired access token, wrong API key, or reused request token | Run the login flow again and exchange a fresh request_token once. |
| Order rejected | Wrong symbol, exchange, product, lot size, or margin | Validate against instruments, product rules, and margin before placing. |
| Duplicate orders | TradingView alert fired again or webhook retried | Save signal IDs and reject duplicates within the same candle or strategy state. |
| Rate limit errors | Polling quotes or placing too many requests too fast | Batch quote calls, use WebSockets for ticks, and add request throttling. |
FAQ
Is Zerodha Kite Connect API free in 2026?
Zerodha lists a free Personal API plan for order, GTT, alerts, margin, and portfolio APIs. The Connect plan adds realtime WebSocket data and historical candles for INR 500/month per app.
Do I need WebSocket data?
Use WebSockets when your system reacts to live tick data. If your order signal already comes from TradingView, your webhook receiver may only need validated order placement plus logging.
Should beginners automate live orders immediately?
No. Start with paper logs, then small quantity dry-runs, then live orders with max-loss, max-orders, and duplicate-signal protection.