#  Image Generations

## OpenAPI Specification

```yaml
openapi: 3.0.1
info:
  title: ''
  description: ''
  version: 1.0.0
paths:
  /v1/images/generations:
    post:
      summary: ' Image Generations'
      deprecated: false
      description: >-
        Some of the models can provide image generation capabilities. You can
        provide some descriptions of the image you would like to generate, and
        let the model generate one or multiple pictures in the output.


        If you're used to interacting with the chat/image-understanding models,
        the image generation is a bit different from them. You only need to send
        a prompt text in the request, instead of a list of messages with
        system/user/assistant roles. When you sent the prompt for image
        generation, your prompt will be revised by a chat model, and then sent
        to the image generation model.


        ##
        [Parameters](https://docs.x.ai/docs/guides/image-generations#parameters)


        - `n`: Number of image(s) to generate (1-10, default to 1)

        - `response_format`: `"url"` or `"b64_json"`. If `"url"` is specified,
        the response will return a url to the image(s) in `data[index].url`; if
        "b64_json" is specified, the response will return the image(s) in base64
        encoded format in `data[index].b64_json`.


        > Note: `quality`, `size` or `style` are not supported by xAI API at the
        moment.


        Request BodyExpand AllpromptstringrequiredPrompt for image
        generation.Show optional fields


        ------


        ## [Generate
        image](https://docs.x.ai/docs/guides/image-generations#generate-image)


        The image generation is offered at a different endpoint
        `https://api.x.ai/v1/images/generations` from the chat and
        image-understanding models that share
        `https://api.x.ai/v1/chat/completions`. The endpoint is **compatible
        with OpenAI SDK** (but **not with Anthropic SDK**), so you can keep
        using the same `base_url` of `https://api.x.ai/v1`.


        You can set `"model": "grok-2-image"` in the request body to use the
        model. The generated image will be in `jpg` format.


        bash`curl -X 'POST' https://api.x.ai/v1/images/generations \ -H 'accept:
        application/json' \ -H 'Authorization: Bearer <API_KEY>' \ -H
        'Content-Type: application/json' \ -d '{ -d '{  "model":
        "grok-2-image",  "prompt": "A cat in a tree" }'`


        The Python and JavaScript examples will print out url of the image on
        xAI managed storage.


        This is an example image generated from the above prompt:



        ![1a9d1139d479c4e156cb73d26bd08e56_example-cat-in-tree.png](https://api.apidog.com/api/v1/projects/875787/resources/353385/image-preview)


        ### [Base 64 JSON
        Output](https://docs.x.ai/docs/guides/image-generations#base-64-json-output)


        Instead of getting an image url by default, you can choose to get a
        base64 encoded image instead. To do so, you need to specify the
        `response_format` parameter to `"b64_json"`.


        ```bash

        curl -X 'POST' https://api.x.ai/v1/images/generations \

        -H 'accept: application/json' \

        -H 'Authorization: Bearer <API_KEY>' \

        -H 'Content-Type: application/json' \

        -d '{

        -d '{
          "model": "grok-2-image",
          "prompt": "A cat in a tree"
        }'

        ```


        You will get a `b64_json` field instead of `url` in the response image
        object.


        ### [Generating multiple
        images](https://docs.x.ai/docs/guides/image-generations#generating-multiple-images)


        You can generate up to 10 images in one request by adding a parameter
        `n` in your request body. For example, to generate four images:


        ```bash

        curl -X 'POST' https://api.x.ai/v1/images/generations \

        -H 'accept: application/json' \

        -H 'Authorization: Bearer <API_KEY>' \

        -H 'Content-Type: application/json' \

        -d '{
          "model": "grok-2-image",
          "prompt": "A cat in a tree",
          "response_format": "b64_json"
        }'

        ```


        ------


        ## [Revised
        prompt](https://docs.x.ai/docs/guides/image-generations#revised-prompt)


        If you inspect the response object, you can see something similar to
        this:


        ```json

        {
            "data": [
                {
                    "b64_json": "data:image/png;base64,...",
                    "revised_prompt": "..."
                }
            ]
        }

        ```


        Before sending the prompt to the image generation model, the prompt will
        be revised by a chat model. The revised prompt from chat model will be
        used by image generation model to create the image, and returned in
        `revised_prompt` to the user.


        To see the revised prompt with OpenAI SDK:


        ``` python

        # ... Steps to make image generation request


        print(response.data[0].revised_prompt)

        ```


        For example:


        | Input/Output                      |
        Example                                                      |

        | --------------------------------- |
        ------------------------------------------------------------ |

        | prompt (in request body)          | A cat in a
        tree                                              |

        | revised_prompt (in response body) | 3D render of a gray cat with green
        eyes perched on a thick branch of a leafy tree, set in a suburban
        backyard during the day. The cat's fur is slightly ruffled by a gentle
        breeze, and it is looking directly at the viewer. The background
        features a sunny sky with a few clouds and other trees, creating a
        natural and serene environment. The scene is focused on the cat, with no
        distracting foreground elements, ensuring the cat remains the central
        subject of the image. |
      tags:
        - Guides
      parameters:
        - name: Authorization
          in: header
          description: ''
          required: true
          example: Bearer {{XAI_API_KEY}}
          schema:
            type: string
        - name: Content-Type
          in: header
          description: ''
          required: true
          example: application/json
          schema:
            type: string
      requestBody:
        content:
          text/plain:
            schema:
              type: string
            example: |-
              {
              -d {
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties: {}
          headers: {}
          x-apidog-name: Success
      security: []
      x-apidog-folder: Guides
      x-apidog-status: released
      x-run-in-apidog: https://app.apidog.com/web/project/875787/apis/api-15799848-run
components:
  schemas: {}
  securitySchemes: {}
servers:
  - url: https://api.x.ai/v1/
    description: Prod Env
security: []

```
