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

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.1.tar.gz (34.7 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.1-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: boj_api-0.1.1.tar.gz
  • Upload date:
  • Size: 34.7 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.1.tar.gz
Algorithm Hash digest
SHA256 c6644ca3b1b9fdbb163d464f9348ae258de97c567f80ef9d6a9a71cd0284253e
MD5 519ffac93c17020640dfcf28f07773f4
BLAKE2b-256 c0590f2117478aa0436f2b5424cdbd1830a9613c0933e0fa156edb6911d418f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: boj_api-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 19bf135366d99657cc654773f5e001ab0eaa28f5f48c2dcda43c8fc51d0d12a7
MD5 c571a342adc00d748914963f09df39a4
BLAKE2b-256 24a1f9c034d7283905106a17ae549eff87163dba84235b78d2831c643420ad5a

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