> 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 data URLs (`data:image/png;base64,...`) are also accepted.
* **`prompt`**: Text description of the desired edit. Maximum 1000 characters.
* **`size`**: Width and height in `"widthxheight"` format. Both dimensions must be multiples of 16 and between 64 and 2048. Default: `"1024x1024"`. Example values: `"1024x1024"`, `"1536x1024"`, `"1024x1536"`, `"2048x2048"`, `"2048x1152"`.
* **`n`**: Number of images to generate. Defaults to `1`. Accepts values from `1` to `10`.
* **`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.

{% 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
