{
  "openapi": "3.0.3",
  "info": {
    "title": "KeepHabits Public API",
    "version": "1.0.0",
    "description": "Pro account API. Create scoped tokens in Settings → Data and security → Public API. Tokens are shown once, expire within 365 days, and can be revoked. Maximum 5 active tokens, 60 requests/minute per token (additional IP protection applies). Active Pro and account status are checked on every request. All dates use YYYY-MM-DD; timezone defaults to UTC. Private responses are not cached. This API never accepts Supabase user/session credentials."
  },
  "servers": [
    {
      "url": "https://keephabits.app/api/v1"
    }
  ],
  "security": [
    {
      "apiToken": []
    }
  ],
  "paths": {
    "/habits": {
      "get": {
        "operationId": "getHabits",
        "summary": "List active habits",
        "description": "Requires habits:read. List ordering is stable; use pagination.has_more to continue.",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 100000,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Habit"
                      }
                    },
                    "pagination": {
                      "$ref": "#/components/schemas/Pagination"
                    }
                  },
                  "required": [
                    "data",
                    "pagination"
                  ]
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters"
          },
          "401": {
            "description": "Missing, invalid, revoked or expired token"
          },
          "403": {
            "description": "Pro inactive or scope missing"
          },
          "404": {
            "description": "Habit unavailable"
          },
          "422": {
            "description": "Invalid date or schedule"
          },
          "429": {
            "description": "Rate limit; wait Retry-After seconds",
            "headers": {
              "Retry-After": {
                "description": "Seconds until retry",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "Temporarily unavailable"
          }
        }
      }
    },
    "/categories": {
      "get": {
        "operationId": "getCategories",
        "summary": "List non-deleted categories",
        "description": "Requires habits:read. List ordering is stable; use pagination.has_more to continue.",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 100000,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Category"
                      }
                    },
                    "pagination": {
                      "$ref": "#/components/schemas/Pagination"
                    }
                  },
                  "required": [
                    "data",
                    "pagination"
                  ]
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters"
          },
          "401": {
            "description": "Missing, invalid, revoked or expired token"
          },
          "403": {
            "description": "Pro inactive or scope missing"
          },
          "404": {
            "description": "Habit unavailable"
          },
          "422": {
            "description": "Invalid date or schedule"
          },
          "429": {
            "description": "Rate limit; wait Retry-After seconds",
            "headers": {
              "Retry-After": {
                "description": "Seconds until retry",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "Temporarily unavailable"
          }
        }
      }
    },
    "/checkins": {
      "get": {
        "operationId": "getCheckins",
        "summary": "List check-ins for a range of at most 365 days",
        "description": "Requires habits:read. List ordering is stable; use pagination.has_more to continue.",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 100000,
              "default": 0
            }
          },
          {
            "name": "from",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "to",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "habit_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "timezone",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "default": "UTC",
              "example": "Europe/Madrid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Checkin"
                      }
                    },
                    "pagination": {
                      "$ref": "#/components/schemas/Pagination"
                    }
                  },
                  "required": [
                    "data",
                    "pagination"
                  ]
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters"
          },
          "401": {
            "description": "Missing, invalid, revoked or expired token"
          },
          "403": {
            "description": "Pro inactive or scope missing"
          },
          "404": {
            "description": "Habit unavailable"
          },
          "422": {
            "description": "Invalid date or schedule"
          },
          "429": {
            "description": "Rate limit; wait Retry-After seconds",
            "headers": {
              "Retry-After": {
                "description": "Seconds until retry",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "Temporarily unavailable"
          }
        }
      }
    },
    "/progress": {
      "get": {
        "operationId": "getProgress",
        "summary": "Get daily progress for the last 365 days",
        "description": "Requires habits:read. List ordering is stable; use pagination.has_more to continue.",
        "parameters": [
          {
            "name": "date",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "timezone",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "default": "UTC",
              "example": "Europe/Madrid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/Progress"
                    }
                  },
                  "required": [
                    "data"
                  ]
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters"
          },
          "401": {
            "description": "Missing, invalid, revoked or expired token"
          },
          "403": {
            "description": "Pro inactive or scope missing"
          },
          "404": {
            "description": "Habit unavailable"
          },
          "422": {
            "description": "Invalid date or schedule"
          },
          "429": {
            "description": "Rate limit; wait Retry-After seconds",
            "headers": {
              "Retry-After": {
                "description": "Seconds until retry",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "Temporarily unavailable"
          }
        }
      }
    },
    "/habits/{id}/checkins/{date}": {
      "put": {
        "operationId": "setCheckin",
        "summary": "Complete or uncomplete a habit (idempotent)",
        "description": "Requires checkins:write. Only the last 365 days in the selected timezone, with no future dates. Cannot complete archived, deleted, paused or unscheduled habits.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "date",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "date"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "completed": {
                    "type": "boolean"
                  },
                  "timezone": {
                    "type": "string",
                    "default": "UTC"
                  }
                },
                "required": [
                  "completed"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Stored check-in",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/Checkin"
                    }
                  },
                  "required": [
                    "data"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters"
          },
          "401": {
            "description": "Missing, invalid, revoked or expired token"
          },
          "403": {
            "description": "Pro inactive or scope missing"
          },
          "404": {
            "description": "Habit unavailable"
          },
          "422": {
            "description": "Invalid date or schedule"
          },
          "429": {
            "description": "Rate limit; wait Retry-After seconds",
            "headers": {
              "Retry-After": {
                "description": "Seconds until retry",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "Temporarily unavailable"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiToken": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "kh_live_…",
        "description": "Opaque scoped API token; not a JWT"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              }
            },
            "required": [
              "code"
            ]
          }
        },
        "required": [
          "error"
        ]
      },
      "Pagination": {
        "type": "object",
        "properties": {
          "limit": {
            "type": "integer",
            "minimum": 1,
            "maximum": 100
          },
          "offset": {
            "type": "integer",
            "minimum": 0
          },
          "has_more": {
            "type": "boolean"
          }
        },
        "required": [
          "limit",
          "offset",
          "has_more"
        ]
      },
      "Habit": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "icon": {
            "type": "string",
            "nullable": true
          },
          "color": {
            "type": "string",
            "nullable": true
          },
          "category_id": {
            "type": "string",
            "format": "uuid",
            "nullable": true
          },
          "frequency_type": {
            "type": "string",
            "enum": [
              "daily",
              "weekly",
              "monthly",
              "custom"
            ]
          },
          "frequency_days": {
            "type": "array",
            "items": {
              "type": "integer"
            },
            "nullable": true
          },
          "target_time": {
            "type": "string",
            "nullable": true
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "pauses": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "start_date": {
                  "type": "string",
                  "format": "date"
                },
                "end_date": {
                  "type": "string",
                  "format": "date",
                  "nullable": true
                }
              },
              "required": [
                "start_date",
                "end_date"
              ]
            }
          }
        },
        "required": [
          "id",
          "name",
          "icon",
          "color",
          "category_id",
          "frequency_type",
          "frequency_days",
          "target_time",
          "created_at",
          "pauses"
        ]
      },
      "Category": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "icon": {
            "type": "string",
            "nullable": true
          },
          "color": {
            "type": "string",
            "nullable": true
          }
        },
        "required": [
          "id",
          "name",
          "icon",
          "color"
        ]
      },
      "Checkin": {
        "type": "object",
        "properties": {
          "habit_id": {
            "type": "string",
            "format": "uuid"
          },
          "date": {
            "type": "string",
            "format": "date"
          },
          "completed": {
            "type": "boolean"
          }
        },
        "required": [
          "habit_id",
          "date",
          "completed"
        ]
      },
      "Progress": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "scheduled": {
            "type": "integer"
          },
          "completed": {
            "type": "integer"
          },
          "percentage": {
            "type": "number",
            "minimum": 0,
            "maximum": 100
          }
        },
        "required": [
          "date",
          "scheduled",
          "completed",
          "percentage"
        ]
      }
    }
  }
}
