{
  "openapi": "3.1.1",
  "info": {
    "title": "Bill Detective API",
    "version": "1.0.0",
    "summary": "Spot spikes, price changes and calculation errors across your utility bills.",
    "description": "Every endpoint performs the same calculation as the human page and returns inputs, result, units, assumptions, sources, warnings and computedAt. MCP endpoint: /mcp."
  },
  "servers": [
    {
      "url": "https://billdetective.usefulweb.site"
    }
  ],
  "paths": {
    "/api/v1/calculate": {
      "post": {
        "operationId": "calculate",
        "summary": "Spot spikes, price changes and calculation errors across your utility bills.",
        "description": "Enter several billing periods of an electricity, gas, water or data bill. Bill Detective normalises consumption per day, flags unusual periods (outliers), unit-price and standing-charge changes, and totals that do not add up, each with a plain-English explanation.\n\nMethodology:\n- Days = end date − start date + 1 (both dates count). Consumption per day = consumption ÷ days. Periods are sorted by start date.\n- Computed total = consumption × unit price + fixed charge + other charges. A mismatch is flagged when |total billed − computed| > tolerance % × computed (minimum 0.01).\n- Consumption change: per-day consumption vs. the previous period; flagged when the absolute change exceeds the threshold (default 25%).\n- Above average: per-day consumption more than the threshold above the average of all periods.\n- Outliers (IQR rule, only with ≥ 4 periods): per-day value above Q3 + 1.5 × IQR or below Q1 − 1.5 × IQR, quartiles by linear interpolation.\n- Outliers (median rule): per-day value more than X% (default 30%) above the median per-day value.\n- Unit price change: any change vs. the previous period. Fixed charge change: both the fixed charge per period and the fixed charge per day differ from the previous period by more than 1% (so a flat monthly fee over months of different length, or a fixed daily rate, is not flagged).\n- Gaps and overlaps between consecutive periods are reported as information.",
        "tags": [
          "money"
        ],
        "x-mcp-tool": "bill_detective",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BillDetectiveInput"
              },
              "examples": {
                "example1": {
                  "summary": "Monthly electricity with a winter spike and a price rise",
                  "value": {
                    "unit": "kWh",
                    "currency": "EUR",
                    "periods": [
                      {
                        "label": "Jan",
                        "startDate": "2026-01-01",
                        "endDate": "2026-01-31",
                        "consumption": 310,
                        "unitPrice": 0.32,
                        "fixedCharge": 12,
                        "totalBilled": 111.2
                      },
                      {
                        "label": "Feb",
                        "startDate": "2026-02-01",
                        "endDate": "2026-02-28",
                        "consumption": 280,
                        "unitPrice": 0.32,
                        "fixedCharge": 12,
                        "totalBilled": 101.6
                      },
                      {
                        "label": "Mar",
                        "startDate": "2026-03-01",
                        "endDate": "2026-03-31",
                        "consumption": 300,
                        "unitPrice": 0.32,
                        "fixedCharge": 12,
                        "totalBilled": 108
                      },
                      {
                        "label": "Apr",
                        "startDate": "2026-04-01",
                        "endDate": "2026-04-30",
                        "consumption": 290,
                        "unitPrice": 0.35,
                        "fixedCharge": 12,
                        "totalBilled": 113.5
                      },
                      {
                        "label": "May",
                        "startDate": "2026-05-01",
                        "endDate": "2026-05-31",
                        "consumption": 520,
                        "unitPrice": 0.35,
                        "fixedCharge": 14,
                        "totalBilled": 198
                      },
                      {
                        "label": "Jun",
                        "startDate": "2026-06-01",
                        "endDate": "2026-06-30",
                        "consumption": 295,
                        "unitPrice": 0.35,
                        "fixedCharge": 14,
                        "totalBilled": 117.25
                      }
                    ]
                  }
                },
                "example2": {
                  "summary": "Quarterly water bills",
                  "value": {
                    "unit": "m3",
                    "currency": "GBP",
                    "periods": [
                      {
                        "label": "Q1",
                        "startDate": "2025-01-01",
                        "endDate": "2025-03-31",
                        "consumption": 24,
                        "unitPrice": 2.1,
                        "fixedCharge": 18,
                        "totalBilled": 68.4
                      },
                      {
                        "label": "Q2",
                        "startDate": "2025-04-01",
                        "endDate": "2025-06-30",
                        "consumption": 26,
                        "unitPrice": 2.1,
                        "fixedCharge": 18,
                        "totalBilled": 72.6
                      },
                      {
                        "label": "Q3",
                        "startDate": "2025-07-01",
                        "endDate": "2025-09-30",
                        "consumption": 31,
                        "unitPrice": 2.25,
                        "fixedCharge": 18,
                        "totalBilled": 87.75
                      }
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Calculation result envelope.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BillDetectiveResult"
                }
              }
            }
          },
          "400": {
            "description": "Malformed JSON body.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Invalid input (see errors[]).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "Rate limit exceeded (see Retry-After).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/tool": {
      "get": {
        "operationId": "getToolDefinition",
        "summary": "Inputs, output schema and examples for this tool",
        "responses": {
          "200": {
            "description": "Tool definition.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Problem": {
        "type": "object",
        "description": "RFC 9457 problem details. 422 responses include per-field `errors`.",
        "required": [
          "type",
          "title",
          "status",
          "detail"
        ],
        "properties": {
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "status": {
            "type": "integer"
          },
          "detail": {
            "type": "string"
          },
          "code": {
            "type": "string"
          },
          "errors": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "field",
                "message"
              ],
              "properties": {
                "field": {
                  "type": "string"
                },
                "message": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "BillDetectiveInput": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "unit": {
            "title": "Consumption unit",
            "type": "string",
            "enum": [
              "kWh",
              "m3",
              "liters",
              "GB"
            ],
            "default": "kWh"
          },
          "currency": {
            "title": "Currency",
            "type": "string",
            "maxLength": 8,
            "default": "EUR"
          },
          "changeThresholdPercent": {
            "title": "Flag per-day change vs. previous period above",
            "type": "number",
            "minimum": 1,
            "maximum": 1000,
            "default": 25,
            "x-unit": "%"
          },
          "outlierPercent": {
            "title": "Flag periods above median by more than",
            "type": "number",
            "minimum": 1,
            "maximum": 1000,
            "default": 30,
            "x-unit": "%"
          },
          "tolerancePercent": {
            "title": "Arithmetic tolerance",
            "description": "Billed total may differ from consumption × unit price + charges by this much (rounding, taxes included in prices) before it is flagged.",
            "type": "number",
            "minimum": 0,
            "maximum": 50,
            "default": 1,
            "x-unit": "%"
          },
          "periods": {
            "title": "Billing periods",
            "description": "Start and end dates are both counted as billing days. Fixed charge = total standing/base charge for the period. Leave Total billed empty to skip the arithmetic check for a row.",
            "type": "array",
            "minItems": 2,
            "maxItems": 36,
            "items": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "label": {
                  "title": "Period",
                  "type": "string"
                },
                "startDate": {
                  "title": "Start date",
                  "type": "string",
                  "format": "date"
                },
                "endDate": {
                  "title": "End date",
                  "type": "string",
                  "format": "date"
                },
                "consumption": {
                  "title": "Consumption",
                  "type": "number",
                  "minimum": 0
                },
                "unitPrice": {
                  "title": "Unit price",
                  "type": "number",
                  "minimum": 0
                },
                "fixedCharge": {
                  "title": "Fixed / standing charge",
                  "type": "number",
                  "minimum": 0,
                  "default": 0
                },
                "otherCharges": {
                  "title": "Other charges",
                  "type": "number",
                  "default": 0
                },
                "totalBilled": {
                  "title": "Total billed",
                  "type": "number",
                  "minimum": 0
                }
              },
              "required": [
                "label",
                "startDate",
                "endDate",
                "consumption",
                "unitPrice"
              ]
            }
          }
        },
        "required": [
          "periods"
        ]
      },
      "BillDetectiveResult": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "schemaVersion",
          "tool",
          "computedAt",
          "inputs",
          "summary",
          "result",
          "units",
          "assumptions",
          "sources",
          "warnings",
          "presentation",
          "links"
        ],
        "properties": {
          "schemaVersion": {
            "type": "string",
            "const": "1.0"
          },
          "tool": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "name",
              "version"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "version": {
                "type": "string"
              }
            }
          },
          "computedAt": {
            "type": "string",
            "format": "date-time"
          },
          "inputs": {
            "type": "object",
            "description": "Normalised inputs actually used, including applied defaults."
          },
          "summary": {
            "type": "string"
          },
          "result": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "unit",
              "currency",
              "averagePerDay",
              "medianPerDay",
              "periods",
              "anomalies",
              "anomalyCount"
            ],
            "properties": {
              "unit": {
                "type": "string"
              },
              "currency": {
                "type": "string"
              },
              "averagePerDay": {
                "type": "number"
              },
              "medianPerDay": {
                "type": "number"
              },
              "iqr": {
                "type": [
                  "object",
                  "null"
                ],
                "additionalProperties": false,
                "required": [
                  "q1",
                  "q3",
                  "lowerFence",
                  "upperFence"
                ],
                "properties": {
                  "q1": {
                    "type": "number"
                  },
                  "q3": {
                    "type": "number"
                  },
                  "lowerFence": {
                    "type": "number"
                  },
                  "upperFence": {
                    "type": "number"
                  }
                }
              },
              "anomalyCount": {
                "type": "integer"
              },
              "periods": {
                "type": "array",
                "items": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "label",
                    "startDate",
                    "endDate",
                    "days",
                    "consumption",
                    "perDay",
                    "changeVsPreviousPercent",
                    "vsAveragePercent",
                    "unitPrice",
                    "fixedCharge",
                    "fixedChargePerDay",
                    "computedTotal",
                    "totalBilled",
                    "difference",
                    "costPerDay"
                  ],
                  "properties": {
                    "label": {
                      "type": "string"
                    },
                    "startDate": {
                      "type": "string",
                      "format": "date"
                    },
                    "endDate": {
                      "type": "string",
                      "format": "date"
                    },
                    "days": {
                      "type": "integer"
                    },
                    "consumption": {
                      "type": "number"
                    },
                    "perDay": {
                      "type": "number"
                    },
                    "changeVsPreviousPercent": {
                      "type": [
                        "number",
                        "null"
                      ]
                    },
                    "vsAveragePercent": {
                      "type": "number"
                    },
                    "unitPrice": {
                      "type": "number"
                    },
                    "fixedCharge": {
                      "type": "number"
                    },
                    "fixedChargePerDay": {
                      "type": "number"
                    },
                    "computedTotal": {
                      "type": "number"
                    },
                    "totalBilled": {
                      "type": [
                        "number",
                        "null"
                      ]
                    },
                    "difference": {
                      "type": [
                        "number",
                        "null"
                      ],
                      "description": "Total billed − computed total."
                    },
                    "costPerDay": {
                      "type": "number",
                      "description": "Billed total (or computed total if not given) ÷ days."
                    }
                  }
                }
              },
              "anomalies": {
                "type": "array",
                "items": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "period",
                    "type",
                    "severity",
                    "explanation"
                  ],
                  "properties": {
                    "period": {
                      "type": "string"
                    },
                    "type": {
                      "type": "string",
                      "enum": [
                        "consumption-change",
                        "above-average",
                        "outlier-iqr",
                        "outlier-median",
                        "unit-price-change",
                        "fixed-charge-change",
                        "arithmetic-mismatch",
                        "period-overlap",
                        "period-gap"
                      ]
                    },
                    "severity": {
                      "type": "string",
                      "enum": [
                        "info",
                        "warning",
                        "alert"
                      ]
                    },
                    "explanation": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "units": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "assumptions": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "sources": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "title"
              ],
              "properties": {
                "title": {
                  "type": "string"
                },
                "url": {
                  "type": "string"
                },
                "publisher": {
                  "type": "string"
                },
                "retrievedAt": {
                  "type": "string"
                },
                "note": {
                  "type": "string"
                }
              }
            }
          },
          "warnings": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "presentation": {
            "type": "array",
            "items": {
              "type": "object"
            },
            "description": "Display blocks used by the human page (same data)."
          },
          "links": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "human",
              "api",
              "openapi",
              "methodology"
            ],
            "properties": {
              "human": {
                "type": "string",
                "format": "uri"
              },
              "api": {
                "type": "string",
                "format": "uri"
              },
              "openapi": {
                "type": "string",
                "format": "uri"
              },
              "methodology": {
                "type": "string",
                "format": "uri"
              }
            }
          }
        }
      }
    }
  }
}