リファレンス

Pain001 技術リファレンス:CLI・Python API・REST

pain001 v0.0.90 のすべてのフラグ・エンドポイント・挙動を、願望ではなく出荷済みコードに基づいて解説します。

本リファレンスは、pain001 v0.0.90 のコマンドラインインターフェース、Python API、REST マイクロサービス、検証パイプラインを文書化しています。ここに記載されているフラグ・エンドポイント・挙動はすべて、願望ではなく出荷済みコードに基づいています。

Pain001 は 13 種類の ISO 20022 メッセージ定義をサポートしています:pain.001.001.03 から pain.001.001.13 まで(Customer Credit Transfer Initiation、11 バージョン)および pain.008.001.02 / pain.008.001.08(Customer Direct Debit Initiation)です。


1. コマンドラインインターフェース#

pain001 実行ファイルは、機能をサブコマンドにまとめています。生成用フラグのみで実行すると暗黙的に generate が呼び出されるため、既存の自動化はそのまま動作し続けます。

サブコマンド 目的
generate データファイルをスキーマ検証済みの ISO 20022 XML に変換します(デフォルトコマンド)。
validate XML を書き出さずに入力データを検証します。
check <file>... [--scheme] [--format] Check pain.001 or pain.008 files you already have: the XSD of the message type each file declares, the message definition's cross-element rules, and any scheme profiles, policy or Schematron rulebook you name.
verify-signature <file>... --cert <PEM> Verify the XML-DSig signature on each file against the signer's certificate or public key; --trust-embedded-cert checks integrity only (requires the sign extra).
versions [--format json] サポート対象の 13 種類のメッセージ定義をすべて一覧表示します。
inspect <type> [--format json] メッセージ種別ごとに、同梱のテンプレート、スキーマ、サンプルデータ、受け付ける入力形式を表示します。
init <type> [-o FILE] メッセージタイプ用のスターター CSV テンプレートを生成します。
serve [--host] [--port] [--reload] FastAPI REST マイクロサービスを起動します(api エクストラが必要です)。
mcp コアライブラリ内蔵の Model Context Protocol サーバーを stdio で起動します(mcp エクストラが必要)。完全版のサーバーは pain001-mcp として提供されています。
plugins list / show / disable 検出されたローダー、バリデーター、スキーム、ライターのプラグインを一覧表示し、その一つを表示するか、サードパーティ製プラグインの検出を無効にする方法を表示します。
face <file> [--face] [--bindings] [-o] [--loss-report] Project a pain.001 XML file into another standard's JSON shape, with a report of every element not carried.
upload [<file>] --sftp <url> Deliver a file atomically to an SFTP server you trust explicitly (requires the upload extra).

generate のオプション

フラグ 説明
-t, --xml-message-type <TYPE> メッセージ定義。例:pain.001.001.09。
-d, --data <FILE> 入力データ:.csv、.json、.jsonl、.db / .sqlite、.parquet、または PGP 暗号化された .gpg / .asc。
-o, --output-dir <DIR> 生成された XML を受け取るディレクトリです。
-m, --template <FILE> / -s, --schema <FILE> 同梱の Jinja2 テンプレートまたは XSD スキーマを上書きします。
-c, --config <FILE> 設定プロファイルからデフォルト値を読み込みます(--profile、--show-config)。
--dry-run(エイリアス --validate-only) 出力を書き出さずに、JSON Schema、XSD、スキームルールブックに対して入力を検証します。
--scheme <NAME[,NAME]> カンマ区切りで一つ以上のスキームのルールブックを適用します。pain001 plugins list はインストール済みのすべてのルールブックを表示します。
--explain / --format json --scheme と併用すると、違反ごとに修正のヒントをテキストまたは JSON で出力します。
--rules <FILE> Enforce a private YAML CEL policy before generation (requires the rules extra).
--schematron <PRESET or FILE> Apply Schematron business rules: the sepa, fednow or cbpr preset, or your own .sch file.
--decrypt-key <FILE> / --decrypt-passphrase-env <VAR> Import a GPG private key, and name the variable that holds its passphrase, for .gpg / .asc input (requires the gpg extra).
--streaming / --chunk-size <N> 大規模バッチ向けのメモリ制限付きチャンク処理です(デフォルトはチャンクあたり 1,000 トランザクション。各チャンクは、再計算された NbOfTxs と CtrlSum を持つ独立した XML ファイルになります)。
--emit-metrics 可観測性パイプライン向けに機械可読の実行メトリクスを出力します。
--envelop-bah Wrap generated payment XML into an ISO 20022 Business Application Header (head.001.001.03) and BizData (head.003.001.01) envelope.
--bah-sender <BIC/ID> Sender financial institution BIC or organisation identifier for BAH <Fr>.
--bah-receiver <BIC/ID> Receiver financial institution BIC or organisation identifier for BAH <To>.
--bah-msg-id <ID> Business Message Identifier for BAH <BizMsgIdr> (defaults to generated UUID).
--xml-sign-key <FILE> PEM private key (RSA, or EC P-256/P-384) for a W3C XML Digital Signature (XML-DSig); the algorithm follows the key.
--xml-sign-cert <FILE> Optional PEM X.509 certificate to embed in XML-DSig <ds:KeyInfo>.
--xml-sign-alg <ALG> rsa-sha256, rsa-pss-sha256, ecdsa-sha256 or ecdsa-sha384; by default an RSA key signs rsa-sha256.
--xml-sign-passphrase-env <VAR> Environment variable holding the passphrase for --xml-sign-key.
--list-templates / --show-template <TYPE> List the bundled templates, or show one template's metadata, and exit.

