{
 "openapi": "3.1.0",
 "info": {
  "title": "Kitforge Chart API",
  "version": "0.1.0",
  "description": "Chart images (SVG) from a URL or JSON. Auth: Authorization: Bearer <Kitforge API key> (shared with the Tech Stack API; free key with 50 credits at https://kitforge-techstack.pages.dev/). 1 credit per new chart; signed URLs are free to view."
 },
 "servers": [
  {
   "url": "https://kitforge-chart-api.kitforgelabs.workers.dev"
  }
 ],
 "components": {
  "securitySchemes": {
   "bearer": {
    "type": "http",
    "scheme": "bearer"
   }
  },
  "schemas": {
   "Error": {
    "type": "object",
    "properties": {
     "error": {
      "type": "string"
     },
     "message": {
      "type": "string"
     },
     "recharge_url": {
      "type": "string"
     }
    },
    "required": [
     "error"
    ]
   },
   "ChartRequest": {
    "type": "object",
    "properties": {
     "type": {
      "type": "string",
      "enum": [
       "bar",
       "line",
       "pie",
       "doughnut",
       "sparkline"
      ],
      "default": "bar"
     },
     "title": {
      "type": "string",
      "maxLength": 100
     },
     "labels": {
      "type": "array",
      "items": {
       "type": "string",
       "maxLength": 40
      }
     },
     "data": {
      "type": "array",
      "items": {
       "type": [
        "number",
        "null"
       ]
      }
     },
     "series": {
      "type": "array",
      "maxItems": 8,
      "items": {
       "type": "object",
       "properties": {
        "name": {
         "type": "string"
        },
        "data": {
         "type": "array",
         "items": {
          "type": [
           "number",
           "null"
          ]
         }
        }
       },
       "required": [
        "data"
       ]
      }
     },
     "stacked": {
      "type": "boolean"
     },
     "values": {
      "type": "boolean"
     },
     "transparent": {
      "type": "boolean"
     },
     "prefix": {
      "type": "string",
      "maxLength": 8
     },
     "suffix": {
      "type": "string",
      "maxLength": 8
     },
     "theme": {
      "type": "string",
      "enum": [
       "light",
       "dark"
      ]
     },
     "width": {
      "type": "integer",
      "minimum": 120,
      "maximum": 1600
     },
     "height": {
      "type": "integer",
      "minimum": 32,
      "maximum": 1000
     },
     "colors": {
      "type": "array",
      "maxItems": 8,
      "items": {
       "type": "string",
       "pattern": "^#?[0-9a-fA-F]{6}$"
      }
     },
     "response": {
      "type": "string",
      "enum": [
       "svg",
       "json"
      ],
      "default": "svg"
     }
    }
   },
   "ChartJson": {
    "type": "object",
    "properties": {
     "url": {
      "type": [
       "string",
       "null"
      ]
     },
     "type": {
      "type": "string"
     },
     "width": {
      "type": "integer"
     },
     "height": {
      "type": "integer"
     },
     "svg": {
      "type": "string"
     },
     "charged": {
      "type": "boolean"
     },
     "credits": {
      "type": "object"
     }
    }
   }
  }
 },
 "paths": {
  "/v1/chart": {
   "get": {
    "operationId": "getChart",
    "summary": "Chart image from query parameters",
    "description": "With a key: renders and charges 1 credit (same chart again: free). With sig (from x-chart-url): no key, free, cacheable.",
    "security": [
     {
      "bearer": []
     },
     {}
    ],
    "parameters": [
     {
      "name": "type",
      "in": "query",
      "required": false,
      "description": "Chart type",
      "schema": {
       "type": "string",
       "enum": [
        "bar",
        "line",
        "pie",
        "doughnut",
        "sparkline"
       ]
      }
     },
     {
      "name": "data",
      "in": "query",
      "required": true,
      "description": "Values \"1,2,3\"; series separated by \"|\"",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "labels",
      "in": "query",
      "required": false,
      "description": "Labels separated by \"|\"",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "series",
      "in": "query",
      "required": false,
      "description": "Series names separated by \"|\"",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "title",
      "in": "query",
      "required": false,
      "description": "Title",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "width",
      "in": "query",
      "required": false,
      "description": "Width in px",
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "height",
      "in": "query",
      "required": false,
      "description": "Height in px",
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "theme",
      "in": "query",
      "required": false,
      "description": "light or dark",
      "schema": {
       "type": "string",
       "enum": [
        "light",
        "dark"
       ]
      }
     },
     {
      "name": "colors",
      "in": "query",
      "required": false,
      "description": "Hex colours separated by \"|\"",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "stacked",
      "in": "query",
      "required": false,
      "description": "Stack bar series (1)",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "values",
      "in": "query",
      "required": false,
      "description": "Show values (1)",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "transparent",
      "in": "query",
      "required": false,
      "description": "No background (1)",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "prefix",
      "in": "query",
      "required": false,
      "description": "Value prefix",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "suffix",
      "in": "query",
      "required": false,
      "description": "Value suffix",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "sig",
      "in": "query",
      "required": false,
      "description": "Signature of a signed chart URL",
      "schema": {
       "type": "string"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "The chart as SVG",
      "content": {
       "image/svg+xml": {
        "schema": {
         "type": "string"
        }
       }
      },
      "headers": {
       "x-chart-url": {
        "description": "Signed public URL of this chart (no key needed, cacheable)",
        "schema": {
         "type": "string"
        }
       },
       "x-credits-remaining": {
        "description": "Credits left on the key",
        "schema": {
         "type": "integer"
        }
       },
       "x-charged": {
        "description": "1 if this call used a credit, 0 if the same chart was already paid for with this key",
        "schema": {
         "type": "string"
        }
       }
      }
     },
     "400": {
      "description": "Invalid chart parameters",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Missing or invalid key",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "402": {
      "description": "No credits left (recharge_url)",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "403": {
      "description": "Invalid signature",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "413": {
      "description": "Query too long",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Rate limited",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   },
   "post": {
    "operationId": "postChart",
    "summary": "Chart image from JSON",
    "security": [
     {
      "bearer": []
     }
    ],
    "parameters": [
     {
      "name": "response",
      "in": "query",
      "required": false,
      "description": "svg (default) or json",
      "schema": {
       "type": "string",
       "enum": [
        "svg",
        "json"
       ]
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/ChartRequest"
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "SVG (default) or JSON with url + svg",
      "headers": {
       "x-chart-url": {
        "description": "Signed public URL of this chart (no key needed, cacheable)",
        "schema": {
         "type": "string"
        }
       },
       "x-credits-remaining": {
        "description": "Credits left on the key",
        "schema": {
         "type": "integer"
        }
       },
       "x-charged": {
        "description": "1 if this call used a credit, 0 if the same chart was already paid for with this key",
        "schema": {
         "type": "string"
        }
       }
      },
      "content": {
       "image/svg+xml": {
        "schema": {
         "type": "string"
        }
       },
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/ChartJson"
        }
       }
      }
     },
     "400": {
      "description": "Invalid chart",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "401": {
      "description": "Missing or invalid key",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "402": {
      "description": "No credits left",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "413": {
      "description": "Body too large",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     },
     "429": {
      "description": "Rate limited",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/v1/balance": {
   "get": {
    "operationId": "getBalance",
    "summary": "Credits left on the key",
    "security": [
     {
      "bearer": []
     }
    ],
    "responses": {
     "200": {
      "description": "Balance",
      "content": {
       "application/json": {
        "schema": {
         "type": "object"
        }
       }
      }
     },
     "401": {
      "description": "Missing or invalid key",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Error"
        }
       }
      }
     }
    }
   }
  },
  "/health": {
   "get": {
    "operationId": "health",
    "summary": "Health check",
    "responses": {
     "200": {
      "description": "OK"
     }
    }
   }
  }
 }
}