Tutorial: Download mutual funds NAV data using python

In this article, we will learn how to download mutual funds NAV(Net Asset Value) data using python.


This article assumes your already familiar with the basics of python and APIs(Application Programming Interface). Before you get started with the tutorial, make sure python is already installed on your system.

Table of Content

– Setup the project – Import the libraries – Getting to know the API – Downloading the Data – Conclusion

Setup the project

Let's create a new folder where we will be working.

mkdir mutual-fund-data
cd mutual-fund-data

Create a new python virtual environment and activate it. Run the following the command inside the mutual-fund-data folder.

# create a virtual environment
python -m venv .
# activate the virtual environemnt
.\Scripts\activate # If using windows

Let's install the required packages

python -m pip install requests

requests is a simple HTTP library for python used for interacting with APIs. It is one of the simplest to get started with.

Import the libraries

Create a new file named mf-data.py and import the packages

import requests

Getting to know the API

Before we go fetch the data, Let's have a look at our API which we will be using.

mfapi is a free indian mutual fund api which provides complete history of mutual funds data over a json API for free.

There are 3 main endpoints in the API

  1. List all Mutual Funds – https://api.mfapi.in/mf
  2. Fetch historical NAV for a particular Scheme. – https://api.mfapi.in/mf/{scheme-code}
  3. Search the Mutual Funds List by Name – https://api.mfapi.in/mf/search?q={search-term}

So, for fetching the historical NAV for a particular mutual fund scheme, we would need the scheme code which we can get either from the List all mutual funds endpoint or by using the search mutual funds by name endpoint.

Downloading the data

Let's now get the data for a mutual fund. I will be fetching the data for UTI – NIFTY Index Fund- Regular Plan – Growth Option whose scheme code is 100822.

# API endpoint
url = "https://api.mfapi.in/mf/100822"
# make the api request
response = requests.get(url)
# Check if request is success
if response.status_code == requests.codes.ok:
    # parse the response as json
    data = response.json()
    # display the data

Now from the terminal, you can run the script to see the data on the console. NOTE: The API returns the entire history of the scheme and doesn't support the filtering of data by timestamp. so the data will huge and you can write it to a file instead of printing it to the console.


Thats' it. we are done. you can go ahead and fetch the data for all the mutual funds the API provides.

Here's the full code -

# File Name: mf-data.py

import requests

url = "https://api.mfapi.in/mf/100822"
response = requests.get(url=url)
if response.status_code == requests.codes.ok:
    data = response.json()
    print(f"Fetching Data Failed with Status Code - {response.status_code}")

References: – https://www.mfapi.in/https://docs.python-requests.org/en/latest