LearnFinance
← Algorithmic & Systematic Trading Lesson 2 of 6 · 15 min · +50 XP
Algorithmic & Systematic Trading · Lesson 02

Broker APIs: Kite Connect, Upstox, and getting started

Placing orders from Python instead of clicking your broker's app. What the major Indian broker APIs offer and cost; how login works, and why tokens expire daily; rate limits, worked step by step; a minimal script; the safety habits that matter from day one; and how Knight Capital, on 1 August 2012, lost about $460 million in 45 minutes when old code woke up on one server.

Facts reviewed Oct 2026

For a long time, a retail trader in India who wanted to automate had two choices. Click faster, or hire a coder to sit next to the trading screen.

That changed in the mid-2010s, when Zerodha released Kite Connect. It’s a documented API, an interface for programs, that lets anyone who can code place orders, stream prices and pull holdings from their own account. Upstox, 5paisa, ICICI Direct, Alice Blue, Fyers and Angel One followed. Today, retail API access in India is among the widest in the world.

This lesson covers what a broker API is, the main Indian options, and how to start with safe habits from day one.

What a broker API gives you

A modern trading API usually offers:

  • Order management: place, change and cancel orders, such as market, limit, stop-loss and bracket orders.
  • Portfolio queries: current positions, holdings, margins and P&L.
  • Historical data: price candles for backtesting, with limits.
  • Live market data: usually a WebSocket, a connection that streams quotes and depth in real time.
  • An instruments list: every tradable symbol, with its code.

You call it from your language of choice. Python is the default for retail. You get back data in a format called JSON, and write your logic around it.

The major Indian broker APIs

BrokerAPI nameAccess costNotable
ZerodhaKite ConnectOrder APIs free (Personal plan); ₹500 / month with live and historical dataBiggest retail community, best docs, pykiteconnect library
UpstoxUpstox APIFreeGrowing fast, generous rate limits
FyersFyers APIFreePopular with retail quants, good WebSocket
Angel OneSmartAPIFreeGenerous free tier, WebSocket for live data
5paisa5paisa APIFreeBasic but works
ICICI DirectBreeze APIFree (with account)Institutional feel, older docs

Free isn’t always cheaper. Kite Connect’s ₹500 a month data plan comes with good documentation, a big community and a well-tested Python library. Many serious retail traders pick it for that alone. For a hobby, or occasional automation, a free API is fine. Prices change, so check each broker’s current charges.

How login works

Every broker uses a version of the same login flow:

  1. Register a developer app on the broker’s dashboard. You get an API key and an API secret.
  2. Open a login page in the browser, and log in with your normal broker details and a one-time code from an authenticator app.
  3. The broker sends you back to your app’s registered address, with a request token.
  4. Your script swaps the request token and API secret for an access token. For most Indian brokers, it lasts until the end of the day.
  5. You send the access token with every later API call.

That daily expiry is an Indian quirk. In the US, brokerage tokens can last weeks. Here, brokers require a fresh login every trading day, citing regulatory norms. So every morning, your algo needs a new token before it can trade. Most retail traders just log in by hand each day.

Rate limits and reality

Kite Connect, for example, allows roughly:

  • 10 orders a second through the API;
  • 3 requests a second for most data requests; and
  • 1 WebSocket connection per API key, with up to about 3,000 symbols.

Rate limits are strict. Break them, and your requests get rejected, or your access throttled. Every retail algo library limits its own request rate for this reason.

For live prices, always use the WebSocket. Don’t keep asking for quotes over and over. That burns through your rate limit in seconds.

Worked example: will this script hit the limit? Your script checks prices for 40 stocks every second, by asking for each quote separately.

  1. Requests needed: 40 a second.
  2. Limit: about 3 a second.
  3. Over by: 40 ÷ 3 ≈ 13 times. Most requests will be rejected.
  4. The fix: subscribe to all 40 on one WebSocket. Prices then arrive as they change, using no data-request limit at all.
Quick check

Your algo must send 45 orders as fast as allowed, and the API permits 10 orders a second. What's the quickest it can finish?

A minimal Python example

Using Zerodha’s pykiteconnect library, a real script is remarkably short. The symbol below is a made-up placeholder:

kite = KiteConnect(api_key)
data = kite.generate_session(request_token, api_secret)
kite.set_access_token(data["access_token"])

order_id = kite.place_order(
    variety=kite.VARIETY_REGULAR,
    exchange=kite.EXCHANGE_NSE,
    tradingsymbol="SYMBOL",
    transaction_type=kite.TRANSACTION_TYPE_BUY,
    quantity=1,
    order_type=kite.ORDER_TYPE_MARKET,
    product=kite.PRODUCT_CNC,
)

