Code Examples
Annotated walkthrough of the official example_orders.py and example_market.py scripts shipped in the Shoonya_API_OAuth repo — the real reference implementation behind the Python SDK, not illustrative pseudocode.
Annotated walkthrough of the official example_orders.py and example_market.py scripts shipped in the Shoonya_API_OAuth repo — the real reference implementation behind the Python SDK, not illustrative pseudocode.
Overview
The Shoonya_API_OAuth repo ships two runnable CLI scripts —
example_orders.pyandexample_market.py— that exercise every major call through a simpleinput()-driven menu loop. They're the fastest way to confirm your credentials and see real request/response shapes before wiring anything into a strategy. Both scripts import the same wrapper class,ShoonyaApiPy, fromapi_helper.py.ShoonyaClientfor readability. The real class isShoonyaApiPy(also aliasedNorenApiPyin some forks), and its method names —place_order,get_quotes,searchscrip,start_websocket— are what you'll actually import and call.Shared setup: OAuth login
Unlike the password/TOTP-based fork, this repo authenticates via the OAuth flow documented on Manual Authentication (OAuth) — redirect to the authorize URL, capture the
code, then exchange it for a token:cred.ymlfile loaded at startup. This OAuth fork has no equivalent file — the only long-lived secret issecret_key, which should be loaded from an environment variable or secrets manager, never hardcoded as shown above. Treat it exactly like the checksum inputs on the Manual Authentication (OAuth) calculator.example_orders.py — full script
A menu-driven loop covering the full order lifecycle. Reproduced with the OAuth login block collapsed (see above):
'd'(Daily MTM) branch is an infinite loop with no sleep or break condition — it's a quick-and-dirty demo, not something to run unmodified. Add a poll interval and an exit condition (see Daily MTM) before reusing it.modify_order's success payload returns the order number underresult, notnorenordno— the same field-naming quirk documented on Modify Order. The example script just prints the raw dict, so this is easy to miss until you try to parse it.place_order — real parameter mapping
The wrapper's Python keyword arguments don't share names with the raw JSON fields documented on Place Order. This is the actual mapping used by
api_helper.py:B/SC/M/I/H/B&, e.g.M&MMKT/SL-MKT; whether the broker's OMS actually accepts them is a separate, account-level restriction — see the warning on Place Order.MKTNoneunlessprice_typeis an SL variantDAY/IOC/EOSexample_market.py — full script
The market-data counterpart, covering symbol search, quotes, contract info, historical candles, and the option chain:
'o'handler fetches the chain once viaget_option_chain, then loops and callsget_quotesper strike returned. For a chain with a real strike count (10–20+ per side), that's 10–20+ sequential REST calls purely for a live price refresh — exactly the polling pattern Rate Limits warns against. Resolve tokens once withget_option_chain, then subscribe to those tokens on the WebSocket feed instead of re-polling quotes per strike.get_time_price_series — the two calling patterns
The script shows both an explicit-range call and a defaults-only call, and the difference matters:
get_time_price_series(exchange, token, starttime, endtime, interval)'v'branch above.get_time_price_series(exchange, token)starttime/endtime/intervalfalls back to wrapper defaults (effectively "today, 1-minute candles") — used by the't'branch.The included helper
get_time(time_string)converts a'%d-%m-%Y %H:%M:%S'string to epoch seconds viatime.mktime— useful for buildingstarttime/endtimewithout hand-computing timestamps. Note this uses local system time, not UTC/IST explicitly, so keep the host clock's timezone in mind (see the NTP note on TOTP Setup for a related clock-drift issue elsewhere in this API).Best practices
p.logging.basicConfig(level=logging.DEBUG)at the top of both scripts prints every raw HTTP request/response — keep this on while debugging, but turn it off (or route to a file) in anything long-running, since it will log your access token on every call.'d'Daily MTM branch's unboundedwhile Trueloop is illustrative only — see Daily MTM for a bounded version with a recompute interval.api.subscribe(...)from insideopen_callback, never before — the socket has to confirm open before a subscribe frame is meaningful. Structure your own WebSocket code the same way; see Streaming Code Examples.secret_keyin the login block as shown for readability above — load it from an environment variable, matching the pattern warned about on Manual Authentication (OAuth).Notes
Source: example_orders.py and example_market.py on GitHub. This fork swaps the TOTP/password login block used by other Shoonya SDK forks for
getOAuthURL→getAccessToken→injectOAuthHeader— see Manual Authentication (OAuth) for the underlying HTTP flow this wraps.