> For the complete documentation index, see [llms.txt](https://docs.ionos.com/cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ionos.com/cloud/ai/ai-model-hub/how-tos/image-edits.md).

# Image Generation with IONOS CLOUD AI Model Hub

The <code class="expression">space.vars.ionos\_cloud\_ai\_model\_hub</code> provides an OpenAI-compatible API that enables image editing using state-of-the-art foundation models. By supplying an input image and a descriptive prompt, you can modify existing images directly through the API without the need for managing underlying hardware or infrastructure.

## Supported image editing models

You can edit images in AI Model Hub using select text-to-image models. For more information, see [<mark style="color:blue;">Models</mark>](/cloud/ai/ai-model-hub/models.md).

## Overview

In this guide, you will learn how to edit images using foundation models through the <code class="expression">space.vars.ionos\_cloud\_api</code>. This guide is intended for developers with basic knowledge of:

* REST APIs
* A programming language for handling REST API endpoints (Python and Bash examples are provided)

By the end, you will be able to:

1. Retrieve a list of available image editing models in the <code class="expression">space.vars.ionos\_cloud\_ai\_model\_hub</code>.
2. Use prompts and input images to edit images with FLUX.2-klein-4B.

## Getting started with image editing

To use image editing models, first set up your environment and authenticate using the OpenAI-compatible API endpoints.

#### Step 1: Retrieve available models

Check the model card for the feature `image editing`. Fetch the list of models from the API to retrieve the model name.

{% tabs %}
{% tab title="Python" %}

```python
# Python example to retrieve available models
import requests

IONOS_API_TOKEN = "[YOUR API TOKEN HERE]"

endpoint = "https://openai.inference.de-txl.ionos.com/v1/models"

header = {
    "Authorization": f"Bearer {IONOS_API_TOKEN}",
    "Content-Type": "application/json"
}
requests.get(endpoint, headers=header).json()
```

{% endtab %}

{% tab title="Bash" %}

```bash
#!/bin/bash

IONOS_API_TOKEN=[YOUR API TOKEN HERE]

curl -H "Authorization: Bearer ${IONOS_API_TOKEN}" \
        --get https://openai.inference.de-txl.ionos.com/v1/models
```

{% endtab %}
{% endtabs %}

This query returns a JSON document listing each model's name, which you'll use to specify a model for image editing in the next step.

#### Step 2: Edit an image using a prompt and FLUX.2-klein-4B

To edit an image, send a prompt and an image URL to the `/images/edits` endpoint. The request must be sent as `multipart/form-data`. Note the following parameter restrictions for FLUX.2-klein-4B:

* **`url`**: A public HTTP or HTTPS URL of the image to edit. Base64-encoded data URLs (`data:image/png;base64,...`) are also supported. Note that only one input image is permitted per request. The image must have a minimum dimension of 64 pixels on each side and an aspect ratio no more extreme than 8 to 1 (or 1 to 8).
* **`prompt`**: Text description of the desired edit. Maximum 1000 characters.
* **`size`**: A string specifying the width and height in `widthxheight` format. Both dimensions must be between 64 and 2048 (inclusive) and are automatically rounded down to the nearest multiple of 16. If omitted, the output defaults to the input image's original size and aspect ratio, scaled down to fit within these limits. Examples: `1024x1024`, `1536x1024`, `1024x1536`, `2048x2048`, `2048x1152`.
* **`n`**: An integer specifying the number of images to generate. Defaults to `1`. Accepts values between `1` and `10` (inclusive), provided the total output does not exceed 12,582,912 pixels per request (`n` × width × height). Examples: You can request up to three `2048x2048` images, or ten `1024x1024` images.
* **`output_format`**: File format of the generated image. Accepted values: `jpg`, `jpeg`, `png`, `webp`.
* **`mask_image`**: *Optional*. URL or base64 data URL of a mask image. Fully transparent areas (`alpha=0`) define where editing occurs. Must have the same dimensions as the input image.

Requests that do not meet these limits are rejected with a `400 Bad Request` error detailing the exceeded limit.

For the accepted formats and the request size limits that apply to the image you send, see [<mark style="color:blue;">Image Input</mark>](/cloud/ai/ai-model-hub/how-tos/image-input.md).

{% tabs %}
{% tab title="Python" %}

```python
# Python example for image editing with FLUX.2-klein-4B
import requests

IONOS_API_TOKEN = "[YOUR API TOKEN HERE]"
MODEL_NAME = "black-forest-labs/FLUX.2-klein-4B"
PROMPT = "Add a rainbow in the sky above the cityscape"
IMAGE_URL = "[IMAGE URL HERE]"

endpoint = "https://openai.inference.de-txl.ionos.com/v1/images/edits"

header = {
    "Authorization": f"Bearer {IONOS_API_TOKEN}"
}
files = {
    "model": (None, MODEL_NAME),
    "prompt": (None, PROMPT),
    "url": (None, IMAGE_URL),
    "size": (None, "1024x1024"),
}
requests.post(endpoint, files=files, headers=header)
```

{% endtab %}

{% tab title="Bash" %}

```bash
#!/bin/bash

IONOS_API_TOKEN=[YOUR API TOKEN HERE]
MODEL_NAME="black-forest-labs/FLUX.2-klein-4B"
PROMPT="Add a rainbow in the sky above the cityscape"
IMAGE_URL="[IMAGE URL HERE]"

curl -H "Authorization: Bearer ${IONOS_API_TOKEN}" \
     -F "model=$MODEL_NAME" \
     -F "prompt=$PROMPT" \
     -F "url=$IMAGE_URL" \
     -F "size=1024x1024" \
     https://openai.inference.de-txl.ionos.com/v1/images/edits
```

{% endtab %}
{% endtabs %}

#### Step 3: Extract and interpret the result

The returned JSON includes several key fields, most importantly:

* **`data.[].b64_json`**: The edited image in `base64` format. You can decode this `base64` string to obtain the edited image in the specified format (e.g., PNG, JPEG).

## What you learned

In this guide, you learned how to:

1. Access available image editing models.
2. Use descriptive prompts and input images to edit images with FLUX.2-klein-4B.

For information on generating images from scratch, see [<mark style="color:blue;">Image Generation</mark>](/cloud/ai/ai-model-hub/how-tos/image-generation.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ionos.com/cloud/ai/ai-model-hub/how-tos/image-edits.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
