{
	"openapi": "3.1.0",
	"info": {
		"title": "Sxitch website API",
		"description": "Public API for sxitch.app, the Sxitch macOS app switcher website. Rate limit convention for all endpoints: every response carries RateLimit-Limit, RateLimit-Policy (<limit>;w=<period seconds>) and RateLimit-Reset (seconds until the window resets); a 429 response additionally carries RateLimit-Remaining: 0 and Retry-After. Errors always use a JSON envelope: {\"error\": {\"code\": string, \"message\": string, \"hint\"?: string}}.",
		"version": "1.0.0",
		"contact": {
			"name": "Sxitch support",
			"email": "support@sxitch.app",
			"url": "https://sxitch.app/contact/"
		}
	},
	"servers": [
		{
			"url": "https://sxitch.app",
			"description": "Production"
		},
		{
			"url": "http://127.0.0.1:8787",
			"description": "Local wrangler dev server"
		}
	],
	"paths": {
		"/api/checkout/create": {
			"post": {
				"operationId": "createCheckout",
				"summary": "Create a Polar checkout session for the Sxitch Pro license",
				"description": "Creates an embedded Polar checkout for the fixed Sxitch Pro product ($10 lifetime, 3 Macs). No authentication and no request body required. The success URL redirects to /thanks with the checkout id.",
				"security": [],
				"parameters": [
					{
						"name": "Origin",
						"in": "header",
						"description": "Browser origin used for the checkout success/return URLs. Optional; falls back to Host.",
						"schema": { "type": "string", "format": "uri" }
					}
				],
				"responses": {
					"201": {
						"description": "Checkout created.",
						"headers": {
							"RateLimit-Limit": { "$ref": "#/components/headers/RateLimitLimit" },
							"RateLimit-Policy": { "$ref": "#/components/headers/RateLimitPolicy" },
							"RateLimit-Reset": { "$ref": "#/components/headers/RateLimitReset" }
						},
						"content": {
							"application/json": {
								"schema": {
									"type": "object",
									"properties": {
										"url": { "type": "string", "format": "uri", "description": "Embedded Polar checkout URL." },
										"id": { "type": "string", "description": "Checkout id to pass to getLicenseKeyFromCheckout." }
									},
									"required": ["url", "id"]
								}
							}
						}
					},
					"429": { "$ref": "#/components/responses/RateLimited" },
					"502": { "$ref": "#/components/responses/UpstreamError" }
				}
			}
		},
		"/api/license-key": {
			"get": {
				"operationId": "getLicenseKeyFromCheckout",
				"summary": "Resolve the license key granted for a completed Polar checkout",
				"description": "Called by the /thanks page after a purchase. Returns key: null (HTTP 200) while the checkout is pending, so poll until key is set. Current errors are returned inside an \"error\" envelope while the status stays 200 for compatibility with the thanks page; treat HTTP 500 as the retryable failure case.",
				"security": [],
				"parameters": [
					{
						"name": "checkout_id",
						"in": "query",
						"required": true,
						"description": "Polar checkout id, e.g. cpc_1g8x... from the createCheckout response.",
						"schema": { "type": "string" }
					}
				],
				"responses": {
					"200": {
						"description": "License key lookup result, possibly not ready yet.",
						"headers": {
							"RateLimit-Limit": { "$ref": "#/components/headers/RateLimitLimit" },
							"RateLimit-Policy": { "$ref": "#/components/headers/RateLimitPolicy" },
							"RateLimit-Reset": { "$ref": "#/components/headers/RateLimitReset" }
						},
						"content": {
							"application/json": {
								"schema": {
									"type": "object",
									"properties": {
										"key": { "type": ["string", "null"], "description": "Sxitch Pro license key, or null while pending/unavailable." },
										"error": { "$ref": "#/components/schemas/Error" }
									},
									"required": ["key"]
								}
							}
						}
					},
					"429": { "$ref": "#/components/responses/RateLimited" },
					"500": { "$ref": "#/components/responses/InternalError" }
				}
			}
		},
		"/api/license-info": {
			"post": {
				"operationId": "getLicenseInfo",
				"summary": "Validate a Sxitch license key and return activation info",
				"description": "Validates a license key against Polar and returns the purchase email plus activation usage. Used by the macOS app and the /recover page.",
				"security": [],
				"requestBody": {
					"required": true,
					"content": {
						"application/json": {
							"schema": {
								"type": "object",
								"properties": {
									"licenseKey": { "type": "string", "description": "Sxitch Pro license key" }
								},
								"required": ["licenseKey"]
							},
							"example": { "licenseKey": "AAAA-BBBB-CCCC-DDDD" }
						}
					}
				},
				"responses": {
					"200": {
						"description": "Key validated.",
						"headers": {
							"RateLimit-Limit": { "$ref": "#/components/headers/RateLimitLimit" },
							"RateLimit-Policy": { "$ref": "#/components/headers/RateLimitPolicy" },
							"RateLimit-Reset": { "$ref": "#/components/headers/RateLimitReset" }
						},
						"content": {
							"application/json": {
								"schema": {
									"type": "object",
									"properties": {
										"email": { "type": ["string", "null"], "description": "Purchase email." },
										"activationsUsed": { "type": "integer" },
										"activationLimit": { "type": ["integer", "null"], "description": "Maximum activations; 3 for Pro." }
									},
									"required": ["activationsUsed"]
								}
							}
						}
					},
					"400": { "$ref": "#/components/responses/MissingParameter" },
					"404": { "$ref": "#/components/responses/LicenseNotFound" },
					"429": { "$ref": "#/components/responses/RateLimited" },
					"500": { "$ref": "#/components/responses/InternalError" }
				}
			}
		},
		"/api/license-recover": {
			"post": {
				"operationId": "recoverLicenseKey",
				"summary": "Email a lost license key to its purchaser",
				"description": "Looks up the Polar customer by email and resends the license key email. To prevent abuse, responses do not reveal whether the email is registered: sent is always true on success.",
				"security": [],
				"requestBody": {
					"required": true,
					"content": {
						"application/json": {
							"schema": {
								"type": "object",
								"properties": {
									"email": { "type": "string", "format": "email" }
								},
								"required": ["email"]
							},
							"example": { "email": "you@example.com" }
						}
					}
				},
				"responses": {
					"200": {
						"description": "Accepted. sent: true does not confirm delivery; delivery is only attempted for registered customers.",
						"headers": {
							"RateLimit-Limit": { "$ref": "#/components/headers/RateLimitLimit" },
							"RateLimit-Policy": { "$ref": "#/components/headers/RateLimitPolicy" },
							"RateLimit-Reset": { "$ref": "#/components/headers/RateLimitReset" }
						},
						"content": {
							"application/json": {
								"schema": {
									"type": "object",
									"properties": { "sent": { "type": "boolean" } },
									"required": ["sent"]
								}
							}
						}
					},
					"400": { "$ref": "#/components/responses/MissingParameter" },
					"429": { "$ref": "#/components/responses/RateLimited" },
					"500": { "$ref": "#/components/responses/InternalError" }
				}
			}
		},
		"/api/webhooks/polar": {
			"post": {
				"operationId": "polarWebhook",
				"summary": "Polar order webhook (internal)",
				"description": "Receives order.paid events from Polar, verifies them with standardwebhooks (webhook-uid, webhook-timestamp, webhook-signature headers), and emails the license key to the buyer. Operated by Sxitch itself; third parties cannot use it because it rejects unsigned payloads.",
				"security": [],
				"parameters": [
					{ "name": "webhook-uid", "in": "header", "schema": { "type": "string" } },
					{ "name": "webhook-timestamp", "in": "header", "schema": { "type": "string" } },
					{ "name": "webhook-signature", "in": "header", "schema": { "type": "string" } }
				],
				"responses": {
					"200": {
						"description": "Event accepted or intentionally ignored.",
						"content": {
							"application/json": {
								"schema": {
									"type": "object",
									"properties": {
										"received": { "type": "boolean" },
										"ignored": { "type": "string" }
									}
								}
							}
						}
					},
					"400": {
						"description": "Unparseable payload.",
						"content": {
							"application/json": {
								"schema": { "$ref": "#/components/schemas/ErrorEnvelope" }
							}
						}
					},
					"401": {
						"description": "Failed signature verification.",
						"content": {
							"application/json": {
								"schema": { "$ref": "#/components/schemas/ErrorEnvelope" }
							}
						}
					},
					"500": { "$ref": "#/components/responses/InternalError" }
				}
			}
		}
	},
	"components": {
		"headers": {
			"RateLimitLimit": {
				"description": "Maximum requests per window for this endpoint.",
				"schema": { "type": "integer" }
			},
			"RateLimitPolicy": {
				"description": "Quoted policy in the form <limit>;w=<period seconds>.",
				"schema": { "type": "string", "example": "30;w=60" }
			},
			"RateLimitReset": {
				"description": "Seconds until the current window resets.",
				"schema": { "type": "integer" }
			}
		},
		"schemas": {
			"Error": {
				"type": "object",
				"properties": {
					"code": {
						"type": "string",
						"description": "Stable machine-readable code.",
						"enum": [
							"missing_parameter",
							"invalid_email",
							"invalid_signature",
							"invalid_payload",
							"not_found",
							"checkout_not_ready",
							"checkout_missing_customer",
							"key_not_found",
							"rate_limited",
							"upstream_error",
							"internal_error"
						]
					},
					"message": { "type": "string", "description": "Human-readable error summary." },
					"hint": { "type": "string", "description": "Resolution hint for the caller." }
				},
				"required": ["code", "message"]
			},
			"ErrorEnvelope": {
				"type": "object",
				"properties": {
					"error": { "$ref": "#/components/schemas/Error" }
				},
				"required": ["error"]
			}
		},
		"responses": {
			"RateLimited": {
				"description": "Rate limited.",
				"headers": {
					"RateLimit-Limit": { "$ref": "#/components/headers/RateLimitLimit" },
					"RateLimit-Policy": { "$ref": "#/components/headers/RateLimitPolicy" },
					"RateLimit-Remaining": {
						"description": "Always 0 on a rejected request.",
						"schema": { "type": "integer" }
					},
					"RateLimit-Reset": { "$ref": "#/components/headers/RateLimitReset" },
					"Retry-After": {
						"description": "Seconds after which to retry; mirrors RateLimit-Reset.",
						"schema": { "type": "integer" }
					}
				},
				"content": {
					"application/json": {
						"schema": {
							"type": "object",
							"properties": {
								"error": {
									"allOf": [{ "$ref": "#/components/schemas/Error" }],
									"properties": { "code": { "const": "rate_limited" } }
								}
							}
						}
					}
				}
			},
			"MissingParameter": {
				"description": "A required parameter is missing or malformed.",
				"content": {
					"application/json": {
						"schema": { "$ref": "#/components/schemas/ErrorEnvelope" }
					}
				}
			},
			"LicenseNotFound": {
				"description": "License key not valid for Sxitch.",
				"content": {
					"application/json": {
						"schema": { "$ref": "#/components/schemas/ErrorEnvelope" }
					}
				}
			},
			"UpstreamError": {
				"description": "Payment processor call failed.",
				"content": {
					"application/json": {
						"schema": { "$ref": "#/components/schemas/ErrorEnvelope" }
					}
				}
			},
			"InternalError": {
				"description": "Unexpected server error.",
				"content": {
					"application/json": {
						"schema": { "$ref": "#/components/schemas/ErrorEnvelope" }
					}
				}
			}
		}
	}
}