That’s under 15 lines from login to a live order. Which is exactly why safeguards matter.

Safe habits: don’t skip these

Every experienced algo trader has a horror story. These habits prevent most of them:

  1. Test on paper first. Use a broker’s test mode if it has one. Or run the script so it writes orders to a file, without placing them.
  2. Set hard position limits in code. If your algo somehow tries to buy 1,000 lots instead of 1, your code should refuse, before the exchange ever sees it.
  3. Add a kill switch. A file, a setting, anything. If KILL_SWITCH=1, no new orders. You’ll thank yourself in a crisis.
  4. Log every request and response, with timestamps. When something goes wrong at 2 pm, you need to know exactly what happened, and when.
  5. Alert on failure. A crashed script that stops trading is usually better than one that keeps sending rubbish. But only if you find out fast. Message and email alerts are easy to set up.
  6. Handle dropped connections and expired tokens. WebSockets disconnect, and tokens expire. Your algo must recover, not crash.
  7. Never put passwords or keys in your script. Keep them in environment settings, or in a secrets file your code-sharing tool ignores.

Costs and taxes

API trading doesn’t change your tax treatment. Every trade lands in the same broker statement that shows your capital gains and F&O results at year end.

Costs still apply: brokerage, STT, GST, exchange fees, SEBI fees and stamp duty. They add up faster than you expect on a busy strategy. Always work out your break-even cost per trade before running any strategy live.

A real case: Knight Capital, 2012

Knight Capital was one of the biggest US market makers. On one morning, a code mistake nearly destroyed it.

  • Background: Knight had old, unused code for a feature it stopped using in 2003. The code was never deleted.
  • 31 July 2012: staff installed new trading software on eight servers, but missed one. On that server, a reused setting switched the old code back on.
  • 1 August 2012, from 9:30 am: the old code sent orders non-stop. In about 45 minutes, it made about 4 million trades in 154 stocks.
  • The loss: about $460 million, more than the firm could absorb. Within days, Knight needed rescue funding from outside investors.
  • October 2013: the SEC fined Knight $12 million for weak controls on its market access.

Worked example: why a kill switch matters. These round figures follow the reported totals.

  1. Loss rate: $460 million ÷ 45 minutes ≈ $10 million a minute.
  2. Trade rate: 4 million ÷ 2,700 seconds ≈ 1,500 trades a second.
  3. If a kill switch had stopped it after 2 minutes, the loss might have been about $20 million, not $460 million.

The lessons:

  • Delete old code you don’t use.
  • Check every server after a release.
  • A kill switch and limits turn a disaster into a bad morning.

Going further: why “it worked in testing” isn’t enough. Knight’s new code was probably fine. The failure came from the gap between plan and reality: a server missed in the rollout, and an old setting reused for a new purpose. Big firms now use automatic, checked releases, so every server gets the same code. They watch live systems for strange order rates, and stop trading on their own if limits break. A retail script is far smaller, but the same rule applies. Assume something will go wrong, and decide in advance what stops it.

Common mistakes

  • Polling quotes instead of using the WebSocket.
  • Running live without paper testing.
  • Putting API keys in shared code.
  • Having no kill switch, or position limit.
  • Forgetting the daily login in the morning routine.
Practice 1: A strategy trades ₹1 lakh each time, and each round trip costs ₹120 in brokerage, taxes and fees. What's the break-even cost per trade, as a percentage?
  1. Cost per round trip: ₹120.
  2. As a share of the trade: ₹120 ÷ ₹1,00,000 = 0.12%.
  3. So each trade must earn more than 0.12%, on average, just to break even.
Practice 2: A bug sends orders that lose about ₹15,000 a minute. Your alert takes 8 minutes to reach you, and you need 2 more minutes to switch it off. Roughly what does the bug cost?
  1. Minutes running: 8 + 2 = 10.
  2. Cost: 10 × ₹15,000 = ₹1.5 lakh.
  3. With a hard limit that stops the algo after ₹30,000 of losses, the cost would be ₹30,000.

Key takeaways

  • Broker APIs offer orders, portfolio data, historical data and live WebSocket prices.
  • Kite Connect is free for placing orders, and ₹500 a month with market data, as of 2025. It has the biggest community. Upstox, Fyers and Angel One offer free options.
  • Indian access tokens expire daily. Build that into your morning routine.
  • Respect rate limits, and use the WebSocket for live prices.
  • Build kill switches, position limits and logging into your very first script.
  • Knight Capital lost about $460 million in 45 minutes on 1 August 2012, from old code left on one server.

Go deeper

Complete this lesson
Pass the quiz below (60% or more) to earn +50 XP and unlock the next lesson.
Quiz not passed yet
🧠

Quiz: Broker APIs

5 questions · pass 60% to complete