終了コードは CI に適しています:0 は成功、1 は検証失敗、2 は使用方法エラーです。

XML signing and verify-signature need the sign extra (pip install "pain001[sign]", which installs cryptography and lxml).

Signatures: pain001 verify-signature

pain001 verify-signature checks the signature on files you received or wrote. Pass the signer's certificate or public key with --cert; that is the trust anchor. --trust-embedded-cert verifies with each file's own certificate instead, which proves the file is unchanged since it was signed but not who signed it. It exits 0 when every file verifies, 1 when any does not, and 2 when it cannot run; --format json prints one report per file. Verification uses exclusive C14N 1.0 and accepts only RSA-SHA256, RSA-PSS and ECDSA signatures with SHA-256 to SHA-512 digests over the whole document; the certificate's chain and validity dates are not checked.

Faces: pain001 face

A face projects a validated pain.001 document into another standard's JSON request body. --face uk_open_banking targets the Open Banking Read/Write API v4.0.1 domestic payment, --face berlin_group targets NextGenPSD2 v1.3.16 payment initiation, and --face iso_2018 renames every element to its ISO 20022 name in snake_case. A face refuses a document that lacks an element its target requires instead of inventing a value; --loss-report lists every source element it did not carry. Faces are outbound only.

Delivery: pain001 upload

pain001 upload sends a finished file to an SFTP server you name, and only after you have trusted its host key: from a known_hosts file (--known-hosts) or an OpenSSH SHA256 fingerprint you verified independently (--accept-host-key). --print-host-key shows the server's fingerprint without logging in. It logs in with a private key (--identity-file, with --passphrase-env), a password read from an environment variable (--password-env), or your SSH agent. The file is written under a temporary name and renamed on the server, so the receiving side never sees a partial file.


2. Python API#

from pain001.core.core import process_files

# Generate a validated pain.001.001.09 file from CSV
process_files(
    xml_message_type="pain.001.001.09",
    xml_template_file_path="template.xml",
    xsd_schema_file_path="schema.xsd",
    data_file_path="payments.csv",
    output_path="out/payments.xml",
)

生成されるすべてのドキュメントは、書き出される前に 3 つの層を通過します:

  1. 入力検証:各レコードは、フィールドエイリアスの正規化と IBAN/BIC の構文チェックを伴って、メッセージタイプの JSON Schema に対して検査されます。
  2. スキームルールブック(任意):指定した決済システムのルールです。SEPA(SCT、Instant、SDD Core、B2B)から、UK Faster Payments、CHAPS、Bacs、米国の ACH や電信送金などの各国の決済システム、Swift CBPR+ までを含みます。pain001 plugins list ですべての名前を確認できます。
  3. XSD 検証:レンダリングされた XML は、1 バイトもディスクに書き込まれる前に、xmlschema を介して公式の ISO 20022 スキーマに対して検証されます。任意の fast エクストラ(lxml)をインストールすると、libxml2 が有効な文書をより速く受理します。libxml2 が拒否した文書は xmlschema が改めて検査し、その判定が最終となります。

金額は、XML 生成およびスキーム検証の間、decimal.Decimal として扱われ、IEEE 754 浮動小数点数は決して使用されません。また、NbOfTxs / CtrlSum のコントロールトータルは、入力を信頼するのではなく、検証済みレコードから再計算されます。

