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=["FXERD09", "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

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.0.tar.gz (29.8 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.0-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: boj_api-0.1.0.tar.gz
  • Upload date:
  • Size: 29.8 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.0.tar.gz
Algorithm Hash digest
SHA256 5314f5c34c5cfb3997d79dd15f8c1b1b95aebe9c011890e334a919bf119f205b
MD5 507a83985a7d0f7fc851ea49739c1f26
BLAKE2b-256 bed800717f3ea591a081806f9081d8b9f302989d1251ec77c634b6bd55a30133

See more details on using hashes here.

File details

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

File metadata

  • Download URL: boj_api-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f94ba9d2e49f703fbc724cbc9c82e3928a1ee0a32ba2497cebf2ec216910515
MD5 b63fb947f02fa7251bf289cc1e53e1d2
BLAKE2b-256 12b2261702b4906017f385cf46c93c212eb90e5846f6d0c80ddb46ccb210c22d

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