Skip to main content

A production-ready Python client for the Bank of Japan (BOJ) Time-Series Statistics Search Site API.

Project description

boj-api

A production-ready Python client for the Bank of Japan (BOJ) Time-Series Statistics Search Site API.

This library provides a clean, typed, and well-documented interface to all three BOJ statistical data APIs:

API Method Description
Code API get_data_by_code() Fetch time-series data by series codes
Layer API get_data_by_layer() Fetch time-series data by hierarchy/layer
Metadata API get_metadata() Fetch database metadata (series names, units, layers, etc.)

Credit: This library uses the Bank of Japan Time-Series Statistics Search Site API. The content is not guaranteed by the Bank of Japan.

「このサービスは、日本銀行時系列統計データ検索サイトのAPI機能を使用しています。 サービスの内容は日本銀行によって保証されたものではありません。」

Installation

pip install boj-api

Quick Start

from boj_api import BOJClient, Database, Frequency

client = BOJClient()

# Fetch exchange rate data (Code API)
response = client.get_data_by_code(
    db=Database.FM08,
    code=["FXERD01", "FXERD02"],
    start_date="202401",
    end_date="202412",
)

for series in response.series:
    print(f"{series.name_jp}: {len(series.observations)} observations")

# Fetch data by layer hierarchy (Layer API)
response = client.get_data_by_layer(
    db=Database.BP01,
    frequency=Frequency.MONTHLY,
    layer=[1, 1, 1],
    start_date="202504",
    end_date="202509",
)

# Fetch metadata for a database (Metadata API)
metadata = client.get_metadata(db=Database.FM08)
for entry in metadata.entries:
    print(f"{entry.series_code}: {entry.name_jp}")

Auto-Pagination

The BOJ API limits results to 250 series / 60,000 data points per request. BOJClient handles pagination automatically:

# Automatically follows NEXTPOSITION until all data is fetched
all_data = client.get_data_by_code(
    db=Database.CO,
    code=series_codes,  # can exceed 250
    auto_paginate=True,
)

Databases

All BOJ database identifiers are available as the Database enum:

Enum DB Name Description
Database.IR01 IR01 Base discount/loan rates
Database.FM01 FM01 Uncollateralized overnight call rate
Database.FM08 FM08 Foreign exchange rates
Database.CO CO Tankan survey
Database.FF FF Flow of funds
... ... See boj_api.enums.Database for full list

Frequency Types

Enum Code Description
Frequency.CALENDAR_YEAR CY Calendar year
Frequency.FISCAL_YEAR FY Fiscal year
Frequency.SEMI_ANNUAL_CY CH Semi-annual (calendar)
Frequency.SEMI_ANNUAL_FY FH Semi-annual (fiscal)
Frequency.QUARTERLY Q Quarterly
Frequency.MONTHLY M Monthly
Frequency.WEEKLY W Weekly
Frequency.DAILY D Daily

Date Formats

The start_date / end_date format depends on the frequency of the series:

Frequency Format Example
Daily, Monthly, Quarterly, Semi-annual YYYYMM "202401"
Calendar year, Fiscal year YYYY "2024"

Note: YYYYMMDD (8-digit) dates are never accepted by the API.

API Reference

BOJClient

BOJClient(
    lang: Language = Language.JP,
    format: Format = Format.JSON,
    timeout: float = 30.0,
    max_retries: int = 3,
    retry_delay: float = 1.0,
)

Error Handling

from boj_api import BOJClient, BOJAPIError, InvalidParameterError

client = BOJClient()
try:
    data = client.get_data_by_code(db="INVALID", code=["X"])
except InvalidParameterError as e:
    print(f"Bad request: {e.message_id} - {e.message}")
except BOJAPIError as e:
    print(f"API error: {e}")

Development

# Clone the repository
git clone https://github.com/YenAle-FT-Gmail/BOJ_API_Python.git
cd BOJ_API_Python

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

# Lint & format
ruff check src/ tests/
ruff format src/ tests/

# Type check
mypy src/

License

MIT License. See LICENSE for details.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

boj_api-0.1.2.tar.gz (35.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boj_api-0.1.2-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file boj_api-0.1.2.tar.gz.

File metadata

  • Download URL: boj_api-0.1.2.tar.gz
  • Upload date:
  • Size: 35.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for boj_api-0.1.2.tar.gz
Algorithm Hash digest
SHA256 47c93501e681093edf3d9813bb2810c9275cab19428d3cc27a31c0f186557591
MD5 745d99faefaa644bf99246eec28dc05c
BLAKE2b-256 85249abe53a1cd82849dc5ac4e76919c66251d58c23dd8a1c25300a3c2386cc7

See more details on using hashes here.

File details

Details for the file boj_api-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: boj_api-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for boj_api-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab1c85f8cb7333d98c752652b42c9e1cf2bc9c8916bbc98cc339e447747998f6
MD5 a174c7f5ab9bd7577275fcc1b09ef82e
BLAKE2b-256 2fe7a77e7da41a7ca3c5d0cb7938aa378ddff63d0966fa134bf6812fe18c1e6a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page