> ## Documentation Index
> Fetch the complete documentation index at: https://modular-main.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Image generation

> Generate and transform images with the responses API

To use an image generation model, you need to use the [responses
API](/api/inference/create-response). The API accepts text and image inputs,
and returns generated images as base64-encoded data.

The examples below use the `FLUX.2-klein-4B` model, but you can replace it with
any [supported model](/models) that's listed as an "Image" type.

## Requirements

* An [API key](/administration/api-keys). The code below assumes you set it
  in an environment variable:

  <CodeGroup>
    ```bash macOS/Linux theme={null}
    export MODULAR_API_KEY="your_api_key"
    ```

    ```bash Windows theme={null}
    $env:MODULAR_API_KEY="your_api_key"
    ```
  </CodeGroup>

* The `openai` Python package. You can install it with this command:

  <CodeGroup>
    ```bash Python (pip) theme={null}
    pip install openai
    ```

    ```bash Python (uv) theme={null}
    uv add openai
    ```

    ```bash Python (pixi) theme={null}
    pixi add openai
    ```

    ```bash TypeScript (npm) theme={null}
    npm install openai
    ```
  </CodeGroup>

## Generate an image

To generate an image, set `input` to a text description of the image and
set generation parameters in `provider_options.image`.

<CodeGroup>
  ```python Python theme={null}
  import base64
  import os
  from pathlib import Path

  from openai import OpenAI

  client = OpenAI(
      base_url="https://api.modular.com/v1",
      api_key=os.environ.get("MODULAR_API_KEY"),
  )

  response = client.responses.create(
      model="black-forest-labs/FLUX.2-klein-4B",
      input="A serene mountain landscape at sunset",
      extra_body={
          "provider_options": {
              "image": {
                  "height": 512,
                  "width": 512,
                  "steps": 4,
              }
          }
      },
  )

  image_data = response.output[0].content[0].image_data
  Path("landscape.png").write_bytes(base64.b64decode(image_data))
  ```

  ```typescript TypeScript theme={null}
  import { writeFile } from "node:fs/promises";
  import process from "node:process";
  import OpenAI from "openai";

  const client = new OpenAI({
    baseURL: "https://api.modular.com/v1",
    apiKey: process.env.MODULAR_API_KEY,
  });

  async function main() {
    const response = await client.responses.create({
      model: "black-forest-labs/FLUX.2-klein-4B",
      input: "A serene mountain landscape at sunset",
      provider_options: {
        image: {
          height: 512,
          width: 512,
          steps: 4,
        },
      },
    });

    const image_data = response.output[0].content[0].image_data;
    await writeFile("landscape.png", Buffer.from(image_data, "base64"));
  }

  main();
  ```

  ```bash cURL theme={null}
  curl -X POST https://api.modular.com/v1/responses \
    -H "Authorization: Bearer $MODULAR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "black-forest-labs/FLUX.2-klein-4B",
      "input": "A serene mountain landscape at sunset",
      "provider_options": {
        "image": {
          "height": 512,
          "width": 512,
          "steps": 4
        }
      }
    }' \
    | jq -r '.output[0].content[0].image_data' \
    | base64 -d > landscape.png
  ```
</CodeGroup>

You should quickly see an image that looks like this:

<img src="https://mintcdn.com/modular-main/COWxIRzvXSUAW4Ov/inference/images/landscape.png?fit=max&auto=format&n=COWxIRzvXSUAW4Ov&q=85&s=1423c4f94d85687b1a2d332cc797e49b" alt="" width="512" height="512" data-path="inference/images/landscape.png" />

## Transform an image

To transform an image, set `input` to a user message containing:

* An `input_image` block with an image URL or base64-encoded data URI.
* An `input_text` block describing the transformation.

This example encodes the `landscape.png` file created above and transforms it
into a watercolor painting:

<CodeGroup>
  ```python Python theme={null}
  import base64
  import os
  from pathlib import Path

  from openai import OpenAI

  client = OpenAI(
      base_url="https://api.modular.com/v1",
      api_key=os.environ.get("MODULAR_API_KEY"),
  )

  input_data = base64.b64encode(Path("landscape.png").read_bytes()).decode("utf-8")

  response = client.responses.create(
      model="black-forest-labs/FLUX.2-klein-4B",
      input=[
          {
              "role": "user",
              "content": [
                  {
                      "type": "input_image",
                      "image_url": f"data:image/png;base64,{input_data}",
                  },
                  {
                      "type": "input_text",
                      "text": "Transform this image into a watercolor painting.",
                  },
              ],
          }
      ],
      extra_body={
          "provider_options": {
              "image": {
                  "height": 512,
                  "width": 512,
                  "steps": 4,
              }
          }
      },
  )

  image_data = response.output[0].content[0].image_data
  Path("watercolor.png").write_bytes(base64.b64decode(image_data))
  ```

  ```typescript TypeScript theme={null}
  import { readFile, writeFile } from "node:fs/promises";
  import process from "node:process";
  import OpenAI from "openai";

  const client = new OpenAI({
    baseURL: "https://api.modular.com/v1",
    apiKey: process.env.MODULAR_API_KEY,
  });

  async function main() {
    const inputImage = await readFile("landscape.png");
    const image_data = inputImage.toString("base64");

    const response = await client.responses.create({
      model: "black-forest-labs/FLUX.2-klein-4B",
      input: [
        {
          role: "user",
          content: [
            {
              type: "input_image",
              image_url: `data:image/png;base64,${image_data}`,
            },
            {
              type: "input_text",
              text: "Transform this image into a watercolor painting.",
            },
          ],
        },
      ],
      provider_options: {
        image: {
          height: 512,
          width: 512,
          steps: 4,
        },
      },
    });

    const output_image_data = response.output[0].content[0].image_data;
    await writeFile("watercolor.png", Buffer.from(output_image_data, "base64"));
  }

  main();
  ```

  ```bash cURL theme={null}
  IMAGE_DATA=$(base64 < landscape.png | tr -d '\n')

  curl -X POST https://api.modular.com/v1/responses \
    -H "Authorization: Bearer $MODULAR_API_KEY" \
    -H "Content-Type: application/json" \
    --data-binary @- <<EOF | jq -r '.output[0].content[0].image_data' | base64 -d > watercolor.png
  {
    "model": "black-forest-labs/FLUX.2-klein-4B",
    "input": [
      {
        "role": "user",
        "content": [
          {
            "type": "input_image",
            "image_url": "data:image/png;base64,$IMAGE_DATA"
          },
          {
            "type": "input_text",
            "text": "Transform this image into a watercolor painting."
          }
        ]
      }
    ],
    "provider_options": {
      "image": {
        "height": 512,
        "width": 512,
        "steps": 4
      }
    }
  }
  EOF
  ```
</CodeGroup>

You should see the image transformed like this:

<img src="https://mintcdn.com/modular-main/COWxIRzvXSUAW4Ov/inference/images/watercolor.png?fit=max&auto=format&n=COWxIRzvXSUAW4Ov&q=85&s=f13cd471997c973004659d0e6c712922" alt="" width="512" height="512" data-path="inference/images/watercolor.png" />
