Skip to content

About

MFAPI

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

74 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MF Data API

Free, open JSON API for Indian mutual fund NAV data β€” a self-hostable, transparent implementation of the familiar mfapi.in-style endpoints, built from the official AMFI India NAV feeds.

  • ~14,000 mutual fund schemes, all AMCs and categories
  • Full historical NAV series with scheme metadata and ISINs
  • No authentication, no API keys, JSON responses
  • Daily ingestion via GitHub Actions (twice a day, IST)
  • Live FastAPI service and a static JSON export for GitHub Pages / any CDN
  • Documentation site, interactive API explorer, status page, Swagger UI
  • MIT-licensed code; Docker-ready

This project is independent and is not affiliated with AMFI, SEBI, any AMC or mfapi.in. NAV data remains subject to AMFI's and the AMCs' terms.


Contents


Architecture

AMFI India                         MF Data API repository
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ NAVAll.txt           β”‚          β”‚  pipeline/                                β”‚
β”‚ (latest NAVs)        β”‚  daily   β”‚    download β†’ parse β†’ SQLite β†’ JSON       β”‚
β”‚                      β”‚ ───────► β”‚                                           β”‚
β”‚ NAVHistoryReport     β”‚          β”‚  data/mfdata.db  (indexed, WAL)           β”‚
β”‚ (historical, 30-day  β”‚          β”‚  public/api/v1/  (static JSON export)     β”‚
β”‚  chunks)             β”‚          β”‚                                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚  app/  FastAPI  ──►  /api/v1/*            β”‚
                                  β”‚  web/  docs, explorer, status             β”‚
                                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚
                              GitHub Actions ─┴─► GitHub Pages (static JSON)

The pipeline stores everything in SQLite for fast querying. The same rows are exported as per-scheme JSON files, so the project works with or without a server. The front-end automatically switches between live-API mode and static mode.


Quick start

Docker

docker compose up -d --build

On first boot the container downloads the latest NAV file plus 365 days of history (configurable). The database is kept in the mfdata volume.

Local Python

python -m venv .venv
# Windows: .venv\Scripts\activate
source .venv/bin/activate
pip install -r requirements.txt

# 1. Download AMFI data (first run backfills 3 years; use a smaller number for a quick demo)
python -m pipeline sync --bootstrap-days 1095

# 2. Optional: export the static JSON tree
python -m pipeline export --out public

# 3. Run the API + documentation site
python -m pipeline serve --port 8000

Open http://localhost:8000. The API is also mounted without the version prefix for compatibility: /mf/125497/latest.

Useful pipeline commands

python -m pipeline sync --bootstrap-days 30   # small first run
python -m pipeline sync --full                # force a full backfill
python -m pipeline sync --lookback-days 14    # re-check two weeks of corrections
python -m pipeline status                     # counts, freshness, integrity
python -m pipeline export --out public        # static JSON tree
python -m pipeline serve --reload             # development server

API

Base URL: /api/v1 (root-level aliases also work).

Method Path Description
GET /mf Scheme list; limit, offset, q, fund_house, category
GET /mf/search?q= Case-insensitive search, prefix-ranked
GET /mf/{code} Metadata + full NAV history, newest first
GET /mf/{code}/latest Latest NAV for one scheme
GET /mf/latest Latest NAV for every scheme
GET /mf/{code}/meta Scheme metadata only
GET /fund-houses Fund houses with scheme counts
GET /fund-houses/{name}/schemes Schemes for a fund house
GET /categories Scheme types/categories with counts
GET /nav/{date} All NAVs published on a date
GET /dates Recent NAV dates
GET /stats Dataset counts and freshness
GET /health Health check

Date inputs accept YYYY-MM-DD, DD-MM-YYYY or DD-Mon-YYYY. Response dates use DD-MM-YYYY; NAVs are fixed-point strings with five decimals.

Examples

curl "http://localhost:8000/api/v1/mf/125497/latest"
curl "http://localhost:8000/api/v1/mf/125497?startDate=2025-01-01&endDate=2025-12-31"
curl "http://localhost:8000/api/v1/mf/search?q=hdfc&limit=5"
curl "http://localhost:8000/api/v1/mf?fund_house=SBI%20Mutual%20Fund"
curl "http://localhost:8000/api/v1/nav/20-11-2026?limit=100"
{
  "meta": {
    "fund_house": "SBI Mutual Fund",
    "scheme_type": "Open Ended Schemes",
    "scheme_category": "Equity Scheme - Small Cap Fund",
    "scheme_code": 125497,
    "scheme_name": "SBI Small Cap Fund - Direct Plan - Growth",
    "isin_growth": "INF200K01T51",
    "isin_div_reinvestment": null
  },
  "data": [{ "date": "20-11-2026", "nav": "210.02130" }],
  "status": "SUCCESS"
}

Full documentation is served at /docs, and the generated OpenAPI schema is at /api/openapi.json. Swagger UI is at /api/docs.

Rate limiting: 120 requests/minute per IP by default (MFDATA_RATE_LIMIT, MFDATA_RATE_WINDOW). CORS is open by default.


Data pipeline

pipeline/amfi.py downloads and parses:

  1. Latest NAVs β€” https://portal.amfiindia.com/spages/NAVAll.txt
  2. Historical NAVs β€” https://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx in 30-day chunks (AMFI caps a request at 90 days)

Rows are normalised, de-duplicated on (scheme_code, date) and upserted into SQLite:

  • schemes β€” code, name, fund house, type, category, ISINs, latest NAV
  • nav_history β€” (scheme_code, date) β†’ nav, indexed by date
  • meta β€” last sync, dataset dates, counts

The first run backfills MFDATA_BOOTSTRAP_DAYS (default 1,095 days). Later runs re-download a small lookback window (default 7 days) to pick up corrections, plus the complete latest NAV file.

pipeline/export_static.py writes the static API tree:

public/api/v1/
β”œβ”€β”€ mf.json                         # all schemes
β”œβ”€β”€ mf/latest.json                  # latest NAV for every scheme
β”œβ”€β”€ mf/125497.json                  # full history
β”œβ”€β”€ mf/125497/latest.json           # latest for one scheme
β”œβ”€β”€ fund-houses.json
β”œβ”€β”€ categories.json
β”œβ”€β”€ dates.json
β”œβ”€β”€ stats.json
β”œβ”€β”€ meta.json
└── index.json

Raw NAVAll snapshots are gzipped into data/raw/ with a configurable retention window (default 90 days) for auditing.


Daily GitHub Actions workflow

.github/workflows/update-data.yml runs at 09:45 IST and 22:15 IST (plus manual dispatch) and:

  1. Restores the SQLite database from the Actions cache.
  2. Installs dependencies and runs python -m pipeline sync.
  3. Exports the static API and copies the documentation site.
  4. Saves the database back to the cache and commits data/status.json.
  5. Uploads the public/ artifact and deploys GitHub Pages.

One-time repository setup

  1. Push this repository to GitHub.
  2. Settings β†’ Pages β†’ Build and deployment β†’ Source: GitHub Actions.
  3. Actions β†’ Update NAV data β†’ Run workflow for the first run. A full three-year backfill can take a while and downloads several hundred MB from AMFI; later runs are incremental.
  4. Optional: protect the main branch but allow the github-actions[bot] to push the status commit (or remove the commit step if you prefer).

The workflow needs the default GITHUB_TOKEN permissions declared in the file (contents: write, pages: write, id-token: write).

If the database cache is evicted, the next run automatically performs a full bootstrap again.


Static JSON mode (GitHub Pages)

scripts/build_site.py copies web/ into public/ and switches assets/config.js to static mode. The same site then works without any server:

Live API Static file
/api/v1/mf /api/v1/mf.json
/api/v1/mf/search?q= filter /api/v1/mf.json client-side
/api/v1/mf/{code} /api/v1/mf/{code}.json
/api/v1/mf/{code}/latest /api/v1/mf/{code}/latest.json
/api/v1/mf/latest /api/v1/mf/latest.json
/api/v1/fund-houses /api/v1/fund-houses.json
/api/v1/categories /api/v1/categories.json
/api/v1/stats /api/v1/stats.json
/api/v1/nav/{date} not available (use per-scheme files)

The documentation site, search, explorer (with client-side date filtering) and status page all adapt automatically.

For a local static build:

python -m pipeline export --out public
python scripts/build_site.py --out public
python -m http.server 8080 --directory public

Static exports of full history can be large (hundreds of MB for three years). Use python -m pipeline export --out public --history-days 365 if you need a smaller tree.


Deployment

Docker / VPS

docker compose up -d --build

Put Nginx/Caddy in front for TLS. Schedule a daily sync on the host:

docker compose exec api python -m pipeline sync

or run the included GitHub Actions workflow and pull the updated database.

Render / Railway / Fly.io

  • Build: pip install -r requirements.txt
  • Start: uvicorn app.main:app --host 0.0.0.0 --port $PORT
  • Persistent disk mounted at /data; set MFDATA_DATA_DIR=/data
  • Run python -m pipeline sync --bootstrap-days 365 once (job or shell), or set MFDATA_AUTO_SYNC=1 and use the Docker entrypoint.

Static-only (GitHub Pages / S3 / Netlify)

Run the export + build steps and publish public/. No Python runtime is needed at request time.


Configuration

Variable Default Purpose
MFDATA_DATA_DIR ./data Database and raw snapshots
MFDATA_DB data/mfdata.db Explicit SQLite path
MFDATA_BOOTSTRAP_DAYS 1095 History depth for a fresh DB
MFDATA_INCREMENTAL_DAYS 7 Correction window on incremental runs
MFDATA_CHUNK_DAYS 30 History download chunk size (max 90)
MFDATA_RAW_RETENTION_DAYS 90 Keep compressed NAVAll snapshots for N days
MFDATA_RATE_LIMIT 120 Requests per window; 0 disables
MFDATA_RATE_WINDOW 60 Window in seconds
MFDATA_CACHE_TTL 300 Default Cache-Control max-age for API responses
MFDATA_CORS_ORIGINS * Comma-separated allowed origins
MFDATA_AUTO_SYNC 1 Docker: sync automatically when DB is missing
MFDATA_TITLE / MFDATA_VERSION MF Data API / 1.0.0 Branding

Copy .env.example to .env for local use (the app reads environment variables; it does not load .env automatically).


Project structure

.
β”œβ”€β”€ app/                    FastAPI application
β”‚   β”œβ”€β”€ main.py             app factory, middleware, static site
β”‚   β”œβ”€β”€ routes.py           all API endpoints
β”‚   β”œβ”€β”€ schemas.py          Pydantic/OpenAPI models
β”‚   β”œβ”€β”€ middleware.py       rate limit + cache headers
β”‚   β”œβ”€β”€ config.py           runtime settings
β”‚   └── deps.py             errors and dependencies
β”œβ”€β”€ pipeline/               AMFI ingestion and storage
β”‚   β”œβ”€β”€ amfi.py             download + parse NAVAll/history feeds
β”‚   β”œβ”€β”€ store.py            SQLite schema, upserts, queries
β”‚   β”œβ”€β”€ sync.py             daily sync orchestration
β”‚   β”œβ”€β”€ export_static.py    static JSON export
β”‚   β”œβ”€β”€ config.py           pipeline settings
β”‚   β”œβ”€β”€ dates.py            date helpers
β”‚   └── __main__.py         `python -m pipeline` CLI
β”œβ”€β”€ web/                    documentation site (no build step)
β”‚   β”œβ”€β”€ index.html          landing page
β”‚   β”œβ”€β”€ docs.html           full API documentation
β”‚   β”œβ”€β”€ explorer.html       interactive request builder
β”‚   β”œβ”€β”€ status.html         dataset/status page
β”‚   └── assets/             CSS, JS, config, favicon
β”œβ”€β”€ scripts/build_site.py   copy site + static config for Pages
β”œβ”€β”€ docker/entrypoint.sh    Docker entrypoint with optional first sync
β”œβ”€β”€ tests/                  parser, store, API, export tests
β”œβ”€β”€ .github/workflows/      daily data update + CI
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
└── requirements.txt

Development

pip install -r requirements-dev.txt
ruff check app pipeline tests scripts
pytest -q

Tests use small AMFI fixtures, so they run offline and do not touch the network.

The web UI has no build step β€” plain HTML/CSS/JS. Edit files in web/ and reload. When served by FastAPI, /docs, /explorer, /status, /privacy and /terms map to the corresponding HTML files.


Data source & attribution

AMFI is the official source of Indian mutual fund NAVs. Scheme names, categories, ISINs and NAV values are reproduced as published. The project is not affiliated with AMFI, SEBI or any asset management company. Verify data against the official source before using it for financial decisions.

License

Code: MIT. Data: subject to AMFI's and the AMCs' terms; attribution to AMFI India is required when redistributing.

Built by PeaBox Labs

About

MFAPI

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages