Static IP for the Flattrade API
Flattrade's Pi API needs a registered static IP before it will even create an API key, and both your daily token and every order must come from that same IP. You need one fixed public IP that only you use, entered when you create the Pi API key, with the login and order calls both sent through it.
What Flattrade checks
From Flattrade's support pages and its API version 2 announcement:
- No key without it. A static IP is mandatory to create an API key. Dynamic home IPs aren't accepted.
- Two addresses. One primary IP and one optional backup IP.
- One change a week. Either one can be changed at most once a calendar week.
- Token and orders from the same IP. The access token must be generated from the registered IP, and orders must come from it too. A mismatch between the token IP and the order IP gets the order rejected.
- Unique per client. Static IPs must be unique across Flattrade clients. Family sharing of one IP wasn't yet enabled when Flattrade announced API v2.
That token rule is stricter than most brokers. Generate the token through the fixed IP too, not from your home connection.
IPv4 or IPv6 for Flattrade
Flattrade's pages don't say whether the static IP field accepts IPv6. On the network side, piconnect.flattrade.in has an IPv6 address, so the API and WebSocket are reachable over IPv6. Try the IPv6 address in the Pi key screen or ask Flattrade support. If you'd rather not wait, IPv4 works for certain.
How to register your IP with Flattrade
- Get your fixed IP's proxy details: host, port, username and password.
- Check the exact exit address:
curl -x http://USER:PASS@HOST:PORT https://ifconfig.co - Log in to Flattrade's WALL platform, open Pi, and create a new API key with your redirect URL and the static IP.
- Run the daily login flow from the Pi docs with the proxy already set, so the token is issued against the registered IP.
Send Flattrade calls through the fixed IP
NorenRestApiPy calls requests.post() directly, so the standard proxy environment variables route every REST call:
import os
PROXY = os.environ["PROXY_URL"] # http://USER:PASS@HOST:PORT
for v in ("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"):
os.environ[v] = PROXY # every requests call in this process uses the fixed IP
from NorenRestApiPy.NorenApi import NorenApi
class FlattradeApi(NorenApi):
def __init__(self):
NorenApi.__init__(
self,
host="https://piconnect.flattrade.in/PiConnectAPI/",
websocket="wss://piconnect.flattrade.in/PiConnectWSAPI/",
)
api = FlattradeApi()
api.set_session(userid=os.environ["FLATTRADE_USERID"], password="", usertoken=os.environ["FLATTRADE_TOKEN"])
If your token script runs separately, set the same variables there as well, or the token will be issued against your home IP and orders will fail. The WebSocket doesn't pick up these variables. The full Flattrade proxy guide on GitHub has a small patch that routes it through the proxy, plus a sample order.
Common Flattrade errors
- Orders rejected with an invalid IP message. Either the order or the token came from a different IP. Regenerate today's token through the proxy, then retry from the same process.
- set_session succeeds but calls fail.
set_sessiononly stores values locally. The first real call is where token and IP problems show up. - Token works one day, fails the next. Flattrade tokens are generated daily. Make a new one each trading day.
- 407 Proxy Authentication Required. Wrong proxy credentials, or a password with
@or:that needs URL-encoding.
For a step-by-step check of IP errors, see how to fix "IP not whitelisted" errors.
Flattrade static IP: FAQs
Can I create a Flattrade API key with a home IP?
No. Flattrade requires a static IP to create an API key, and dynamic home IPs aren't accepted.
Why are my Flattrade orders rejected when the IP is correct?
Check where the token was generated. Flattrade needs the token and the orders to come from the same registered IP, so a token made on your home connection fails even if orders leave from the right IP.
Can I add a backup IP on Flattrade?
Yes, one primary and one optional backup IP. Each can be changed at most once a calendar week.
Which TrueIP plan should I take for Flattrade?
IPv4 at ₹200 a month plus GST works for certain. If Flattrade accepts your IPv6 address in the Pi key screen, a dedicated IPv6 at ₹50 a month is the cheaper option.
Sources
- Flattrade: API version 2 launch
- Flattrade support: why only a static IP is allowed
- Flattrade support: orders rejected for invalid IP
- Flattrade support: backup static IP
- Flattrade support: how often the IP can be updated
Get your dedicated static IP
IPv6 ₹50 a month, IPv4 ₹200 a month, plus GST. Live in minutes, whitelisted once.