> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate images with Grok

> Generate Grok images with JSON requests, aspect ratio controls, resolution controls, and URL or Base64 output.

Use this route to create images from text with Grok image models. The request body uses `application/json`.
The request supports `model`, `prompt`, `n`, `aspect_ratio`, `resolution`, and `response_format`.

## Choose a model

Choose a Grok image model ID from the [Models page](/overview/models). The API examples use `grok-imagine-image-quality`.

## Configure the output

* Use `aspect_ratio` to select the output shape, or use `auto` to let the model select the shape.
* Use `resolution` to request `1k` or `2k` output.
* Use `n` to request up to 10 images.
* Use `response_format` to return temporary URLs or Base64-encoded image data.

For provider parameter details, see the [xAI image generation guide](https://docs.x.ai/developers/model-capabilities/images/generation).

<Note>
  Download URL results after the request completes. Generated image URLs can expire.
</Note>


## OpenAPI

````yaml api/openapi/image/grok/post-image-generation.openapi.json POST /v1/images/generations
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
                  example: grok-imagine-image-quality
                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
                  description: The requested output resolution.
                  example: 1k
                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-quality
                  prompt: A glass observatory above a quiet alpine lake at sunrise
                  aspect_ratio: '16:9'
                  resolution: 1k
                  response_format: url
              base64-2k:
                summary: Base64 and 2K
                value:
                  model: grok-imagine-image-quality
                  prompt: >-
                    A detailed botanical illustration of a blue orchid on ivory
                    paper
                  aspect_ratio: '3:4'
                  resolution: 2k
                  response_format: b64_json
              two-images:
                summary: Two images
                value:
                  model: grok-imagine-image-quality
                  prompt: >-
                    An editorial photograph of a red bicycle beside a modern
                    library
                  'n': 2
                  aspect_ratio: '4:3'
                  resolution: 1k
                  response_format: url
      responses:
        '200':
          description: The generated image results.
          content:
            application/json:
              schema:
                type: object
                additionalProperties: false
                required:
                  - data
                  - usage
                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
                    required:
                      - cost_in_usd_ticks
                    additionalProperties: false
                    description: Usage details for the request.
                    properties:
                      cost_in_usd_ticks:
                        type: integer
                        minimum: 0
                        description: The request cost in USD ticks.
              example:
                data:
                  - url: https://cdn.example.com/generated-image.jpg
                    mime_type: image/jpeg
                usage:
                  cost_in_usd_ticks: 500000000
      x-codeSamples:
        - lang: Shell
          label: Basic URL
          source: |-
            curl https://api.cometapi.com/v1/images/generations \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              --data-binary '{
                "model": "grok-imagine-image-quality",
                "prompt": "A glass observatory above a quiet alpine lake at sunrise",
                "aspect_ratio": "16:9",
                "resolution": "1k",
                "response_format": "url"
              }'
        - lang: Python
          label: Basic URL
          source: |-
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/v1/images/generations",
                headers={
                    "Authorization": f"Bearer {os.environ['COMETAPI_KEY']}",
                    "Content-Type": "application/json",
                },
                json={
                    "model": "grok-imagine-image-quality",
                    "prompt": "A glass observatory above a quiet alpine lake at sunrise",
                    "aspect_ratio": "16:9",
                    "resolution": "1k",
                    "response_format": "url",
                },
                timeout=180,
            )
            response.raise_for_status()
            print(response.json())
        - lang: JavaScript
          label: Basic URL
          source: >-
            const response = await fetch(
              "https://api.cometapi.com/v1/images/generations",
              {
                method: "POST",
                headers: {
                  Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                  "Content-Type": "application/json",
                },
                body: JSON.stringify({
                  model: "grok-imagine-image-quality",
                  prompt: "A glass observatory above a quiet alpine lake at sunrise",
                  aspect_ratio: "16:9",
                  resolution: "1k",
                  response_format: "url",
                }),
              },
            );

            if (!response.ok) throw new Error(`Request failed:
            ${response.status}`);

            console.log(await response.json());
        - lang: Shell
          label: Base64 and 2K
          source: |-
            curl https://api.cometapi.com/v1/images/generations \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              --data-binary '{
                "model": "grok-imagine-image-quality",
                "prompt": "A detailed botanical illustration of a blue orchid on ivory paper",
                "aspect_ratio": "3:4",
                "resolution": "2k",
                "response_format": "b64_json"
              }'
        - lang: Python
          label: Base64 and 2K
          source: |-
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/v1/images/generations",
                headers={
                    "Authorization": f"Bearer {os.environ['COMETAPI_KEY']}",
                    "Content-Type": "application/json",
                },
                json={
                    "model": "grok-imagine-image-quality",
                    "prompt": "A detailed botanical illustration of a blue orchid on ivory paper",
                    "aspect_ratio": "3:4",
                    "resolution": "2k",
                    "response_format": "b64_json",
                },
                timeout=180,
            )
            response.raise_for_status()
            print(response.json())
        - lang: JavaScript
          label: Base64 and 2K
          source: >-
            const response = await fetch(
              "https://api.cometapi.com/v1/images/generations",
              {
                method: "POST",
                headers: {
                  Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                  "Content-Type": "application/json",
                },
                body: JSON.stringify({
                  model: "grok-imagine-image-quality",
                  prompt: "A detailed botanical illustration of a blue orchid on ivory paper",
                  aspect_ratio: "3:4",
                  resolution: "2k",
                  response_format: "b64_json",
                }),
              },
            );

            if (!response.ok) throw new Error(`Request failed:
            ${response.status}`);

            console.log(await response.json());
        - lang: Shell
          label: Two images
          source: |-
            curl https://api.cometapi.com/v1/images/generations \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              --data-binary '{
                "model": "grok-imagine-image-quality",
                "prompt": "An editorial photograph of a red bicycle beside a modern library",
                "n": 2,
                "aspect_ratio": "4:3",
                "resolution": "1k",
                "response_format": "url"
              }'
        - lang: Python
          label: Two images
          source: |-
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/v1/images/generations",
                headers={
                    "Authorization": f"Bearer {os.environ['COMETAPI_KEY']}",
                    "Content-Type": "application/json",
                },
                json={
                    "model": "grok-imagine-image-quality",
                    "prompt": "An editorial photograph of a red bicycle beside a modern library",
                    "n": 2,
                    "aspect_ratio": "4:3",
                    "resolution": "1k",
                    "response_format": "url",
                },
                timeout=180,
            )
            response.raise_for_status()
            print(response.json())
        - lang: JavaScript
          label: Two images
          source: >-
            const response = await fetch(
              "https://api.cometapi.com/v1/images/generations",
              {
                method: "POST",
                headers: {
                  Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                  "Content-Type": "application/json",
                },
                body: JSON.stringify({
                  model: "grok-imagine-image-quality",
                  prompt: "An editorial photograph of a red bicycle beside a modern library",
                  n: 2,
                  aspect_ratio: "4:3",
                  resolution: "1k",
                  response_format: "url",
                }),
              },
            );

            if (!response.ok) throw new Error(`Request failed:
            ${response.status}`);

            console.log(await response.json());
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API key
      description: Use your CometAPI API key as the bearer value.

````