NewConnect Claude, Cursor, ChatGPT, and other AI agents to API Ninjas via our MCP server

How to Get Public Holidays in Python

Holidays APIUpdated May 23, 2026
To get public holidays in Python, send a GET request to the API Ninjas Holidays endpoint with a country code and year:
holidays.py
import requests

API_KEY = 'YOUR_API_KEY'
response = requests.get(
    'https://api.api-ninjas.com/v1/holidays',
    params={'country': 'US', 'year': 2026},
    headers={'X-Api-Key': API_KEY},
)
print(response.json())

Prerequisites

Step 1: Get your API key

Sign up at api-ninjas.com/register.

Step 2: Make the API request

holidays.py
import requests

API_KEY = 'YOUR_API_KEY'
response = requests.get(
    'https://api.api-ninjas.com/v1/holidays',
    params={'country': 'US', 'year': 2026},
    headers={'X-Api-Key': API_KEY},
)
print(response.json())

Step 3: Use the response

JSON
[
  {
    "country": "United States",
    "iso": "US",
    "year": 2026,
    "date": "2026-01-01",
    "day": "Thursday",
    "name": "New Year's Day",
    "type": "federal_holiday"
  },
  {
    "country": "United States",
    "iso": "US",
    "year": 2026,
    "date": "2026-07-04",
    "day": "Saturday",
    "name": "Independence Day",
    "type": "federal_holiday"
  }
]

Complete example

holidays.py
import os
import requests
from datetime import date

API_KEY = os.environ['API_NINJAS_KEY']
URL = 'https://api.api-ninjas.com/v1/holidays'


def get_holidays(country: str, year: int, kind: str | None = None) -> list:
    params = {'country': country, 'year': year}
    if kind:
        params['type'] = kind
    response = requests.get(
        URL,
        params=params,
        headers={'X-Api-Key': API_KEY},
        timeout=10,
    )
    response.raise_for_status()
    return response.json()


if __name__ == '__main__':
    holidays = get_holidays('US', 2026)
    for h in holidays:
        print(f"{h['date']}  {h['day']:9}  {h['name']}")

Finding the next upcoming holidays

Python
from datetime import date

today = date.today()
holidays = get_holidays('US', today.year)
upcoming = [h for h in holidays if date.fromisoformat(h['date']) >= today]

print(f"Next {min(5, len(upcoming))} holidays:")
for h in upcoming[:5]:
    print(f"  {h['date']}  {h['name']}")

Filtering by holiday type

Pass a type parameter to get only federal holidays, observances, or seasonal events:

Python
# Filter to a specific holiday type — e.g., federal_holiday, observance, season
federal = get_holidays('US', 2026, kind='federal_holiday')
for h in federal:
    print(f"{h['date']}  {h['name']}")

Holidays across multiple countries

Python
countries = ['US', 'CA', 'GB', 'AU', 'JP']
year = 2026

for country in countries:
    holidays = get_holidays(country, year)
    print(f"{country}: {len(holidays)} holidays in {year}")

Calculating business days

Combine holidays with weekday filtering to count business days in a date range — useful for SLA calculations or invoicing:

Python
from datetime import date, timedelta

def is_business_day(day: date, holiday_dates: set[date]) -> bool:
    return day.weekday() < 5 and day not in holiday_dates

holidays = get_holidays('US', 2026)
holiday_dates = {date.fromisoformat(h['date']) for h in holidays}

# Count business days in March 2026
start = date(2026, 3, 1)
end = date(2026, 3, 31)
days = [start + timedelta(d) for d in range((end - start).days + 1)]
business_days = sum(1 for d in days if is_business_day(d, holiday_dates))
print(f"Business days in March 2026: {business_days}")

For workday and bank-closure questions use Working Days. For exchange-specific schedules see Stock Exchange (includes stockexchangeholidays).

Frequently asked questions

What country codes does the API use?

ISO 3166-1 alpha-2 two-letter country codes — for example, US, CA, GB, JP, AU, DE.

What years can I query?

The API covers current and recent past years, plus several future years for planning. Use the year parameter as a four-digit integer.

Does the API distinguish between federal and observed holidays?

Yes. The type field identifies each holiday — for example, federal_holiday, public_holiday, observance, or season. Use the type query parameter to filter to a single category.

Can I get holidays for multiple countries in one call?

No. The endpoint returns one country per request. Loop over your country list to assemble a multi-country view.

Is there a free tier?

Yes. Every API Ninjas account includes a free tier — no credit card required.