DEV Community

Alex Spinov
Alex Spinov

Posted on

World Bank Has a Free API — GDP, Population, and Economic Data for Every Country

Need GDP data for 200+ countries? Population growth rates? Poverty statistics? Inflation trends?

The World Bank has been collecting economic data since 1960. And it is all available through a free API. No key. No signup.

What Data Is Available?

  • GDP, GNI, inflation for every country since 1960
  • Population, life expectancy, mortality rates
  • Education, literacy, school enrollment statistics
  • Trade, exports, imports data
  • Poverty headcount ratios
  • 16,000+ indicators across 200+ countries

Quick Start

import requests

# GDP of the United States (last 5 years)
response = requests.get(
    "https://api.worldbank.org/v2/country/US/indicator/NY.GDP.MKTP.CD",
    params={"format": "json", "per_page": 5, "date": "2019:2023"}
)

data = response.json()[1]
for entry in data:
    year = entry["date"]
    gdp = entry["value"]
    if gdp:
        print(f"{year}: ${gdp/1e12:.2f} trillion")
Enter fullscreen mode Exit fullscreen mode

Output:

2023: $27.36 trillion
2022: $25.46 trillion
2021: $23.32 trillion
2020: $21.06 trillion
2019: $21.43 trillion
Enter fullscreen mode Exit fullscreen mode

Compare Countries

countries = ["US", "CN", "IN", "DE", "JP"]
indicator = "NY.GDP.MKTP.CD"  # GDP

for code in countries:
    resp = requests.get(
        f"https://api.worldbank.org/v2/country/{code}/indicator/{indicator}",
        params={"format": "json", "per_page": 1, "date": "2023"}
    )
    data = resp.json()[1]
    if data and data[0]["value"]:
        name = data[0]["country"]["value"]
        gdp = data[0]["value"]
        print(f"{name:>20}: ${gdp/1e12:.2f}T")
Enter fullscreen mode Exit fullscreen mode

Population Trends

# World population over time
resp = requests.get(
    "https://api.worldbank.org/v2/country/WLD/indicator/SP.POP.TOTL",
    params={"format": "json", "per_page": 10, "date": "2014:2023"}
)

for entry in sorted(resp.json()[1], key=lambda x: x["date"]):
    year = entry["date"]
    pop = entry["value"]
    if pop:
        bar = "#" * int(pop / 1e9)
        print(f"{year}: {pop/1e9:.2f}B {bar}")
Enter fullscreen mode Exit fullscreen mode

Useful Indicators

Code What it measures
NY.GDP.MKTP.CD GDP (current US$)
SP.POP.TOTL Total population
SP.DYN.LE00.IN Life expectancy
SE.ADT.LITR.ZS Literacy rate
FP.CPI.TOTL.ZG Inflation (CPI)
SI.POV.DDAY Poverty ($2.15/day)
SL.UEM.TOTL.ZS Unemployment rate
NY.GDP.PCAP.CD GDP per capita

Full list: data.worldbank.org/indicator

Pro Tips

  1. Use WLD as country code for world aggregates
  2. Date ranges: date=2010:2023 for multiple years
  3. Multiple countries: country/US;CN;IN/indicator/...
  4. The API also has regional aggregates (EAS, ECS, etc.)
  5. Combine with Plotly for instant visualizations

Free data APIs: OpenAlex (academic) | Crossref (DOIs) | World Bank (economics)

What economic data do you need? Let me know what indicator you want me to cover next.

More API tools: GitHub\n\n---\n\n## More Free Research APIs\n\nThis is part of my series on free APIs for researchers and data scientists:\n\n- OpenAlex API — 250M+ Academic Works\n- CORE API — 260M+ Scientific Papers\n- Crossref API — DOI Metadata for 150M+ Papers\n- Unpaywall API — Find Free Paper Versions\n- Europe PMC — 40M+ Biomedical Papers\n- World Bank API — GDP & Economic Data\n- ORCID API — 18M+ Researcher Profiles\n- DBLP API — 6M+ CS Publications\n- NASA APIs — 20+ Free Space Data APIs\n- FRED API — 800K+ US Economic Time Series\n- All 30+ Research APIs Mapped\n\n*Tools: Academic Research Toolkit on GitHub*

Top comments (0)