{
  "openapi": "3.1.0",
  "info": {
    "title": "Grok Image Generation API",
    "version": "1.0.0",
    "description": "Generate images with Grok image models through CometAPI."
  },
  "servers": [
    {
      "url": "https://api.cometapi.com"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/v1/images/generations": {
      "post": {
        "summary": "Generate images with Grok",
        "operationId": "grok_image_generation",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "model",
                  "prompt"
                ],
                "properties": {
                  "model": {
                    "type": "string",
                    "description": "The Grok image model ID. See the [Models page](/overview/models) for available model IDs.",
                    "enum": [
                      "grok-imagine-image",
                      "grok-imagine-image-quality",
                      "grok-imagine-image-2.0"
                    ],
                    "example": "grok-imagine-image-2.0"
                  },
                  "prompt": {
                    "type": "string",
                    "minLength": 1,
                    "description": "A text description of the image to generate.",
                    "example": "A glass observatory above a quiet alpine lake at sunrise"
                  },
                  "n": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 10,
                    "default": 1,
                    "description": "The number of images to generate. Use an integer from 1 through 10."
                  },
                  "aspect_ratio": {
                    "type": "string",
                    "enum": [
                      "1:1",
                      "3:4",
                      "4:3",
                      "9:16",
                      "16:9",
                      "2:3",
                      "3:2",
                      "9:19.5",
                      "19.5:9",
                      "9:20",
                      "20:9",
                      "1:2",
                      "2:1",
                      "auto"
                    ],
                    "description": "The output shape. Use `auto` to let the model select the aspect ratio.",
                    "example": "16:9"
                  },
                  "resolution": {
                    "type": "string",
                    "enum": [
                      "1k",
                      "2k"
                    ],
                    "default": "1k",
                    "description": "The requested output resolution. Image 2.0 uses `1k` when this field is omitted.",
                    "example": "1k"
                  },
                  "quality": {
                    "type": "string",
                    "enum": [
                      "low",
                      "medium"
                    ],
                    "description": "Image quality for `grok-imagine-image-2.0`. This field is not available for the older Grok image model IDs.",
                    "example": "low"
                  },
                  "response_format": {
                    "type": "string",
                    "enum": [
                      "url",
                      "b64_json"
                    ],
                    "default": "url",
                    "description": "The image representation in each response item. Use `url` for a temporary download URL or `b64_json` for Base64-encoded image bytes."
                  }
                }
              },
              "examples": {
                "basic-url": {
                  "summary": "Basic URL",
                  "value": {
                    "model": "grok-imagine-image-2.0",
                    "prompt": "A glass observatory above a quiet alpine lake at sunrise",
                    "aspect_ratio": "16:9",
                    "resolution": "1k",
                    "quality": "low",
                    "response_format": "url"
                  }
                },
                "base64-2k": {
                  "summary": "Base64 and 2K",
                  "value": {
                    "model": "grok-imagine-image-2.0",
                    "prompt": "A detailed botanical illustration of a blue orchid on ivory paper",
                    "aspect_ratio": "3:4",
                    "resolution": "2k",
                    "quality": "medium",
                    "response_format": "b64_json"
                  }
                },
                "two-images": {
                  "summary": "Two images",
                  "value": {
                    "model": "grok-imagine-image-2.0",
                    "prompt": "An editorial photograph of a red bicycle beside a modern library",
                    "n": 2,
                    "aspect_ratio": "4:3",
                    "resolution": "1k",
                    "quality": "low",
                    "response_format": "url"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The generated image results.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "minItems": 1,
                      "description": "The generated images.",
                      "items": {
                        "type": "object",
                        "additionalProperties": false,
                        "required": [
                          "mime_type"
                        ],
                        "oneOf": [
                          {
                            "required": [
                              "url"
                            ]
                          },
                          {
                            "required": [
                              "b64_json"
                            ]
                          }
                        ],
                        "properties": {
                          "url": {
                            "type": "string",
                            "format": "uri",
                            "description": "A temporary image URL when `response_format` is `url`."
                          },
                          "b64_json": {
                            "type": "string",
                            "description": "Base64-encoded image bytes when `response_format` is `b64_json`."
                          },
                          "mime_type": {
                            "type": "string",
                            "description": "The media type of the generated image.",
                            "example": "image/jpeg"
                          }
                        }
                      }
                    },
                    "usage": {
                      "type": "object",
                      "description": "Additional usage metadata returned for the request. Its contents can vary by model.",
                      "additionalProperties": true
                    }
                  }
                },
                "example": {
                  "data": [
                    {
                      "url": "https://cdn.example.com/generated-image.jpg",
                      "mime_type": "image/jpeg"
                    }
                  ]
                }
              }
            }
          }
        },
        "x-codeSamples": [
          {
            "lang": "Shell",
            "label": "Basic URL",
            "source": "curl https://api.cometapi.com/v1/images/generations \\\n  -H \"Authorization: Bearer $COMETAPI_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  --data-binary '{\n    \"model\": \"grok-imagine-image-2.0\",\n    \"prompt\": \"A glass observatory above a quiet alpine lake at sunrise\",\n    \"aspect_ratio\": \"16:9\",\n    \"resolution\": \"1k\",\n    \"quality\": \"low\",\n    \"response_format\": \"url\"\n  }'"
          },
          {
            "lang": "Python",
            "label": "Basic URL",
            "source": "import os\nimport requests\n\nresponse = requests.post(\n    \"https://api.cometapi.com/v1/images/generations\",\n    headers={\n        \"Authorization\": f\"Bearer {os.environ['COMETAPI_KEY']}\",\n        \"Content-Type\": \"application/json\",\n    },\n    json={\n        \"model\": \"grok-imagine-image-2.0\",\n        \"prompt\": \"A glass observatory above a quiet alpine lake at sunrise\",\n        \"aspect_ratio\": \"16:9\",\n        \"resolution\": \"1k\",\n        \"quality\": \"low\",\n        \"response_format\": \"url\",\n    },\n    timeout=180,\n)\nresponse.raise_for_status()\nprint(response.json())"
          },
          {
            "lang": "JavaScript",
            "label": "Basic URL",
            "source": "const response = await fetch(\n  \"https://api.cometapi.com/v1/images/generations\",\n  {\n    method: \"POST\",\n    headers: {\n      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,\n      \"Content-Type\": \"application/json\",\n    },\n    body: JSON.stringify({\n      model: \"grok-imagine-image-2.0\",\n      prompt: \"A glass observatory above a quiet alpine lake at sunrise\",\n      aspect_ratio: \"16:9\",\n      resolution: \"1k\",\n      quality: \"low\",\n      response_format: \"url\",\n    }),\n  },\n);\nif (!response.ok) throw new Error(`Request failed: ${response.status}`);\nconsole.log(await response.json());"
          },
          {
            "lang": "Shell",
            "label": "Base64 and 2K",
            "source": "curl https://api.cometapi.com/v1/images/generations \\\n  -H \"Authorization: Bearer $COMETAPI_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  --data-binary '{\n    \"model\": \"grok-imagine-image-2.0\",\n    \"prompt\": \"A detailed botanical illustration of a blue orchid on ivory paper\",\n    \"aspect_ratio\": \"3:4\",\n    \"resolution\": \"2k\",\n    \"quality\": \"medium\",\n    \"response_format\": \"b64_json\"\n  }'"
          },
          {
            "lang": "Python",
            "label": "Base64 and 2K",
            "source": "import base64\nimport os\nimport requests\n\nresponse = requests.post(\n    \"https://api.cometapi.com/v1/images/generations\",\n    headers={\n        \"Authorization\": f\"Bearer {os.environ['COMETAPI_KEY']}\",\n        \"Content-Type\": \"application/json\",\n    },\n    json={\n        \"model\": \"grok-imagine-image-2.0\",\n        \"prompt\": \"A detailed botanical illustration of a blue orchid on ivory paper\",\n        \"aspect_ratio\": \"3:4\",\n        \"resolution\": \"2k\",\n        \"quality\": \"medium\",\n        \"response_format\": \"b64_json\",\n    },\n    timeout=180,\n)\nresponse.raise_for_status()\nresult = response.json()\nimage = result[\"data\"][0]\nwith open(\"generated-image.png\", \"wb\") as image_file:\n    image_file.write(base64.b64decode(image[\"b64_json\"]))\nprint(image[\"mime_type\"])"
          },
          {
            "lang": "JavaScript",
            "label": "Base64 and 2K",
            "source": "import { writeFile } from \"node:fs/promises\";\n\nconst response = await fetch(\n  \"https://api.cometapi.com/v1/images/generations\",\n  {\n    method: \"POST\",\n    headers: {\n      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,\n      \"Content-Type\": \"application/json\",\n    },\n    body: JSON.stringify({\n      model: \"grok-imagine-image-2.0\",\n      prompt: \"A detailed botanical illustration of a blue orchid on ivory paper\",\n      aspect_ratio: \"3:4\",\n      resolution: \"2k\",\n      quality: \"medium\",\n      response_format: \"b64_json\",\n    }),\n  },\n);\nif (!response.ok) throw new Error(`Request failed: ${response.status}`);\nconst result = await response.json();\nconst image = result.data[0];\nawait writeFile(\"generated-image.png\", Buffer.from(image.b64_json, \"base64\"));\nconsole.log(image.mime_type);"
          },
          {
            "lang": "Shell",
            "label": "Two images",
            "source": "curl https://api.cometapi.com/v1/images/generations \\\n  -H \"Authorization: Bearer $COMETAPI_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  --data-binary '{\n    \"model\": \"grok-imagine-image-2.0\",\n    \"prompt\": \"An editorial photograph of a red bicycle beside a modern library\",\n    \"n\": 2,\n    \"aspect_ratio\": \"4:3\",\n    \"resolution\": \"1k\",\n    \"quality\": \"low\",\n    \"response_format\": \"url\"\n  }'"
          },
          {
            "lang": "Python",
            "label": "Two images",
            "source": "import os\nimport requests\n\nresponse = requests.post(\n    \"https://api.cometapi.com/v1/images/generations\",\n    headers={\n        \"Authorization\": f\"Bearer {os.environ['COMETAPI_KEY']}\",\n        \"Content-Type\": \"application/json\",\n    },\n    json={\n        \"model\": \"grok-imagine-image-2.0\",\n        \"prompt\": \"An editorial photograph of a red bicycle beside a modern library\",\n        \"n\": 2,\n        \"aspect_ratio\": \"4:3\",\n        \"resolution\": \"1k\",\n        \"quality\": \"low\",\n        \"response_format\": \"url\",\n    },\n    timeout=180,\n)\nresponse.raise_for_status()\nprint(response.json())"
          },
          {
            "lang": "JavaScript",
            "label": "Two images",
            "source": "const response = await fetch(\n  \"https://api.cometapi.com/v1/images/generations\",\n  {\n    method: \"POST\",\n    headers: {\n      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,\n      \"Content-Type\": \"application/json\",\n    },\n    body: JSON.stringify({\n      model: \"grok-imagine-image-2.0\",\n      prompt: \"An editorial photograph of a red bicycle beside a modern library\",\n      n: 2,\n      aspect_ratio: \"4:3\",\n      resolution: \"1k\",\n      quality: \"low\",\n      response_format: \"url\",\n    }),\n  },\n);\nif (!response.ok) throw new Error(`Request failed: ${response.status}`);\nconsole.log(await response.json());"
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API key",
        "description": "Use your CometAPI API key as the bearer value."
      }
    }
  }
}
