openapi: 3.1.0
info:
  title: KPI Trust API
  version: "1.0.0"
  summary: 外部サービスから KPI Trust に日次の実績を送る API
  description: |
    KPI Trust に、ほかのシステムやスプレッドシートから日次の実績を送るための API です。
    人が読む仕様書は https://kpitrust.com/developers にあります。

    - 同じ KPI・同じ日を送り直すと上書きします（足しません）。
    - 1 回の送信のうち、通る値は書き、通らない値は rejected・rejected_items で返します（200）。どの値も通らないときだけ 422 で、何も書きません。
    - date は組織のタイムゾーンでの日付です。
  contact:
    url: https://kpitrust.com/contact
servers:
  - url: https://kpitrust.com/api/v1
security:
  - bearerAuth: []
paths:
  /kpis:
    get:
      operationId: listKpis
      summary: 外部キーの付いた KPI の一覧
      description: トークンが使えるかの確認にも使います。
      responses:
        "200":
          description: 一覧
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/KpiList"
        "401":
          $ref: "#/components/responses/Error"
        "403":
          $ref: "#/components/responses/Error"
        "429":
          $ref: "#/components/responses/RateLimited"
        "500":
          $ref: "#/components/responses/Error"
  /actuals:
    post:
      operationId: writeActuals
      summary: 日次の実績を書く・消す
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ActualsRequest"
            example:
              values:
                - key: web_sessions
                  date: "2026-11-21"
                  value: 1234
                - key: web_cv
                  date: "2026-11-21"
                  value: 18
      responses:
        "200":
          description: 書けた（一部が通らなかったときも 200。rejected を確かめる）
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActualsResult"
        "400":
          $ref: "#/components/responses/Error"
        "401":
          $ref: "#/components/responses/Error"
        "403":
          $ref: "#/components/responses/Error"
        "413":
          $ref: "#/components/responses/Error"
        "422":
          $ref: "#/components/responses/Error"
        "429":
          $ref: "#/components/responses/RateLimited"
        "500":
          $ref: "#/components/responses/Error"
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: 「設定 → 連携」で発行する API トークン（kpt_live_ と 16 進 32 桁）
  responses:
    Error:
      description: 失敗
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
    RateLimited:
      description: 送信が多すぎる（トークン 1 つにつき 1 分 60 回）
      headers:
        Retry-After:
          description: 待つ秒数
          schema:
            type: integer
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
  schemas:
    KpiList:
      type: object
      required: [ok, organization, kpis]
      properties:
        ok:
          const: true
        organization:
          type: object
          required: [name, time_zone, today]
          properties:
            name:
              type: string
            time_zone:
              type: string
              description: 組織のタイムゾーン（IANA）。date はこのタイムゾーンの日付で送る
              examples: [Asia/Tokyo]
            today:
              type: string
              format: date
              description: 組織のタイムゾーンでの今日
        kpis:
          type: array
          items:
            $ref: "#/components/schemas/Kpi"
    Kpi:
      type: object
      required: [key, name, type, format, unit_label, aggregation, frequency, data_source, lifecycle, writable]
      properties:
        key:
          type: string
          pattern: "^[a-z][a-z0-9_]{0,63}$"
          description: 外部キー
        name:
          type: string
        type:
          type: string
          enum: [KRI, RI, PI, KPI]
        format:
          type: string
          enum: [count, percent, time, currency]
        unit_label:
          type: string
        aggregation:
          type: string
          enum: [sum, average, last, ratio, formula]
        frequency:
          type: string
          enum: [daily, weekly, monthly]
          description: monthly の KPI には date の代わりに month で送る
        data_source:
          type: string
          enum: [manual, api, csv]
        lifecycle:
          type: string
          enum: [trial, active, rejected, retired]
        writable:
          type: boolean
          description: データの出どころが api、比率でない、試行中か運用中のとき true
    ActualsRequest:
      type: object
      required: [values]
      properties:
        values:
          type: array
          minItems: 1
          maxItems: 500
          items:
            $ref: "#/components/schemas/ActualValue"
    ActualValue:
      type: object
      required: [key, value]
      oneOf:
        - required: [date]
        - required: [month]
      properties:
        key:
          type: string
          description: KPI の外部キー
        date:
          type: string
          format: date
          description: 日次・週次の KPI のとき。組織のタイムゾーンでの日付。今日から 400 日前まで
        month:
          type: string
          pattern: "^\\d{4}-(0[1-9]|1[0-2])$"
          description: 月次の KPI のとき（date の代わり）。その月の月末の日の実績として持つ
        value:
          type: [number, "null"]
          description: その日（月次なら、その月）1 つ分の値。null ならその日（月）の API の実績を消す
    ActualsResult:
      type: object
      required: [ok, written, deleted, rejected, rejected_items]
      properties:
        ok:
          const: true
        written:
          type: integer
          description: 書いた（新しく入れた・上書きした）件数
        deleted:
          type: integer
          description: null で消した件数
        rejected:
          type: integer
          description: 通らず、書かなかった件数。すべて通れば 0。1 以上なら rejected_items を見て直す
        rejected_items:
          type: array
          description: 通らなかった値（最大 100 件）
          items:
            $ref: "#/components/schemas/RejectedItem"
    RejectedItem:
      type: object
      required: [index, code, message]
      properties:
        index:
          type: integer
          description: values の何件目か（0 始まり）
        code:
          type: string
          enum:
            - invalid_item
            - invalid_date
            - invalid_month
            - date_out_of_range
            - period_mismatch
            - invalid_value
            - unknown_key
            - not_api_source
            - ratio_kpi
            - retired_kpi
            - duplicate_item
        message:
          type: string
    Error:
      type: object
      required: [ok, error]
      properties:
        ok:
          const: false
        error:
          type: object
          required: [code, message]
          properties:
            code:
              type: string
              enum:
                - invalid_json
                - invalid_request
                - too_many_items
                - invalid_token
                - token_revoked
                - plan_required
                - payload_too_large
                - validation_failed
                - rate_limited
                - internal_error
                - not_configured
            message:
              type: string
            retry_after:
              type: integer
              description: rate_limited のとき、待つ秒数
            items:
              type: array
              description: validation_failed（どの値も通らなかった）のとき、通らなかった値（最大 100 件）
              items:
                $ref: "#/components/schemas/RejectedItem"
