Skip to main content

Python client for Bank of Japan timeseries statistics API

Project description

boj-api-client

日本銀行「時系列統計データ検索サイト API」(https://www.stat-search.boj.or.jp/api/v1)向けの Python クライアントです。

主な特徴

  • 同期/非同期クライアント
    • BojClient
    • AsyncBojClient
  • 通信フォーマットは JSON 固定(format=json
  • 型付き Query/Response モデル
  • getDataCode の 250 件超を自動分割して統合
  • NEXTPOSITION を使った自動ページング
  • リトライ/スロットリング
  • 途中失敗時の partial result + checkpoint 再開

対応 API

  • getDataCode
  • getDataLayer
  • getMetadata

要件

  • Python 3.11+

インストール

通常利用(PyPI):

pip install boj-api-client

2026-02-20 時点で、boj-api-client==0.1.0pip install boj-api-client で導入し、 以下の API 呼び出しでデータ取得できることを確認済みです。

  • getMetadatadb="FM08"
  • getDataCodedb="CO", code=["TK99F1000601GCQ01000"]

ローカル開発環境で使う場合:

pip install -e .

または

uv pip install -e .

クイックスタート(同期)

from boj_api_client import BojClient
from boj_api_client.timeseries import DataCodeQuery

with BojClient() as client:
    result = client.timeseries.get_data_code(
        DataCodeQuery(
            db="CO",
            code=["TK99F1000601GCQ01000"],
            lang="JP",
        )
    )

for series in result.series:
    print(series.series_code, len(series.points))

クイックスタート(非同期)

import asyncio

from boj_api_client import AsyncBojClient
from boj_api_client.timeseries import MetadataQuery


async def main() -> None:
    async with AsyncBojClient() as client:
        result = await client.timeseries.get_metadata(
            MetadataQuery(db="FM08", lang="JP")
        )
    print(result.envelope.status, len(result.entries))


asyncio.run(main())

ページ単位で取得する(iter_*)

from boj_api_client import BojClient
from boj_api_client.timeseries import DataLayerQuery

with BojClient() as client:
    for page in client.timeseries.iter_data_layer(
        DataLayerQuery(db="MD10", frequency="Q", layer1="*")
    ):
        print(page.envelope.status, len(page.series))

getDataLayer の auto-partition を有効化する

getDataLayer が 1,250 系列上限に達したとき、metadata 経由の fallback を使う設定です。

from boj_api_client import BojClient, BojClientConfig
from boj_api_client.config import TimeSeriesConfig
from boj_api_client.timeseries import DataLayerQuery

config = BojClientConfig(
    timeseries=TimeSeriesConfig(enable_layer_auto_partition=True),
)

with BojClient(config=config) as client:
    result = client.timeseries.get_data_layer(
        DataLayerQuery(db="MD10", frequency="Q", layer1="*", lang="JP")
    )

partial result から再開する

from boj_api_client import BojClient
from boj_api_client.core.errors import BojPartialResultError
from boj_api_client.timeseries import DataCodeQuery

query = DataCodeQuery(db="CO", code=["TK99F1000601GCQ01000"], lang="JP")

with BojClient() as client:
    try:
        result = client.timeseries.get_data_code(query)
    except BojPartialResultError as exc:
        if exc.checkpoint_id is None:
            raise
        result = client.timeseries.get_data_code(
            query,
            checkpoint_id=exc.checkpoint_id,
        )

設定

from boj_api_client import BojClient, BojClientConfig
from boj_api_client.config import (
    CheckpointConfig,
    RetryConfig,
    ThrottlingConfig,
    TimeSeriesConfig,
    TransportConfig,
)

config = BojClientConfig(
    transport=TransportConfig(timeout_read_seconds=20.0),
    retry=RetryConfig(max_attempts=3, max_backoff_seconds=5.0),
    throttling=ThrottlingConfig(min_wait_interval_seconds=1.0),
    checkpoint=CheckpointConfig(enabled=True, ttl_seconds=86400.0),
    timeseries=TimeSeriesConfig(enable_layer_auto_partition=False),
)

with BojClient(config=config) as client:
    ...

主な例外

  • BojValidationError
  • BojServerError
  • BojUnavailableError
  • BojTransportError
  • BojProtocolError
  • BojPartialResultError
  • BojClientClosedError

live contract test

BOJ_RUN_LIVE=1 pytest -m live -q tests/contract_live

getDataLayer live テストを実行する場合:

  • BOJ_LIVE_LAYER1(必須)
  • BOJ_LIVE_LAYER_DB(任意、既定 MD10
  • BOJ_LIVE_LAYER_FREQUENCY(任意、既定 Q

ドキュメント

  • パッケージ構成と責務: docs/architecture.md
  • API 仕様の実装対応: docs/api_overview.md

開発者向け

  • sync orchestrator は async source から生成:
    • uv run --extra dev python scripts/generate_sync_orchestrator.py
  • PyPI 公開:
    • 初回のみ、PyPI 側で Trusted Publisher に GitHub Actions を登録
    • pyproject.tomlproject.version を更新後、v<version> タグを push
    • GitHub Actions (.github/workflows/ci.yml) がテスト成功後に自動 publish

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_client-0.1.1.tar.gz (33.4 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_client-0.1.1-py3-none-any.whl (47.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: boj_api_client-0.1.1.tar.gz
  • Upload date:
  • Size: 33.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for boj_api_client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 838f443bc81967dc9f0ec80e8cfa0582bc6cee82df8b3fa72e920cefd1bad445
MD5 7311f6bce5455b3b29bd7c6d7c0e4f13
BLAKE2b-256 e248b2a4a2a73645be3d2e381c4f8037365544d7b4c8b8f19fb69c0b0c2e5a9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for boj_api_client-0.1.1.tar.gz:

Publisher: ci.yml on delihiros/boj-api-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: boj_api_client-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 47.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for boj_api_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40d73729bb434f0a15bd1c1547ee5254dabfedcf24812e81d9db6bf1ee7d17d2
MD5 d2ca450c2be020da9e78f79b6d254f5f
BLAKE2b-256 4dd10a01b459480a4f5b65c84cd7b7de71b28212f730212dbeb44a08687e93e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for boj_api_client-0.1.1-py3-none-any.whl:

Publisher: ci.yml on delihiros/boj-api-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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