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

# Image Generation

> Generate images from text prompts.

## Generate an image

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from meshapi import MeshAPI, ImageGenerationParams

    client = MeshAPI(base_url="https://api.meshapi.ai", token="rsk_...")

    resp = client.images.generate(
        ImageGenerationParams(
            model="openai/gpt-image-1",
            prompt="A small blue square on a white background.",
            n=1,
            size="1024x1024",
        )
    )

    print(resp.created)          # Unix timestamp
    print(len(resp.data))        # number of images
    # each item has either .b64_json or .url
    if resp.data[0].b64_json:
        image_bytes = base64.b64decode(resp.data[0].b64_json)
    elif resp.data[0].url:
        print(resp.data[0].url)
    ```
  </Tab>

  <Tab title="Node.js">
    ```javascript theme={null}
    const resp = await client.images.generate({
      model: "openai/gpt-image-1",
      prompt: "A small blue square on a white background.",
      n: 1,
      size: "1024x1024",
    });

    console.log(resp.created);    // Unix timestamp
    if (resp.data[0].b64_json) {
      const bytes = Buffer.from(resp.data[0].b64_json, "base64");
    } else if (resp.data[0].url) {
      console.log(resp.data[0].url);
    }
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    model := "openai/gpt-image-1"
    n := 1
    size := "1024x1024"

    resp, err := client.Images.Generate(ctx, meshapi.ImageGenerationParams{
        Model:  &model,
        Prompt: "A small blue square on a white background.",
        N:      &n,
        Size:   &size,
    })
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("created=%d images=%d\n", resp.Created, len(resp.Data))
    if resp.Data[0].B64JSON != nil {
        // base64 image data
    } else if resp.Data[0].URL != nil {
        fmt.Println(*resp.Data[0].URL)
    }
    ```
  </Tab>
</Tabs>

## Getting the image bytes

Where the image data lands depends on the model and `response_format`. With the
default (or `response_format="url"`), some models — including `openai/gpt-image-1`
— return the image **inline as a `data:` URI in `url`** rather than in `b64_json`.
Pass `response_format="b64_json"` to always get base64 in `b64_json`, or use the
helper below to get raw bytes regardless of shape:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    # handles b64_json OR a data: URI in url
    image_bytes = resp.data[0].image_bytes()
    with open("out.png", "wb") as f:
        f.write(image_bytes)
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    imageBytes, err := resp.Data[0].Bytes() // handles B64JSON or a data: URI in URL
    if err != nil {
        log.Fatal(err)
    }
    os.WriteFile("out.png", imageBytes, 0644)
    ```
  </Tab>
</Tabs>

Both raise/return an error if `url` is a remote `http(s)` link (fetch it yourself).

## Response fields

| Field                                     | Description                                                   |
| ----------------------------------------- | ------------------------------------------------------------- |
| `resp.created`                            | Unix timestamp of when the image was created                  |
| `resp.data`                               | Array of image objects                                        |
| `resp.data[i].b64_json`                   | Base64-encoded image (if returned as base64)                  |
| `resp.data[i].url`                        | Image URL — may be a hosted link **or** an inline `data:` URI |
| `resp.data[i].image_bytes()` / `.Bytes()` | Raw image bytes from either shape (Python / Go)               |

<Info>
  `openai/gpt-image-1` is the recommended image generation model. Additional image generation models may be available depending on your account.
</Info>