pain.001 の生成に加えて、コアライブラリには、pain.002 ステータスレポートのパーサーとジェネレーター(銀行の受理/拒否応答を読み取れます)と、日末照合のための camt.053 ステートメントのパーサーとジェネレーター、さらにメッセージバージョン間でレコードを移行する VersionMapper が含まれています。

pain001.faces.project_face is the Python entry point for faces. sign_xml_document signs a document with XML-DSig, and verify_signature(signed_xml, trust_anchor=cert_pem) verifies one and returns a SignatureReport (valid, trust, algorithm, signer, reason); both need the sign extra. verify_xml_signature without a key or certificate still trusts the certificate inside the document, with a DeprecationWarning; pain001 0.0.90 refuses it.

Runnable, self-checking scripts for the public API and the CLI live in the library's examples/ directory.


3. REST マイクロサービス#

pip install "pain001[api]"
pain001 serve --host 0.0.0.0 --port 8000

すべてのエンドポイントは /api/v1 配下にマウントされています(バージョンなしの /api エイリアスもあります):

メソッドとパス 目的
GET /api/v1/health 死活監視プローブです。
POST /api/v1/validate レコードを検証し、フィールドレベルのエラーを返します。
POST /api/v1/generate 同期的な XML 生成です。
POST /api/v1/generate/async 大規模バッチをバックグラウンド生成のためにキューに入れます。
GET /api/v1/status/{job_id} 非同期ジョブをポーリングします。
GET /api/v1/download/{job_id} 完成した XML をダウンロードします。
DELETE /api/v1/jobs/{job_id} 完了したジョブをクリーンアップします。
GET /api/v1/faces List the faces and the binding keys each accepts.
POST /api/v1/faces/{face} Project a pain.001 document sent as XML text; returns the payload and the loss report, or 422 with the reason the face refused it.
GET /metrics Prometheus メトリクスです。

インタラクティブなドキュメントは /api/docs(Swagger UI)、/api/redoc、/api/reference(Scalar)で提供され、OpenAPI ドキュメントは /openapi.json にあります。

When the PAIN001_API_KEY environment variable is set, every endpoint except /health and /metrics requires it as a bearer token; when it is unset, the API is open. pain001 serve binds to 127.0.0.1 unless you pass --host.


4. 入力の正規化#

Pain001 は、検証の前に、実務上のエクスポートデータを有効なレコードに変換します:

  • フィールドエイリアス:一般的な ERP のカラム名が正規のフィールドにマッピングされます(例:amount → payment_amount)。
  • IBAN / BIC の正規化:空白を除去し、大文字小文字を揃えたうえで検査します(IBAN は ISO 13616 の mod-97、BIC は ISO 9362 の構造)。
  • 日付:実行日について ISO 8601 の YYYY-MM-DD 形式で解析します。
  • 金額:decimal.Decimal を経由して処理されます。不正な金額は、暗黙に丸められるのではなく検証エラーになります。
  • 文字セット:翻字ヘルパーが、SWIFT と SEPA が受け付ける ISO 20022 ラテン文字セットへとコンテンツを変換します。
  • Formula cells: CSV cells that start with =, +, - or @ are kept as supplied by default, so phone numbers and references survive; load_csv_data(path, sanitize_formula=True) escapes them with a leading quote instead.

5. プラグインアーキテクチャ#

本スイートは、pain001.loaders、pain001.validators、pain001.schemes、pain001.writers の 4 つのエントリポイントグループを通じて拡張できます。pain001-loader-xlsx はこの仕組みで登録され、インストール時に自動検出されます。pain001-loader-mt101 は、直接(および MCP サーバーの convert_mt101 ツールから)利用されるスタンドアロンの解析ライブラリです。キルスイッチ PAIN001_DISABLE_PLUGINS=1 により、ロックダウンされた環境ではサードパーティ製プラグインの検出を完全に無効化できます。


6. 品質ゲート#

コアライブラリは、厳格で検証可能なゲートのもとで開発されています:CI で強制される 100% のラインおよびブランチカバレッジ(--cov-fail-under=100)、厳格な mypy 型付け、100% の docstring カバレッジ、セキュリティリンティング(Bandit、pip-audit)です。すべてのリリースビルドで CycloneDX SBOM が生成されます。

続けて、インストールガイド、AI エージェント向け MCP サーバー、または決済用語集をご覧ください。