{
  "openapi": "3.1.0",
  "info": {
    "title": "StudyQuest Developer API",
    "version": "1.0.0-beta",
    "description": "Approval-based beta. Private, account-owned study sets. Text input only; extract PDF/DOCX first. No automatic billing. Hard per-account quotas.",
    "contact": {
      "email": "hello@studyquest.app"
    }
  },
  "servers": [
    {
      "url": "https://jepbczthrhzznwzxrzoz.supabase.co/functions/v1/developer-api/v1"
    }
  ],
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "sq_dev_<64 hex characters>",
        "description": "Create a key in Account > Developer after approval."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message",
              "request_id"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              },
              "request_id": {
                "type": "string"
              }
            }
          }
        }
      },
      "Question": {
        "type": "object",
        "required": [
          "question",
          "options",
          "correct"
        ],
        "properties": {
          "question": {
            "type": "string"
          },
          "options": {
            "type": "array",
            "minItems": 4,
            "maxItems": 4,
            "items": {
              "type": "string"
            }
          },
          "correct": {
            "type": "integer",
            "minimum": 0,
            "maximum": 3
          },
          "topic": {
            "type": "string"
          }
        }
      },
      "Subject": {
        "type": "object",
        "required": [
          "id",
          "name",
          "created_at"
        ],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "StudySet": {
        "type": "object",
        "required": [
          "id",
          "name",
          "question_count",
          "subject_id",
          "created_at",
          "updated_at"
        ],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "subject_id": {
            "type": [
              "string",
              "null"
            ],
            "format": "uuid"
          },
          "question_count": {
            "type": "integer"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          },
          "questions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Question"
            }
          },
          "play_url": {
            "type": "string",
            "format": "uri"
          }
        }
      },
      "Generation": {
        "type": "object",
        "required": [
          "id",
          "status",
          "study_set_id",
          "error_code",
          "play_url",
          "created_at"
        ],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "status": {
            "enum": [
              "processing",
              "succeeded",
              "failed"
            ]
          },
          "study_set_id": {
            "type": [
              "string",
              "null"
            ],
            "format": "uuid"
          },
          "error_code": {
            "type": [
              "string",
              "null"
            ]
          },
          "play_url": {
            "type": [
              "string",
              "null"
            ],
            "format": "uri"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "CreateStudySet": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name",
          "content"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200
          },
          "content": {
            "type": "string",
            "minLength": 50,
            "maxLength": 40000
          },
          "question_count": {
            "type": "integer",
            "minimum": 1,
            "maximum": 20,
            "default": 10
          },
          "language": {
            "type": "string",
            "default": "en",
            "pattern": "^[a-z]{2,3}(?:-[a-zA-Z]{2,4})?$"
          },
          "subject_id": {
            "type": [
              "string",
              "null"
            ],
            "format": "uuid"
          }
        }
      },
      "GenerationResponse": {
        "type": "object",
        "required": [
          "data"
        ],
        "properties": {
          "data": {
            "$ref": "#/components/schemas/Generation"
          },
          "replayed": {
            "type": "boolean"
          }
        }
      },
      "Usage": {
        "type": "object",
        "properties": {
          "period": {
            "type": "string",
            "format": "date"
          },
          "generations_used": {
            "type": "integer"
          },
          "generations_limit": {
            "type": "integer"
          }
        }
      }
    }
  },
  "paths": {
    "/study-sets": {
      "get": {
        "summary": "List your study sets",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:read.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/StudySet"
                      }
                    },
                    "has_more": {
                      "type": "boolean"
                    },
                    "next_offset": {
                      "type": [
                        "integer",
                        "null"
                      ]
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 20
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          },
          {
            "name": "q",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 200
            }
          },
          {
            "name": "subject_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "description": "A subject UUID, or none for unassigned sets."
            }
          }
        ]
      },
      "post": {
        "summary": "Generate and save a study set",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:write. Allow up to 150 seconds. Same key and normalized input replays the original job; changed input returns 409. Failed replays remain failed. Keys are retained for the lifetime of the developer account.",
        "responses": {
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GenerationResponse"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "200": {
            "description": "Idempotent replay of completed or failed generation.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GenerationResponse"
                }
              }
            }
          },
          "202": {
            "description": "Idempotent replay still processing.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GenerationResponse"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateStudySet"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 128
            }
          }
        ]
      }
    },
    "/study-sets/{id}": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string",
            "format": "uuid"
          }
        }
      ],
      "get": {
        "summary": "Read a set and its questions",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:read.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/StudySet"
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "patch": {
        "summary": "Rename a set or change its subject",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:write.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/StudySet"
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "minProperties": 1,
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 200
                  },
                  "subject_id": {
                    "type": [
                      "string",
                      "null"
                    ],
                    "format": "uuid"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/generations/{id}": {
      "get": {
        "summary": "Read generation status",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:read.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GenerationResponse"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ]
      }
    },
    "/subjects": {
      "get": {
        "summary": "List your subjects",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:read.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Subject"
                      }
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create a subject",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:write.",
        "responses": {
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/Subject"
                    }
                  }
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "name"
                ],
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 80
                  }
                }
              }
            }
          }
        }
      }
    },
    "/usage": {
      "get": {
        "summary": "Read monthly generation usage",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "description": "Required scope: study:read.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Usage"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code and X-Request-Id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  }
}
