{
  "openapi": "3.1.0",
  "info": {
    "title": "JustLab AI API",
    "version": "1.0.0",
    "description": "Unified AI API for text, vision, audio/speech, and video generation. Supports synchronous and asynchronous (job-based) flows, including batch processing and webhooks.",
    "termsOfService": "https://justlab.ai/terms",
    "contact": { "email": "support@justlab.ai", "url": "https://justlab.ai" },
    "license": { "name": "Apache-2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0" }
  },
  "servers": [
    {
      "url": "https://{projectId}.justlab.ai",
      "description": "Project subdomain",
      "variables": {
        "projectId": {
          "default": "your-project-id",
          "description": "Your JustLab project ID. Replace with your actual project id when calling the API."
        }
      }
    }
  ],
  "security": [ { "bearerAuth": [] } ],
  "tags": [
    { "name": "Text", "description": "Text generation and chat completion" },
    { "name": "Images", "description": "Image generation from text prompts" },
    { "name": "Vision", "description": "Image understanding and vision models" },
    { "name": "Audio & Speech", "description": "Text-To-Speech (TTS) and Speech-To-Text (STT)" },
    { "name": "Video", "description": "Video generation and processing (async)" },
    { "name": "Jobs", "description": "Long-running jobs and status polling" },
    { "name": "Batch", "description": "Batch processing APIs" },
    { "name": "Models", "description": "List available models" }
  ],
  "paths": {
    "/v1/models": {
      "get": {
        "tags": ["Models"],
        "summary": "List available models",
        "responses": {
          "200": {
            "description": "A list of models",
            "content": {
              "application/json": {
                "schema": {"type":"object","properties":{"data":{"type":"array","items":{"$ref":"#/components/schemas/Model"}}}},
                "examples": {
                  "default": {
                    "value": {"data": [
                      {"id":"gpt-4o-mini","type":"text"},
                      {"id":"flux-schnell","type":"image"},
                      {"id":"elevenlabs-alloy","type":"tts"},
                      {"id":"gen-vid-1","type":"video"}
                    ]}
                  }
                }
              }
            }
          }
        }
      }
    },

    "/v1/text/generate": {
      "post": {
        "tags": ["Text"],
        "summary": "Generate text",
        "description": "Synchronous by default. Set async=true to run as a job.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {"$ref":"#/components/schemas/TextRequest"},
              "examples": {
                "sync": {"value": {"model":"gpt-4o-mini","prompt":"Write a creative story about AI.","max_tokens":400,"temperature":0.7}},
                "async": {"value": {"model":"gpt-4o-mini","prompt":"Summarize the attached document.","async":true,"callback_url":"https://example.com/webhooks/justlab"}}
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Generated text (sync)",
            "content": {"application/json": {"schema": {"$ref":"#/components/schemas/TextResponse"}}}
          },
          "202": {
            "description": "Job accepted (async)",
            "content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobSubmission"}}}
          },
          "400": {"$ref":"#/components/responses/BadRequest"},
          "401": {"$ref":"#/components/responses/Unauthorized"}
        }
      }
    },

    "/v1/vision/analyze": {
      "post": {
        "tags": ["Vision"],
        "summary": "Analyze an image (caption, classify, or detect)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {"$ref":"#/components/schemas/VisionRequest"},
              "examples": {
                "caption": {"value": {"model":"gpt-4o-mini","mode":"caption","image_url":"https://images.example.com/cat.jpg"}},
                "classify": {"value": {"model":"gpt-4o-mini","mode":"classify","image_url":"https://images.example.com/leaf.jpg","labels":["maple","oak","birch"]}},
                "detect": {"value": {"model":"gpt-4o-mini","mode":"detect","image_url":"https://images.example.com/street.jpg"}}
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Vision result",
            "content": {"application/json": {"schema": {"$ref":"#/components/schemas/VisionResponse"}}}
          },
          "400": {"$ref":"#/components/responses/BadRequest"},
          "401": {"$ref":"#/components/responses/Unauthorized"}
        }
      }
    },

    "/v1/images/generate": {
      "post": {
        "tags": ["Images"],
        "summary": "Generate an image from a text prompt",
        "description": "Synchronous by default. Set async=true to run as a job for longer generation times.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {"$ref":"#/components/schemas/ImageRequest"},
              "examples": {
                "sync": {"value": {"model":"flux-schnell","prompt":"A beautiful sunset over a calm ocean","size":"1024x1024","quality":"standard"}},
                "async": {"value": {"model":"flux-dev","prompt":"A highly detailed cyberpunk cityscape at night","size":"1536x1024","quality":"hd","async":true,"callback_url":"https://example.com/webhooks/justlab"}},
                "multiple": {"value": {"model":"flux-schnell","prompt":"A cute cat wearing sunglasses","n":4,"size":"512x512"}}
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Generated image(s) (sync)",
            "content": {"application/json": {"schema": {"$ref":"#/components/schemas/ImageResponse"}}}
          },
          "202": {
            "description": "Job accepted (async)",
            "content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobSubmission"}}}
          },
          "400": {"$ref":"#/components/responses/BadRequest"},
          "401": {"$ref":"#/components/responses/Unauthorized"}
        }
      }
    },

    "/v1/audio/speech": {
      "post": {
        "tags": ["Audio & Speech"],
        "summary": "Text-to-Speech (TTS)",
        "requestBody": {
          "required": true,
          "content": {"application/json": {"schema": {"$ref":"#/components/schemas/AudioSpeechRequest"}}}
        },
        "responses": {
          "200": {"description":"Generated audio (sync)","content": {"application/json": {"schema": {"$ref":"#/components/schemas/AudioSpeechResponse"}}}},
          "202": {"description":"Job accepted (async)","content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobSubmission"}}}}
        }
      }
    },

    "/v1/audio/transcriptions": {
      "post": {
        "tags": ["Audio & Speech"],
        "summary": "Speech-to-Text (STT)",
        "requestBody": {
          "required": true,
          "content": {"application/json": {"schema": {"$ref":"#/components/schemas/AudioTranscribeRequest"}}}
        },
        "responses": {
          "200": {"description":"Transcription (sync)","content": {"application/json": {"schema": {"$ref":"#/components/schemas/AudioTranscribeResponse"}}}},
          "202": {"description":"Job accepted (async)","content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobSubmission"}}}}
        }
      }
    },

    "/v1/video/generate": {
      "post": {
        "tags": ["Video"],
        "summary": "Generate a video from text or images (async)",
        "description": "Always asynchronous: returns a job to poll or await via webhook.",
        "requestBody": {
          "required": true,
          "content": {"application/json": {"schema": {"$ref":"#/components/schemas/VideoRequest"}}}
        },
        "responses": {
          "202": {"description":"Job accepted","content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobSubmission"}}}},
          "400": {"$ref":"#/components/responses/BadRequest"},
          "401": {"$ref":"#/components/responses/Unauthorized"}
        }
      }
    },

    "/v1/jobs": {
      "post": {
        "tags": ["Jobs"],
        "summary": "Create a generic job",
        "description": "Submit a job for any supported task by specifying type and input.",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref":"#/components/schemas/GenericJobRequest"}}}},
        "responses": {"202": {"description":"Job accepted","content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobSubmission"}}}}}
      }
    },

    "/v1/jobs/{job_id}": {
      "get": {
        "tags": ["Jobs"],
        "summary": "Get job details and result",
        "parameters": [ {"name":"job_id","in":"path","required":true,"schema":{"type":"string"}} ],
        "responses": {"200": {"description":"Job state","content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobStatus"}}}}, "404": {"$ref":"#/components/responses/NotFound"}}
      }
    },

    "/v1/jobs/{job_id}/status": {
      "get": {
        "tags": ["Jobs"],
        "summary": "Get job status (lightweight)",
        "parameters": [ {"name":"job_id","in":"path","required":true,"schema":{"type":"string"}} ],
        "responses": {"200": {"description":"Status only","content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobStatusLite"}}}}}
      }
    },

    "/v1/batch": {
      "post": {
        "tags": ["Batch"],
        "summary": "Submit a batch of jobs",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref":"#/components/schemas/BatchRequest"}}}},
        "responses": {"202": {"description":"Batch accepted","content": {"application/json": {"schema": {"$ref":"#/components/schemas/BatchSubmission"}}}}}
      }
    },

    "/v1/batch/{batch_id}": {
      "get": {
        "tags": ["Batch"],
        "summary": "Get batch status",
        "parameters": [ {"name":"batch_id","in":"path","required":true,"schema":{"type":"string"}} ],
        "responses": {"200": {"description":"Batch details","content": {"application/json": {"schema": {"$ref":"#/components/schemas/BatchStatus"}}}}, "404": {"$ref":"#/components/responses/NotFound"}}
      }
    }
  },

  "webhooks": {
    "job.completed": {
      "post": {
        "summary": "Webhook when a job succeeds",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobStatus"}}}},
        "responses": {"200": {"description": "Your server should return 200 OK"}},
        "security": []
      }
    },
    "job.failed": {
      "post": {
        "summary": "Webhook when a job fails",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref":"#/components/schemas/JobStatus"}}}},
        "responses": {"200": {"description": "Your server should return 200 OK"}},
        "security": []
      }
    }
  },

  "components": {
    "securitySchemes": {
      "bearerAuth": {"type":"http","scheme":"bearer"}
    },
    "responses": {
      "BadRequest": {"description":"Bad request","content": {"application/json": {"schema": {"$ref":"#/components/schemas/Error"}}}},
      "Unauthorized": {"description":"Unauthorized","content": {"application/json": {"schema": {"$ref":"#/components/schemas/Error"}}}},
      "NotFound": {"description":"Not found","content": {"application/json": {"schema": {"$ref":"#/components/schemas/Error"}}}}
    },
    "schemas": {
      "Model": {"type":"object","properties": {"id":{"type":"string"},"type":{"type":"string"}}},

      "TextRequest": {
        "type": "object",
        "required": ["model","prompt"],
        "properties": {
          "model": {"type":"string","description":"Model id"},
          "prompt": {"type":"string"},
          "max_tokens": {"type":"integer","minimum":1,"maximum":4096},
          "temperature": {"type":"number","minimum":0,"maximum":2,"default":1},
          "async": {"type":"boolean","default": false},
          "callback_url": {"type":"string","format":"uri","description":"HTTP webhook to receive job updates"},
          "metadata": {"type":"object","additionalProperties": true}
        }
      },
      "TextResponse": {"type":"object","properties": {"id":{"type":"string"},"object":{"type":"string","example":"text_generation"},"model":{"type":"string"},"created":{"type":"integer"},"text":{"type":"string"},"usage":{"$ref":"#/components/schemas/Usage"}}},

      "VisionRequest": {
        "type":"object",
        "required":["model","image_url"],
        "properties": {
          "model": {"type":"string"},
          "image_url": {"type":"string","format":"uri"},
          "mode": {"type":"string","enum":["caption","classify","detect"],"default":"caption"},
          "labels": {"type":"array","items":{"type":"string"}},
          "prompt": {"type":"string","description":"Optional guiding prompt"}
        }
      },
      "VisionResponse": {
        "type":"object",
        "oneOf": [
          {"properties": {"mode":{"const":"caption"},"caption":{"type":"string"}}},
          {"properties": {"mode":{"const":"classify"},"label":{"type":"string"},"scores":{"type":"object","additionalProperties":{"type":"number"}}}},
          {"properties": {"mode":{"const":"detect"},"objects":{"type":"array","items":{"type":"object","properties":{"label":{"type":"string"},"bbox":{"type":"array","items":{"type":"number"},"minItems":4,"maxItems":4}}}}}}
        ],
        "properties": {"created":{"type":"integer"},"model":{"type":"string"}}
      },

      "ImageRequest": {
        "type": "object",
        "required": ["model", "prompt"],
        "properties": {
          "model": {"type": "string", "description": "Image generation model id"},
          "prompt": {"type": "string", "description": "Text description of the desired image", "maxLength": 4000},
          "n": {"type": "integer", "minimum": 1, "maximum": 10, "default": 1, "description": "Number of images to generate"},
          "size": {"type": "string", "enum": ["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536"], "default": "1024x1024", "description": "Image dimensions"},
          "quality": {"type": "string", "enum": ["standard", "hd"], "default": "standard", "description": "Image quality level"},
          "style": {"type": "string", "enum": ["vivid", "natural"], "default": "vivid", "description": "Image style"},
          "response_format": {"type": "string", "enum": ["url", "b64_json"], "default": "url", "description": "Response format"},
          "async": {"type": "boolean", "default": false, "description": "Run generation asynchronously"},
          "callback_url": {"type": "string", "format": "uri", "description": "HTTP webhook to receive job updates"},
          "seed": {"type": "integer", "description": "Seed for reproducible results"},
          "metadata": {"type": "object", "additionalProperties": true}
        }
      },
      "ImageResponse": {
        "type": "object",
        "properties": {
          "id": {"type": "string"},
          "object": {"type": "string", "example": "image_generation"},
          "model": {"type": "string"},
          "created": {"type": "integer"},
          "data": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "url": {"type": "string", "format": "uri", "description": "Image URL (expires in 60 minutes)"},
                "b64_json": {"type": "string", "description": "Base64 encoded image data"},
                "revised_prompt": {"type": "string", "description": "The revised prompt used for generation"}
              }
            }
          },
          "usage": {"$ref": "#/components/schemas/Usage"}
        }
      },

      "AudioSpeechRequest": {
        "type":"object",
        "required":["model","text"],
        "properties": {
          "model": {"type":"string"},
          "text": {"type":"string"},
          "voice": {"type":"string","default":"alloy"},
          "format": {"type":"string","enum":["mp3","wav","ogg"],"default":"mp3"},
          "async": {"type":"boolean","default": false},
          "callback_url": {"type":"string","format":"uri"}
        }
      },
      "AudioSpeechResponse": {"type":"object","properties": {"id":{"type":"string"},"url":{"type":"string","format":"uri"},"duration_seconds":{"type":"number"}}},

      "AudioTranscribeRequest": {
        "type":"object",
        "required":["model","audio_url"],
        "properties": {
          "model": {"type":"string"},
          "audio_url": {"type":"string","format":"uri"},
          "language": {"type":"string","example":"en"},
          "async": {"type":"boolean","default": false},
          "callback_url": {"type":"string","format":"uri"}
        }
      },
      "AudioTranscribeResponse": {"type":"object","properties": {"id":{"type":"string"},"text":{"type":"string"},"confidence":{"type":"number"}}},

      "VideoRequest": {
        "type":"object",
        "required":["model","prompt"],
        "properties": {
          "model": {"type":"string"},
          "prompt": {"type":"string"},
          "duration_seconds": {"type":"integer","minimum":1,"maximum":60,"default":6},
          "seed": {"type":"integer"},
          "callback_url": {"type":"string","format":"uri"},
          "metadata": {"type":"object"}
        }
      },

      "GenericJobRequest": {
        "type":"object",
        "required":["type","input"],
        "properties": {
          "type": {"type":"string","enum":["text","image","vision","tts","stt","video"]},
          "input": {"type":"object","description":"Input object matching the respective request schema"},
          "callback_url": {"type":"string","format":"uri"},
          "priority": {"type":"string","enum":["low","normal","high"],"default":"normal"}
        }
      },

      "BatchRequest": {
        "type":"object",
        "required":["items"],
        "properties": {
          "items": {"type":"array","minItems":1,"maxItems":100,"items": {"type":"object","required":["id","type","input"],"properties": {"id":{"type":"string","description":"Client supplied id for tracking"},"type":{"type":"string","enum":["text","image","vision","tts","stt","video"]},"input":{"type":"object"}}}},
          "callback_url": {"type":"string","format":"uri","description":"Webhook for batch completion"},
          "priority": {"type":"string","enum":["low","normal","high"],"default":"normal"}
        }
      },

      "BatchSubmission": {"type":"object","properties": {"batch_id":{"type":"string"},"status_url":{"type":"string","format":"uri"},"job_ids":{"type":"array","items":{"type":"string"}}}},
      "BatchStatus": {"type":"object","properties": {"batch_id":{"type":"string"},"status":{"type":"string","enum":["queued","processing","succeeded","failed","partial"],"example":"processing"},"progress":{"type":"number","minimum":0,"maximum":100},"items":{"type":"array","items":{"type":"object","properties": {"id":{"type":"string"},"job_id":{"type":"string"},"status":{"type":"string","enum":["queued","processing","succeeded","failed"]},"result":{"type":"object"},"error":{"type":"string"}}}}}},

      "Usage": {"type":"object","properties": {"total_tokens":{"type":"integer"},"prompt_tokens":{"type":"integer"},"completion_tokens":{"type":"integer"},"cost":{"type":"number"}}},

      "JobSubmission": {"type":"object","properties": {"job_id":{"type":"string"},"status":{"type":"string","enum":["queued","processing"],"example":"queued"},"eta_seconds":{"type":"integer","example":30},"status_url":{"type":"string","format":"uri"},"created_at":{"type":"string","format":"date-time"}}},
      "JobStatusLite": {"type":"object","properties": {"job_id":{"type":"string"},"status":{"type":"string","enum":["queued","processing","succeeded","failed","canceled"]},"progress":{"type":"number","minimum":0,"maximum":100}}},
      "JobStatus": {"allOf": [ {"$ref":"#/components/schemas/JobStatusLite"}, {"type":"object","properties": {"result":{"type":"object","description":"Task-specific result; e.g., text, url, etc."},"error":{"type":"string"},"started_at":{"type":"string","format":"date-time"},"completed_at":{"type":"string","format":"date-time"}}} ]},

      "Error": {"type":"object","properties": {"error":{"type":"string"},"message":{"type":"string"},"code":{"type":"string"}}}
    }
  }
}
