Python script to fetch Bank details from IFSC | Daily Python #9
This article is a tutorial on fetching Bank details from IFSC using Python and Razorpay IFSC Toolkit.
This article is a part of Daily Python challenge that I have taken up for myself. I will be writing short python articles daily.
Requirements:
- Python 3.0
- Pip
Install the following packages:
- requests— Library to making HTTP requests.
pip install requests
Let’s import the requests library
import requests
First, let’s visit the Razorpay IFSC Toolkit and see how to make requests to its API for fetching bank details.
Let’s define a class in Python that can fetch this JSON response for a given IFSC code and make these fields accessible using function calls.
class BankDetails:
'''
class to fetch the response of one Bank and
make it accessible through functions
'''
def __init__(self,IFSC_Code):
URL = "https://ifsc.razorpay.com/"
self.result = requests.get(URL+IFSC_Code).json()
def getBankName(self):
bankName = self.result['BANK']
print('Bank Name : ',bankName)
return bankNamedef getBankContact(self):
bankContact = self.result['CONTACT']
print('Bank Contact : ',bankContact)
return bankContactdef getBankAddress(self):
bankAddr = self.result['ADDRESS']
print('Bank Address : ',bankAddr)
return bankAddr
def getBankBranch(self):
bankBranch = self.result['BRANCH']
print('Bank Branch : ',bankBranch)
return bankBranchdef getBankState(self):
bankState = self.result['STATE']
print('Bank State : ',bankState)
return bankStatedef getBankCity(self):
bankCity = self.result['CITY']
print('Bank City : ',bankCity)
return bankCity
The above class requires a parameter that is the IFSC code while instantiating its object. The function calls make it easy to access the details of the particular Bank. Let’s create an object and see the working of the above code.
IFSC_Code = 'MAHB0001716'
bank = BankDetails(IFSC_Code)
bank.getBankName()
bank.getBankContact()
bank.getBankBranch()
bank.getBankAddress()
bank.getBankCity()
bank.getBankState()
I hope this article was helpful, do leave some claps if you liked it.