{
  "openapi": "3.1.0",
  "info": {
    "title": "Kingshot Auto Redeemer Developer API",
    "version": "4.0.0",
    "description": "V4 is the current developer API for player lookup and manual redemption. It provides a consistent success/data/error/meta envelope, per-request tracing, machine-readable retry guidance, quota metadata, and V4-scoped idempotency. V3 remains available for existing integrations; V1 routes are deprecated. Keep API keys server-side and use only with player accounts you are authorized to access. Public JSON responses include top-level dev attribution. Full guide: /developer-v4.md.",
    "termsOfService": "https://app.ks-redeemer.workers.dev/terms?service=kingshot-auto",
    "contact": {
      "name": "Judson",
      "url": "https://discord.com/users/871756466900598815",
      "x-discord-handle": "@judson00713"
    }
  },
  "servers": [
    {
      "url": "https://app.ks-redeemer.workers.dev"
    }
  ],
  "tags": [
    {
      "name": "Developer API",
      "description": "Bearer-key protected integration endpoints."
    },
    {
      "name": "Enrollment",
      "description": "Public website enrollment flow, separate from developer API key access."
    }
  ],
  "paths": {
    "/api/v1/player": {
      "get": {
        "tags": [
          "Developer API"
        ],
        "summary": "Look up a player profile",
        "description": "Legacy V1 route retained for compatibility. New integrations should use GET /api/v4/players/{playerId}.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "description": "Numeric Kingshot Player ID (5–20 digits).",
            "schema": {
              "type": "string",
              "pattern": "^[0-9]{5,20}$",
              "minLength": 5,
              "maxLength": 20
            },
            "example": "43180889"
          }
        ],
        "responses": {
          "200": {
            "description": "Lookup completed; V1 is deprecated, and responses include deprecation headers pointing to V3 JSON responses include the top-level dev attribution object.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PlayerLookupResponse"
                }
              }
            }
          },
          "400": {
            "description": "Missing or invalid Player ID JSON responses include the top-level dev attribution object."
          },
          "401": {
            "description": "Missing, invalid, or revoked API key JSON responses include the top-level dev attribution object."
          },
          "404": {
            "description": "Player not found JSON responses include the top-level dev attribution object."
          },
          "429": {
            "description": "Rate limit or quota exceeded JSON responses include the top-level dev attribution object."
          },
          "502": {
            "description": "Upstream provider unavailable JSON responses include the top-level dev attribution object."
          }
        },
        "deprecated": true
      }
    },
    "/api/v1/redeem": {
      "post": {
        "tags": [
          "Developer API"
        ],
        "summary": "Manually redeem a gift code",
        "description": "Legacy V1 route retained for compatibility. New integrations should use POST /api/v4/redemptions. Inspect the response body; HTTP success alone does not guarantee rewards were granted.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ManualRedemptionRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Request processed; inspect operation result in response body; V1 is deprecated, and responses include deprecation headers pointing to V3 JSON responses include the top-level dev attribution object."
          },
          "400": {
            "description": "Invalid request fields JSON responses include the top-level dev attribution object."
          },
          "401": {
            "description": "Missing, invalid, or revoked API key JSON responses include the top-level dev attribution object."
          },
          "429": {
            "description": "Rate limit or quota exceeded JSON responses include the top-level dev attribution object."
          },
          "502": {
            "description": "Upstream provider unavailable JSON responses include the top-level dev attribution object."
          }
        },
        "deprecated": true
      }
    },
    "/api/kingshot-register": {
      "post": {
        "tags": [
          "Enrollment"
        ],
        "summary": "Enroll a player in automatic redemption",
        "description": "Public website enrollment route, separate from the Bearer-key developer API. Does not start or control the worker and does not guarantee code validity.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EnrollmentRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Enrollment request processed JSON responses include the top-level dev attribution object."
          },
          "400": {
            "description": "Invalid request fields JSON responses include the top-level dev attribution object."
          },
          "429": {
            "description": "Rate limited JSON responses include the top-level dev attribution object."
          }
        }
      }
    },
    "/api/v3/players/{playerId}": {
      "get": {
        "tags": [
          "Developer API"
        ],
        "operationId": "getPlayerV3",
        "summary": "Look up a player profile (V3)",
        "description": "V3 compatibility endpoint for existing integrations. New integrations should use GET /api/v4/players/{playerId}. Requires an active developer Bearer key.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "playerId",
            "in": "path",
            "required": true,
            "description": "Numeric Kingshot Player ID (5–20 digits).",
            "schema": {
              "type": "string",
              "pattern": "^[0-9]{5,20}$",
              "minLength": 5,
              "maxLength": 20
            },
            "example": "43180889"
          }
        ],
        "responses": {
          "200": {
            "description": "Player lookup completed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3PlayerResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid Player ID",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Player not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "429": {
            "description": "Rate limit or quota exceeded",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "502": {
            "description": "Upstream provider unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "503": {
            "description": "Authorization or provider configuration unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "504": {
            "description": "Upstream request timed out",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/api/v3/redemptions": {
      "post": {
        "tags": [
          "Developer API"
        ],
        "operationId": "createRedemptionV3",
        "summary": "Submit a manual gift-code redemption (V3)",
        "description": "V3 compatibility endpoint for existing integrations. New integrations should use POST /api/v4/redemptions. Inspect data.ok and data.status; HTTP 200 means the operation was processed, not necessarily that rewards were granted.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ApiV3RedemptionRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Redemption operation completed; inspect data for the in-game outcome",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3RedemptionResponse"
                }
              }
            },
            "headers": {
              "Idempotency-Replayed": {
                "description": "True when a stored outcome is replayed",
                "schema": {
                  "type": "string",
                  "enum": [
                    "true"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Invalid request body",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid or revoked API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "405": {
            "description": "Method not allowed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency key conflict or request still processing",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "415": {
            "description": "Content-Type must be application/json",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "429": {
            "description": "Rate limit or quota exceeded",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "502": {
            "description": "Upstream provider unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "503": {
            "description": "Authorization or provider configuration unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          },
          "504": {
            "description": "Upstream request timed out",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV3ErrorResponse"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Optional durable replay protection for 24 hours; conflicting reuse returns 409.",
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 128,
              "pattern": "^[A-Za-z0-9._:-]{8,128}$"
            },
            "example": "redeem-43180889-example-001"
          }
        ]
      }
    },
    "/api/v4/players/{playerId}": {
      "get": {
        "tags": [
          "Developer API"
        ],
        "operationId": "getPlayerV4",
        "summary": "Look up a player profile (V4)",
        "description": "Returns a normalized player profile using the stable V4 response envelope. success describes the API request, data contains the player, error is null on success, and meta describes the current HTTP attempt. Requires an active developer Bearer key. Transient read failures may be retried with bounded backoff when error.retryable is true and error.retryHint is bounded_backoff.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "playerId",
            "in": "path",
            "required": true,
            "description": "Numeric Kingshot Player ID (5–20 digits).",
            "schema": {
              "type": "string",
              "pattern": "^[0-9]{5,20}$",
              "minLength": 5,
              "maxLength": 20
            },
            "example": "43180889"
          }
        ],
        "responses": {
          "200": {
            "description": "Successful API response.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4PlayerResponse"
                },
                "example": {
                  "success": true,
                  "data": {
                    "playerId": "43180889",
                    "name": "Example Player",
                    "kingdomId": "1125",
                    "power": 123456789,
                    "townCenterLevel": 30,
                    "avatarUrl": null,
                    "allianceName": "Example Alliance",
                    "allianceTag": "EXA"
                  },
                  "error": null,
                  "meta": {
                    "apiVersion": "4",
                    "requestId": "server-generated-id",
                    "timestamp": "2026-10-26T10:00:00.000Z",
                    "source": "mightpulse",
                    "stale": false,
                    "rateLimit": {
                      "perMinuteLimit": 60,
                      "perMinuteRemaining": 59,
                      "perDayLimit": 1000,
                      "perDayRemaining": 999
                    }
                  },
                  "dev": {
                    "name": "Judson",
                    "discord": "@judson00713",
                    "url": "https://discord.com/users/871756466900598815"
                  }
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request data.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid, or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "404": {
            "description": "Player not found.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                },
                "example": {
                  "success": false,
                  "data": null,
                  "error": {
                    "code": "player_not_found",
                    "message": "No player was found for this ID.",
                    "retryable": false,
                    "retryHint": "do_not_retry"
                  },
                  "meta": {
                    "apiVersion": "4",
                    "requestId": "server-generated-id",
                    "timestamp": "2026-10-26T10:00:00.000Z"
                  },
                  "dev": {
                    "name": "Judson",
                    "discord": "@judson00713",
                    "url": "https://discord.com/users/871756466900598815"
                  }
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "405": {
            "description": "Unsupported method.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "429": {
            "description": "Per-key, per-IP, or daily quota exceeded.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                },
                "example": {
                  "success": false,
                  "data": null,
                  "error": {
                    "code": "rate_limit_exceeded",
                    "message": "The per-key or per-IP request limit has been reached.",
                    "retryable": true,
                    "retryHint": "honor_retry_after",
                    "retryAfterSeconds": 15
                  },
                  "meta": {
                    "apiVersion": "4",
                    "requestId": "server-generated-id",
                    "timestamp": "2026-10-26T10:00:00.000Z",
                    "resetAt": "2026-10-26T10:00:15.000Z"
                  },
                  "dev": {
                    "name": "Judson",
                    "discord": "@judson00713",
                    "url": "https://discord.com/users/871756466900598815"
                  }
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              },
              "Retry-After": {
                "description": "Preferred wait time before retrying, in seconds. The JSON error may also expose retryAfterSeconds.",
                "schema": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            }
          },
          "502": {
            "description": "Upstream provider unavailable or returned invalid data.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "503": {
            "description": "Authorization, provider configuration, or idempotency storage unavailable.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "504": {
            "description": "Upstream request timed out; redemption outcome may be ambiguous.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          }
        }
      }
    },
    "/api/v4/redemptions": {
      "post": {
        "tags": [
          "Developer API"
        ],
        "operationId": "createRedemptionV4",
        "summary": "Submit a manual gift-code redemption (V4)",
        "description": "Submits one player/code/kingdom combination using the V4 response envelope. HTTP-level success means the API processed the request; check data.ok and data.status for the game outcome. Send a stable Idempotency-Key for each logical redemption. If the upstream result is ambiguous, retry only with the same key and identical body. V4 idempotency records are separated from V3 records. Errors include retryable and retryHint fields to guide clients.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Recommended for every logical redemption request. Use a stable, high-entropy key of 8–128 safe ASCII characters and reuse it only for retries of the exact same operation and body. A different body with the same key returns 409. Conflicting/repeated header values are rejected. V4 key space is isolated from V3.",
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 128,
              "pattern": "^[A-Za-z0-9._:-]{8,128}$"
            },
            "example": "redeem-43180889-example-001"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ApiV4RedemptionRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successful API response.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4RedemptionResponse"
                },
                "example": {
                  "success": true,
                  "data": {
                    "ok": false,
                    "status": "ERROR",
                    "statusLabel": "Already redeemed",
                    "message": "This code has already been redeemed.",
                    "errorCategory": "ALREADY_REDEEMED",
                    "errCode": null
                  },
                  "error": null,
                  "meta": {
                    "apiVersion": "4",
                    "requestId": "server-generated-id",
                    "timestamp": "2026-10-26T10:00:00.000Z",
                    "rateLimit": {
                      "perMinuteLimit": 60,
                      "perMinuteRemaining": 59,
                      "perDayLimit": 1000,
                      "perDayRemaining": 999
                    }
                  },
                  "dev": {
                    "name": "Judson",
                    "discord": "@judson00713",
                    "url": "https://discord.com/users/871756466900598815"
                  }
                }
              }
            },
            "headers": {
              "Idempotency-Replayed": {
                "description": "True when an earlier V4 outcome is replayed.",
                "schema": {
                  "type": "string",
                  "enum": [
                    "true"
                  ]
                }
              },
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request data.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "401": {
            "description": "Missing, invalid, or revoked API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "405": {
            "description": "Unsupported method.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency key conflict or same-key request still processing.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "415": {
            "description": "Redemption body must use application/json.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "429": {
            "description": "Per-key, per-IP, or daily quota exceeded.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                },
                "example": {
                  "success": false,
                  "data": null,
                  "error": {
                    "code": "rate_limit_exceeded",
                    "message": "The per-key or per-IP request limit has been reached.",
                    "retryable": true,
                    "retryHint": "honor_retry_after",
                    "retryAfterSeconds": 15
                  },
                  "meta": {
                    "apiVersion": "4",
                    "requestId": "server-generated-id",
                    "timestamp": "2026-10-26T10:00:00.000Z",
                    "resetAt": "2026-10-26T10:00:15.000Z"
                  },
                  "dev": {
                    "name": "Judson",
                    "discord": "@judson00713",
                    "url": "https://discord.com/users/871756466900598815"
                  }
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              },
              "Retry-After": {
                "description": "Preferred wait time before retrying, in seconds. The JSON error may also expose retryAfterSeconds.",
                "schema": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            }
          },
          "502": {
            "description": "Upstream provider unavailable or returned invalid data.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "503": {
            "description": "Authorization, provider configuration, or idempotency storage unavailable.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          },
          "504": {
            "description": "Upstream request timed out; redemption outcome may be ambiguous.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiV4ErrorResponse"
                }
              }
            },
            "headers": {
              "X-Request-Id": {
                "description": "Unique request identifier, also included in meta.requestId.",
                "schema": {
                  "type": "string",
                  "format": "uuid"
                }
              },
              "X-API-Version": {
                "description": "API response contract version.",
                "schema": {
                  "type": "string",
                  "const": "4"
                }
              }
            }
          }
        }
      }
    },
    "/api/v4/alliances": {
      "get": {
        "tags": [
          "Developer API"
        ],
        "summary": "Read an alliance roster or kingdom rankings",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "kingdomId",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^\\\\d{1,10}$"
            }
          },
          {
            "name": "tag",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 10000,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response"
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Missing, invalid, expired, or revoked API key"
          },
          "403": {
            "description": "Required scope missing"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "503": {
            "description": "Feature temporarily unavailable"
          }
        }
      }
    },
    "/api/v4/redemption-history": {
      "get": {
        "tags": [
          "Developer API"
        ],
        "summary": "Read stored player redemption history",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "playerId",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^\\\\d{5,20}$"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 25
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 10000,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response"
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Missing, invalid, expired, or revoked API key"
          },
          "403": {
            "description": "Required scope missing"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "503": {
            "description": "Feature temporarily unavailable"
          }
        }
      }
    },
    "/api/v4/webhooks": {
      "get": {
        "tags": [
          "Developer API"
        ],
        "summary": "List webhook subscriptions",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response"
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Missing, invalid, expired, or revoked API key"
          },
          "403": {
            "description": "Required scope missing"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "503": {
            "description": "Feature temporarily unavailable"
          }
        }
      },
      "post": {
        "tags": [
          "Developer API"
        ],
        "summary": "Create webhook subscription",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url",
                  "events"
                ],
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri"
                  },
                  "events": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "redemption.completed",
                        "redemption.unknown"
                      ]
                    },
                    "minItems": 1,
                    "maxItems": 2
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Subscription created; signing secret shown once"
          },
          "400": {
            "description": "Invalid subscription"
          },
          "503": {
            "description": "Storage or encryption unavailable"
          }
        }
      }
    },
    "/api/v4/webhooks/{webhookId}": {
      "delete": {
        "tags": [
          "Developer API"
        ],
        "summary": "Disable webhook subscription",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "webhookId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response"
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Missing, invalid, expired, or revoked API key"
          },
          "403": {
            "description": "Required scope missing"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "503": {
            "description": "Feature temporarily unavailable"
          }
        }
      }
    },
    "/api/v4/usage": {
      "get": {
        "tags": [
          "Developer API"
        ],
        "summary": "Read compact usage for current key",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response"
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Missing, invalid, expired, or revoked API key"
          },
          "403": {
            "description": "Required scope missing"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "503": {
            "description": "Feature temporarily unavailable"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API key",
        "description": "Create a key from the developer portal. Keep it secret and send it only over HTTPS."
      },
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API key"
      }
    },
    "schemas": {
      "PlayerLookupResponse": {
        "type": "object",
        "properties": {
          "data": {
            "type": "object",
            "properties": {
              "playerId": {
                "type": "string"
              },
              "name": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "kingdomId": {
                "type": [
                  "string",
                  "number",
                  "null"
                ]
              },
              "power": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "townCenterLevel": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "avatarUrl": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "uri"
              },
              "allianceName": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "Current alliance name, when available."
              },
              "allianceTag": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "Current alliance abbreviation/tag, when available."
              }
            }
          },
          "meta": {
            "type": "object",
            "properties": {
              "requestId": {
                "type": "string"
              },
              "remaining": {
                "type": "integer"
              },
              "dailyRemaining": {
                "type": "integer"
              }
            }
          },
          "dev": {
            "$ref": "#/components/schemas/DeveloperAttribution"
          }
        },
        "required": [
          "data",
          "dev"
        ]
      },
      "ManualRedemptionRequest": {
        "type": "object",
        "required": [
          "playerId",
          "code",
          "kid"
        ],
        "properties": {
          "playerId": {
            "type": "string",
            "pattern": "^[0-9]{5,20}$"
          },
          "code": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64
          },
          "kid": {
            "type": "string",
            "pattern": "^[0-9]{1,10}$"
          }
        },
        "additionalProperties": false
      },
      "EnrollmentRequest": {
        "type": "object",
        "required": [
          "playerId"
        ],
        "properties": {
          "playerId": {
            "type": "string",
            "pattern": "^[0-9]{5,20}$"
          },
          "kingdomId": {
            "type": "string",
            "pattern": "^[0-9]{1,10}$"
          },
          "playerName": {
            "type": "string"
          },
          "avatarUrl": {
            "type": "string",
            "format": "uri"
          }
        },
        "additionalProperties": false
      },
      "ApiV3RateLimit": {
        "type": "object",
        "properties": {
          "perMinuteLimit": {
            "type": "integer"
          },
          "perMinuteRemaining": {
            "type": "integer"
          },
          "perDayLimit": {
            "type": "integer"
          },
          "perDayRemaining": {
            "type": "integer"
          },
          "resetAt": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": true
      },
      "ApiV3Meta": {
        "type": "object",
        "required": [
          "apiVersion",
          "requestId",
          "timestamp"
        ],
        "properties": {
          "apiVersion": {
            "type": "string",
            "const": "3"
          },
          "requestId": {
            "type": "string"
          },
          "timestamp": {
            "type": "string",
            "format": "date-time"
          },
          "fetchedAt": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "source": {
            "type": "string"
          },
          "stale": {
            "type": [
              "boolean",
              "null"
            ]
          },
          "providerFresh": {
            "type": [
              "boolean",
              "null"
            ]
          },
          "providerAgeSeconds": {
            "type": [
              "integer",
              "null"
            ]
          },
          "rateLimit": {
            "$ref": "#/components/schemas/ApiV3RateLimit"
          },
          "reason": {
            "type": "string"
          },
          "resetAt": {
            "type": "string",
            "format": "date-time"
          },
          "documentation": {
            "type": "string"
          }
        },
        "additionalProperties": true
      },
      "ApiV3ErrorResponse": {
        "type": "object",
        "required": [
          "error",
          "meta",
          "dev"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            },
            "additionalProperties": false
          },
          "meta": {
            "$ref": "#/components/schemas/ApiV3Meta"
          },
          "dev": {
            "$ref": "#/components/schemas/DeveloperAttribution"
          }
        },
        "additionalProperties": false
      },
      "ApiV3PlayerResponse": {
        "type": "object",
        "required": [
          "data",
          "meta",
          "dev"
        ],
        "properties": {
          "data": {
            "type": "object",
            "required": [
              "playerId",
              "name",
              "kingdomId",
              "power",
              "townCenterLevel",
              "avatarUrl"
            ],
            "properties": {
              "playerId": {
                "type": "string"
              },
              "name": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "kingdomId": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "power": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "townCenterLevel": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "avatarUrl": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "uri"
              },
              "allianceName": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "Current alliance name, when available."
              },
              "allianceTag": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "Current alliance abbreviation/tag, when available."
              }
            },
            "additionalProperties": false
          },
          "meta": {
            "$ref": "#/components/schemas/ApiV3Meta"
          },
          "dev": {
            "$ref": "#/components/schemas/DeveloperAttribution"
          }
        },
        "additionalProperties": false
      },
      "ApiV3RedemptionRequest": {
        "type": "object",
        "required": [
          "playerId",
          "code",
          "kid"
        ],
        "properties": {
          "playerId": {
            "type": "string",
            "pattern": "^[0-9]{5,20}$"
          },
          "code": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_-]{1,64}$",
            "minLength": 1,
            "maxLength": 64
          },
          "kid": {
            "type": "string",
            "pattern": "^[0-9]{1,10}$"
          }
        },
        "additionalProperties": false
      },
      "ApiV3RedemptionResponse": {
        "type": "object",
        "required": [
          "data",
          "meta",
          "dev"
        ],
        "properties": {
          "data": {
            "type": "object",
            "required": [
              "ok",
              "status",
              "statusLabel",
              "message",
              "errorCategory",
              "errCode"
            ],
            "properties": {
              "ok": {
                "type": "boolean"
              },
              "status": {
                "type": "string"
              },
              "statusLabel": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "message": {
                "type": "string"
              },
              "errorCategory": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "errCode": {
                "type": [
                  "string",
                  "number",
                  "null"
                ]
              }
            },
            "additionalProperties": false
          },
          "meta": {
            "$ref": "#/components/schemas/ApiV3Meta"
          },
          "dev": {
            "$ref": "#/components/schemas/DeveloperAttribution"
          }
        },
        "additionalProperties": false
      },
      "DeveloperAttribution": {
        "type": "object",
        "required": [
          "name",
          "discord",
          "url"
        ],
        "properties": {
          "name": {
            "type": "string",
            "const": "Judson",
            "description": "Project developer and maintainer."
          },
          "discord": {
            "type": "string",
            "const": "@judson00713",
            "description": "Public Discord handle."
          },
          "url": {
            "type": "string",
            "format": "uri",
            "const": "https://discord.com/users/871756466900598815",
            "description": "Direct Discord profile URL."
          }
        },
        "additionalProperties": false
      },
      "ApiV4Meta": {
        "type": "object",
        "required": [
          "apiVersion",
          "requestId",
          "timestamp"
        ],
        "properties": {
          "apiVersion": {
            "type": "string",
            "const": "4"
          },
          "requestId": {
            "type": "string",
            "description": "Unique identifier for the current HTTP attempt; equals the X-Request-Id response header, including when a stored redemption outcome is replayed.",
            "format": "uuid"
          },
          "timestamp": {
            "type": "string",
            "format": "date-time"
          },
          "rateLimit": {
            "$ref": "#/components/schemas/ApiV3RateLimit"
          },
          "fetchedAt": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "source": {
            "type": "string"
          },
          "stale": {
            "type": [
              "boolean",
              "null"
            ]
          },
          "providerFresh": {
            "type": [
              "boolean",
              "null"
            ]
          },
          "providerAgeSeconds": {
            "type": [
              "integer",
              "null"
            ]
          },
          "reason": {
            "type": "string"
          },
          "resetAt": {
            "type": "string",
            "format": "date-time"
          },
          "documentation": {
            "type": "string"
          }
        },
        "additionalProperties": true
      },
      "ApiV4Error": {
        "type": "object",
        "required": [
          "code",
          "message",
          "retryable",
          "retryHint"
        ],
        "properties": {
          "code": {
            "type": "string",
            "description": "Stable machine-readable error code."
          },
          "message": {
            "type": "string",
            "description": "Human-readable explanation safe to show to an API client."
          },
          "retryable": {
            "type": "boolean",
            "description": "Server hint indicating whether retrying may be reasonable. Always follow retryHint; this is not a guarantee that the upstream operation is safe to repeat."
          },
          "retryHint": {
            "type": "string",
            "enum": [
              "do_not_retry",
              "honor_retry_after",
              "bounded_backoff",
              "same_idempotency_key"
            ],
            "description": "Safe retry strategy: do_not_retry for permanent input/auth/conflict errors; honor_retry_after for rate limits; bounded_backoff for transient reads; same_idempotency_key for redemption retries whose outcome may be ambiguous."
          },
          "retryAfterSeconds": {
            "type": "integer",
            "minimum": 1,
            "description": "Suggested wait before retrying when the server knows the quota reset. Prefer the Retry-After response header when present."
          }
        },
        "additionalProperties": false
      },
      "ApiV4ErrorResponse": {
        "type": "object",
        "required": [
          "success",
          "data",
          "error",
          "meta",
          "dev"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": false
          },
          "data": {
            "type": "null"
          },
          "error": {
            "$ref": "#/components/schemas/ApiV4Error"
          },
          "meta": {
            "$ref": "#/components/schemas/ApiV4Meta"
          },
          "dev": {
            "$ref": "#/components/schemas/DeveloperAttribution"
          }
        },
        "additionalProperties": false
      },
      "ApiV4PlayerResponse": {
        "type": "object",
        "required": [
          "success",
          "data",
          "error",
          "meta",
          "dev"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true,
            "description": "The API request completed successfully."
          },
          "data": {
            "type": "object",
            "required": [
              "playerId",
              "name",
              "kingdomId",
              "power",
              "townCenterLevel",
              "avatarUrl"
            ],
            "properties": {
              "playerId": {
                "type": "string"
              },
              "name": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "kingdomId": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "power": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "townCenterLevel": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "avatarUrl": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "uri"
              },
              "allianceName": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "Current alliance name, when available."
              },
              "allianceTag": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "Current alliance abbreviation/tag, when available."
              }
            },
            "additionalProperties": false
          },
          "error": {
            "type": "null"
          },
          "meta": {
            "$ref": "#/components/schemas/ApiV4Meta"
          },
          "dev": {
            "$ref": "#/components/schemas/DeveloperAttribution"
          }
        },
        "additionalProperties": false
      },
      "ApiV4RedemptionResponse": {
        "type": "object",
        "required": [
          "success",
          "data",
          "error",
          "meta",
          "dev"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true,
            "description": "The API request was processed. Check data.ok for the in-game redemption outcome."
          },
          "data": {
            "type": "object",
            "required": [
              "ok",
              "status",
              "statusLabel",
              "message",
              "errorCategory",
              "errCode"
            ],
            "properties": {
              "ok": {
                "type": "boolean"
              },
              "status": {
                "type": "string"
              },
              "statusLabel": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "message": {
                "type": "string"
              },
              "errorCategory": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "errCode": {
                "type": [
                  "string",
                  "number",
                  "null"
                ]
              }
            },
            "additionalProperties": false
          },
          "error": {
            "type": "null"
          },
          "meta": {
            "$ref": "#/components/schemas/ApiV4Meta"
          },
          "dev": {
            "$ref": "#/components/schemas/DeveloperAttribution"
          }
        },
        "additionalProperties": false
      },
      "ApiV4RedemptionRequest": {
        "type": "object",
        "required": [
          "playerId",
          "code",
          "kid"
        ],
        "properties": {
          "playerId": {
            "type": "string",
            "pattern": "^[0-9]{5,20}$",
            "minLength": 5,
            "maxLength": 20,
            "description": "Numeric Kingshot Player ID as a string."
          },
          "code": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_-]{1,64}$",
            "minLength": 1,
            "maxLength": 64,
            "description": "Gift code. Only letters, digits, underscores, and hyphens are accepted."
          },
          "kid": {
            "type": "string",
            "pattern": "^[0-9]{1,10}$",
            "description": "Kingdom ID as a string."
          }
        },
        "additionalProperties": false
      }
    }
  },
  "externalDocs": {
    "description": "Interactive developer documentation",
    "url": "https://app.ks-redeemer.workers.dev/developer"
  }
}
