DEV Community

Declan
Declan

Posted on Edited on

Compare SBI, HDFC and ICICI dollar rates in one API call

If you search for an "SBI dollar rate API" you mostly find scrapers that broke last year. Frankfurter has India's official reference rate (through FBIL), which is the right number for accounts. It doesn't carry bank card rates.

Here is how to get the card rates of 52 Indian banks, including SBI, HDFC, ICICI, Axis and Bank of Baroda, in one request.

The call

import os, requests

r = requests.get(
    "https://allratestoday.com/api/v1/bank-rates/in",
    headers={"Authorization": f"Bearer {os.environ['ART_KEY']}"},
)
data = r.json()

rows = []
for bank in data["banks"]:
    usd = next((x for x in bank["rates"] if x["ccy"] == "USD"), None)
    if usd and usd.get("buy"):
        rows.append((bank["bank"], usd["buy"], usd["sell"]))

best_buy = max(rows, key=lambda x: x[1])
best_sell = min(rows, key=lambda x: x[2])
print("Best bank to sell USD to:", best_buy)
print("Cheapest bank to buy USD from:", best_sell)
Enter fullscreen mode Exit fullscreen mode

What you get per bank

  • buy / sell: the telegraphic transfer (TT) pair, which is what an inbound remittance converts at
  • mid and spread_pct
  • official: the RBI reference rate, so you can see how far each bank sits from it

For a single bank, narrow it down:

curl "https://allratestoday.com/api/v1/bank-rates/in/sbi?ccy=USD" \
  -H "Authorization: Bearer YOUR_KEY"
Enter fullscreen mode Exit fullscreen mode

A note on SBI

Rule 115 of the Income-tax Rules uses SBI's TT buying rate, so SBI's history is worth having. It is stored daily back to January 2020, with history endpoints on paid plans.

When to use Frankfurter instead

If you only need India's daily reference rate for bookkeeping, Frankfurter's FBIL source is free and fine. Bank rates are the part it doesn't cover.


Top comments (0)