{
  "openapi": "3.1.0",
  "info": {
    "title": "Lumikey API",
    "version": "1.0.0",
    "description": "Key checks for your apps and scripts, and the Management API for your own server. Guides: https://lumikey.cc/docs"
  },
  "servers": [{ "url": "https://lumikey.cc/api/v1" }],
  "tags": [
    { "name": "Keys", "description": "Called from your app or script. No secrets needed." },
    { "name": "Management", "description": "Called from your own server with a project API token. Never ship the token to users." }
  ],
  "paths": {
    "/validate": {
      "post": {
        "tags": ["Keys"],
        "summary": "Check a key",
        "description": "Also accepts GET with the same fields in the query string, and form-encoded bodies. A wrong key is a normal 200 answer with valid false.",
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CheckRequest" } } } },
        "responses": {
          "200": { "description": "Result of the check", "headers": { "X-RateLimit-Limit": { "$ref": "#/components/headers/X-RateLimit-Limit" }, "X-RateLimit-Window": { "$ref": "#/components/headers/X-RateLimit-Window" } },"content": { "application/json": { "schema": { "$ref": "#/components/schemas/CheckResult" } } } },
          "429": { "description": "Too many requests (code RATE_LIMITED)", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CheckResult" } } } }
        }
      }
    },
    "/heartbeat": {
      "post": {
        "tags": ["Keys"],
        "summary": "Tell Lumikey a script is still running",
        "description": "Call every next_in seconds. Returns the same fields as a key check. With sharing detection set to enforce, an extra session gets KEY_IN_USE.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "allOf": [
                  { "$ref": "#/components/schemas/CheckRequest" },
                  { "type": "object", "required": ["session"], "properties": { "session": { "type": "string", "pattern": "^[A-Za-z0-9_-]{8,64}$", "description": "Random id your script makes once when it starts." } } }
                ]
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Result", "content": { "application/json": { "schema": { "allOf": [{ "$ref": "#/components/schemas/CheckResult" }, { "type": "object", "properties": { "next_in": { "type": "integer" } } }] } } } },
          "400": { "description": "Missing or bad session id (code BAD_SESSION)" },
          "429": { "description": "Too many requests" }
        }
      }
    },
    "/reset-hwid": {
      "post": {
        "tags": ["Keys"],
        "summary": "Let a key's owner clear its devices",
        "description": "Within the project's reset limit, at most once per 24 hours.",
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/KeyRef" } } } },
        "responses": {
          "200": {
            "description": "Result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "code": { "type": "string", "enum": ["RESET_OK", "NOT_LOCKED", "RESET_DISABLED", "RESET_LIMIT", "RESET_COOLDOWN", "KEY_INVALID", "KEY_REVOKED", "KEY_EXPIRED", "KEY_FROZEN", "BLACKLISTED", "PROJECT_NOT_FOUND"] },
                    "message": { "type": "string" },
                    "resets_left": { "type": "integer" },
                    "retry_after": { "type": "integer" }
                  }
                }
              }
            }
          },
          "429": { "description": "Too many requests" }
        }
      }
    },
    "/key-info": {
      "post": {
        "tags": ["Keys"],
        "summary": "What a key's owner may see about it",
        "description": "Returns PROJECT_NOT_FOUND when the project turned its customer page off. Raw device ids are never returned.",
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/KeyRef" } } } },
        "responses": {
          "200": {
            "description": "Key information",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "code": { "type": "string" },
                    "state": { "type": "string", "enum": ["active", "unused", "expired", "revoked", "frozen"] },
                    "expires_at": { "type": ["integer", "null"] },
                    "starts_on_first_use": { "type": "boolean" },
                    "lifetime": { "type": "boolean" },
                    "tier": { "type": ["string", "null"] },
                    "test": { "type": "boolean" },
                    "max_devices": { "type": "integer" },
                    "devices": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "label": { "type": "string" }, "first_seen": { "type": "integer" }, "last_seen": { "type": "integer" } } } },
                    "resets_left": { "type": "integer" },
                    "reset_cooldown": { "type": "integer" },
                    "can_reset": { "type": "boolean" }
                  }
                }
              }
            }
          },
          "429": { "description": "Too many requests" }
        }
      }
    },
    "/loader/{project}": {
      "get": {
        "tags": ["Keys"],
        "summary": "Hosted Lua script for a valid key",
        "description": "On failure returns a Lua error() statement, so loadstring prints a clear message.",
        "parameters": [
          { "$ref": "#/components/parameters/ProjectPath" },
          { "name": "key", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "hwid", "in": "query", "schema": { "type": "string" } },
          { "name": "version", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "The script", "content": { "text/plain": {} } }, "403": { "description": "Key refused (Lua error statement)" }, "404": { "description": "Unknown project or no script" } }
      }
    },
    "/script/{project}": {
      "get": {
        "tags": ["Keys"],
        "summary": "Hosted file for a valid key",
        "parameters": [
          { "$ref": "#/components/parameters/ProjectPath" },
          { "name": "key", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "hwid", "in": "query", "schema": { "type": "string" } },
          { "name": "version", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "The file", "content": { "text/plain": {} } }, "403": { "description": "Key refused (JSON with code)" }, "404": { "description": "Unknown project or no script" } }
      }
    },
    "/manage/{project}/keys": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }],
      "post": {
        "tags": ["Management"],
        "summary": "Create keys",
        "description": "Scope: create.",
        "security": [{ "token": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["duration"],
                "properties": {
                  "count": { "type": "integer", "minimum": 1, "maximum": 100, "default": 1 },
                  "duration": { "type": "string", "examples": ["30m", "12h", "7d", "1w", "1mo", "lifetime"] },
                  "start_on_first_use": { "type": "boolean", "default": true },
                  "type": { "type": "string", "enum": ["paid", "manual"], "default": "manual" },
                  "note": { "type": "string", "maxLength": 120 },
                  "tier": { "type": ["string", "null"], "pattern": "^[a-z0-9_-]{1,32}$" },
                  "meta": { "$ref": "#/components/schemas/Meta" },
                  "max_devices": { "type": "integer", "minimum": 1, "maximum": 50 },
                  "test": { "type": "boolean", "default": false },
                  "email": { "type": "string", "format": "email" },
                  "send_email": { "type": "boolean", "default": false }
                }
              }
            }
          }
        },
        "responses": {
          "201": { "description": "Created", "content": { "application/json": { "schema": { "type": "object", "properties": { "keys": { "type": "array", "items": { "type": "string" } }, "details": { "type": "array", "items": { "$ref": "#/components/schemas/Key" } }, "emailed": { "type": "boolean" } } } } } },
          "400": { "$ref": "#/components/responses/Error" },
          "403": { "$ref": "#/components/responses/Error" }
        }
      },
      "get": {
        "tags": ["Management"],
        "summary": "List keys",
        "description": "Scope: read.",
        "security": [{ "token": [] }],
        "parameters": [
          { "name": "state", "in": "query", "schema": { "type": "string", "enum": ["active", "unused", "expired", "revoked", "frozen"] } },
          { "name": "tier", "in": "query", "schema": { "type": "string" } },
          { "name": "page", "in": "query", "schema": { "type": "integer", "minimum": 0, "default": 0 } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 50 } }
        ],
        "responses": { "200": { "description": "Keys", "content": { "application/json": { "schema": { "type": "object", "properties": { "keys": { "type": "array", "items": { "$ref": "#/components/schemas/Key" } }, "page": { "type": "integer" }, "limit": { "type": "integer" } } } } } } }
      }
    },
    "/manage/{project}/keys/{key}": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "get": { "tags": ["Management"], "summary": "Get a key with its devices", "description": "Scope: read.", "security": [{ "token": [] }], "responses": { "200": { "description": "Key", "content": { "application/json": { "schema": { "type": "object", "properties": { "key": { "$ref": "#/components/schemas/Key" } } } } } }, "404": { "$ref": "#/components/responses/Error" } } },
      "patch": {
        "tags": ["Management"],
        "summary": "Change a key",
        "description": "Scope: update.",
        "security": [{ "token": [] }],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "note": { "type": "string", "maxLength": 120 },
                  "tier": { "type": ["string", "null"] },
                  "meta": { "$ref": "#/components/schemas/Meta" },
                  "max_devices": { "type": "integer", "minimum": 1, "maximum": 50 },
                  "email": { "type": ["string", "null"], "format": "email" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "Updated key", "content": { "application/json": { "schema": { "type": "object", "properties": { "key": { "$ref": "#/components/schemas/Key" } } } } } } }
      },
      "delete": { "tags": ["Management"], "summary": "Delete a key", "description": "Scope: delete.", "security": [{ "token": [] }], "responses": { "200": { "$ref": "#/components/responses/Ok" } } }
    },
    "/manage/{project}/keys/{key}/history": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "get": {
        "tags": ["Management"],
        "summary": "A key's history (last 30 days)",
        "description": "Scope: read.",
        "security": [{ "token": [] }],
        "parameters": [{ "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 200, "default": 50 } }],
        "responses": {
          "200": {
            "description": "Events, newest first",
            "content": { "application/json": { "schema": { "type": "object", "properties": { "events": { "type": "array", "items": { "type": "object", "properties": { "kind": { "type": "string" }, "hwid": { "type": ["string", "null"] }, "ip": { "type": ["string", "null"] }, "country": { "type": ["string", "null"] }, "detail": { "type": ["string", "null"] }, "created_at": { "type": "integer" } } } } } } } }
          }
        }
      }
    },
    "/manage/{project}/keys/{key}/extend": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "post": {
        "tags": ["Management"],
        "summary": "Add time to a key",
        "description": "Scope: update.",
        "security": [{ "token": [] }],
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "required": ["duration"], "properties": { "duration": { "type": "string" } } } } } },
        "responses": { "200": { "description": "Updated key" } }
      }
    },
    "/manage/{project}/keys/{key}/reset-hwid": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "post": { "tags": ["Management"], "summary": "Clear all devices of a key", "description": "Scope: update. Does not use the customer's reset limit.", "security": [{ "token": [] }], "responses": { "200": { "$ref": "#/components/responses/Ok" } } }
    },
    "/manage/{project}/keys/{key}/devices/{hwid}": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }, { "name": "hwid", "in": "path", "required": true, "schema": { "type": "string" } }],
      "delete": { "tags": ["Management"], "summary": "Remove one device from a key", "description": "Scope: update.", "security": [{ "token": [] }], "responses": { "200": { "$ref": "#/components/responses/Ok" } } }
    },
    "/manage/{project}/keys/{key}/revoke": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "post": { "tags": ["Management"], "summary": "Revoke a key", "description": "Scope: update.", "security": [{ "token": [] }], "responses": { "200": { "$ref": "#/components/responses/Ok" } } }
    },
    "/manage/{project}/keys/{key}/restore": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "post": { "tags": ["Management"], "summary": "Restore a revoked key", "description": "Scope: update.", "security": [{ "token": [] }], "responses": { "200": { "$ref": "#/components/responses/Ok" } } }
    },
    "/manage/{project}/keys/{key}/freeze": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "post": { "tags": ["Management"], "summary": "Pause a key", "description": "Scope: update. Checks return KEY_FROZEN; the time is kept.", "security": [{ "token": [] }], "responses": { "200": { "description": "Updated key" } } }
    },
    "/manage/{project}/keys/{key}/unfreeze": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "$ref": "#/components/parameters/KeyPath" }],
      "post": { "tags": ["Management"], "summary": "Resume a paused key", "description": "Scope: update. The paused time is added back.", "security": [{ "token": [] }], "responses": { "200": { "description": "Updated key" } } }
    },
    "/manage/{project}/freeze-all": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }],
      "post": { "tags": ["Management"], "summary": "Pause every key in the project", "description": "Scope: update.", "security": [{ "token": [] }], "responses": { "200": { "description": "Number of keys paused" } } }
    },
    "/manage/{project}/unfreeze-all": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }],
      "post": { "tags": ["Management"], "summary": "Resume every paused key in the project", "description": "Scope: update.", "security": [{ "token": [] }], "responses": { "200": { "description": "Number of keys resumed" } } }
    },
    "/manage/{project}/blacklist": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }],
      "get": { "tags": ["Management"], "summary": "List bans (all of your projects)", "description": "Scope: read.", "security": [{ "token": [] }], "responses": { "200": { "description": "Bans" } } },
      "post": {
        "tags": ["Management"],
        "summary": "Ban a device id, IP or key",
        "description": "Scope: update. Applies to all of your projects.",
        "security": [{ "token": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["kind", "value"],
                "properties": {
                  "kind": { "type": "string", "enum": ["hwid", "ip", "key"] },
                  "value": { "type": "string", "maxLength": 128 },
                  "reason": { "type": "string", "maxLength": 200 },
                  "duration": { "type": "string", "description": "Leave out for a permanent ban." }
                }
              }
            }
          }
        },
        "responses": { "201": { "description": "Ban added" } }
      }
    },
    "/manage/{project}/blacklist/{id}": {
      "parameters": [{ "$ref": "#/components/parameters/ProjectPath" }, { "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
      "delete": { "tags": ["Management"], "summary": "Remove a ban", "description": "Scope: delete.", "security": [{ "token": [] }], "responses": { "200": { "$ref": "#/components/responses/Ok" } } }
    }
  },
  "components": {
    "securitySchemes": {
      "token": { "type": "http", "scheme": "bearer", "description": "Project API token from the project's Settings. Scopes: read, create, update, delete." }
    },
    "parameters": {
      "ProjectPath": { "name": "project", "in": "path", "required": true, "schema": { "type": "string" } },
      "KeyPath": { "name": "key", "in": "path", "required": true, "schema": { "type": "string" } }
    },
    "headers": {
      "X-RateLimit-Limit": { "schema": { "type": "integer" }, "description": "Requests allowed per window." },
      "X-RateLimit-Window": { "schema": { "type": "integer" }, "description": "Window length in seconds." },
      "Retry-After": { "schema": { "type": "integer" }, "description": "Seconds to wait, sent with 429." }
    },
    "responses": {
      "Ok": { "description": "Done", "content": { "application/json": { "schema": { "type": "object", "properties": { "ok": { "type": "boolean" } } } } } },
      "Error": {
        "description": "Error",
        "content": { "application/json": { "schema": { "type": "object", "properties": { "error": { "type": "object", "properties": { "code": { "type": "string" }, "message": { "type": "string" } } } } } } }
      }
    },
    "schemas": {
      "KeyRef": {
        "type": "object",
        "required": ["project", "key"],
        "properties": { "project": { "type": "string" }, "key": { "type": "string" } }
      },
      "CheckRequest": {
        "type": "object",
        "required": ["project", "key"],
        "properties": {
          "project": { "type": "string" },
          "key": { "type": "string" },
          "hwid": { "type": "string", "maxLength": 128, "description": "Device id. Required when the project locks keys to devices." },
          "version": { "type": "string", "maxLength": 32 },
          "nonce": { "type": "string", "maxLength": 64, "description": "Random value, echoed and covered by the signature." }
        }
      },
      "Meta": {
        "type": ["object", "null"],
        "maxProperties": 20,
        "propertyNames": { "pattern": "^[A-Za-z0-9_]{1,32}$" },
        "additionalProperties": { "type": ["string", "number", "boolean"] }
      },
      "CheckResult": {
        "type": "object",
        "properties": {
          "valid": { "type": "boolean" },
          "code": { "type": "string", "enum": ["KEY_VALID", "KEY_INVALID", "KEY_EXPIRED", "KEY_REVOKED", "KEY_FROZEN", "HWID_MISMATCH", "HWID_REQUIRED", "BLACKLISTED", "GEO_BLOCKED", "VERSION_BLOCKED", "NETWORK_BLOCKED", "KEY_IN_USE", "PROJECT_NOT_FOUND", "PROJECT_PAUSED", "RATE_LIMITED"] },
          "message": { "type": "string" },
          "expires_at": { "type": ["integer", "null"] },
          "seconds_left": { "type": ["integer", "null"] },
          "lifetime": { "type": "boolean" },
          "key_type": { "type": ["string", "null"] },
          "note": { "type": ["string", "null"] },
          "tier": { "type": ["string", "null"] },
          "meta": { "$ref": "#/components/schemas/Meta" },
          "test": { "type": "boolean" },
          "max_devices": { "type": ["integer", "null"] },
          "latest_version": { "type": ["string", "null"] },
          "update_available": { "type": "boolean" },
          "nonce": { "type": ["string", "null"] },
          "signature": { "type": ["string", "null"], "description": "HMAC-SHA256 of code:KEY:hwid:expires_at:nonce with the project's signing secret." },
          "signature_full": { "type": ["string", "null"], "description": "Same, with :tier:meta (compact JSON) added." }
        }
      },
      "Key": {
        "type": "object",
        "properties": {
          "key": { "type": "string" },
          "state": { "type": "string" },
          "type": { "type": "string" },
          "note": { "type": "string" },
          "tier": { "type": ["string", "null"] },
          "meta": { "$ref": "#/components/schemas/Meta" },
          "test": { "type": "boolean" },
          "email": { "type": ["string", "null"] },
          "duration_seconds": { "type": "integer" },
          "expires_at": { "type": ["integer", "null"] },
          "max_devices": { "type": "integer" },
          "hwid": { "type": ["string", "null"] },
          "hwid_resets": { "type": "integer" },
          "devices": { "type": "array", "items": { "type": "object", "properties": { "hwid": { "type": "string" }, "first_seen": { "type": "integer" }, "last_seen": { "type": "integer" } } } },
          "discord_user_id": { "type": ["string", "null"] },
          "uses": { "type": "integer" },
          "last_used_at": { "type": ["integer", "null"] },
          "redeemed_at": { "type": ["integer", "null"] },
          "created_at": { "type": "integer" },
          "source": { "type": "string" }
        }
      }
    }
  }
}
