> 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-generation.md).

# Image Generation

The <code class="expression">space.vars.ionos\_cloud\_ai\_model\_hub</code> provides an OpenAI-compatible API that enables high-quality image generation using state-of-the-art foundation models. By inserting descriptive prompts, users can create detailed images directly through the API without the need for managing underlying hardware, or infrastructure.

## Supported image generation models

AI Model Hub offers varied text-to-image models. For more information, see [<mark style="color:blue;">models list</mark>](/cloud/ai/ai-model-hub/models.md).

## Overview

In this guide, you will learn how to generate images using foundation models through the <code class="expression">space.vars.ionos\_cloud</code> API. 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 generation models in the <code class="expression">space.vars.ionos\_cloud\_ai\_model\_hub</code>.
2. Use prompts to generate images with these models.

## Getting started with image generation

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

Download the respective code files to use image generation-specific scripts and examples and generate the intended output:

{% tabs %}
{% tab title="Python Notebook" %}
Download this Python Notebook file to use image generation-specific scripts and examples and generate the intended output.

{% file src="/files/se4T2eGB9kmH0VZSI7qb" %}
{% endtab %}

{% tab title="Python Code" %}
Download this Python code file to use image generation-specific scripts and examples and generate the intended output.

{% file src="/files/jGajipCRV5t7jfeXjUmx" %}
{% endtab %}

{% tab title="Bash Code" %}
Download this Bash code file to use image generation-specific scripts and examples and generate the intended output.

{% file src="/files/NwOtVF9qnOFCO0AmMG2I" %}
{% endtab %}
{% endtabs %}

#### Step 1: Retrieve Available Models

Fetch a list of models to see which are available for your use case:

{% 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 generation in later steps.

#### Step 2: Generate an image using a prompt and FLUX.1-schnell

To generate an image, send a prompt to the `/images/generations` endpoint. Note the following parameter restrictions for FLUX.1-schnell:

* **`size`**: Must be one of `"1024x1024"`, `"1792x1024"`, or `"1024x1792"`.
* **`n`**: Defaults to `1`. Only `1` is accepted.

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

```python
# Python example for image generation
import requests

IONOS_API_TOKEN = "[YOUR API TOKEN HERE]"
MODEL_NAME = "[MODEL NAME HERE]"
PROMPT = "A futuristic cityscape at sunset, highly detailed"

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

header = {
    "Authorization": f"Bearer {IONOS_API_TOKEN}", 
    "Content-Type": "application/json"
}
body = {
    "model": MODEL_NAME,
    "prompt": PROMPT,
    "size": "1024x1024"
}
requests.post(endpoint, json=body, headers=header)
```

{% endtab %}

{% tab title="Bash" %}

```bash
#!/bin/bash

IONOS_API_TOKEN=[YOUR API TOKEN HERE]
MODEL_NAME=[MODEL NAME HERE]
PROMPT="A futuristic cityscape at sunset, highly detailed"

BODY="{ 
    \"model\": \"$MODEL_NAME\",
    \"prompt\": \"$PROMPT\",
    \"size\": \"1024x1024\"
}"

curl -H "Authorization: Bearer ${IONOS_API_TOKEN}" \
     -H "Content-Type: application/json" \
     -d "$BODY" \
     https://openai.inference.de-txl.ionos.com/v1/images/generations
```

{% endtab %}
{% endtabs %}

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

To generate an image, send a prompt to the `/images/generations` endpoint. Note the following parameter restrictions for FLUX.2-klein-4B:

* **`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`**: Format of the returned image. Accepted values: `jpg`, `jpeg`, `png`, `webp`.

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

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

IONOS_API_TOKEN = "[YOUR API TOKEN HERE]"
MODEL_NAME = "black-forest-labs/FLUX.2-klein-4B"
PROMPT = "A futuristic cityscape at sunset, highly detailed"

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

header = {
    "Authorization": f"Bearer {IONOS_API_TOKEN}", 
    "Content-Type": "application/json"
}
body = {
    "model": MODEL_NAME,
    "prompt": PROMPT,
    "size": "1024x1024",
    "output_format": "png"
}
requests.post(endpoint, json=body, 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="A futuristic cityscape at sunset, highly detailed"

BODY="{ 
    \"model\": \"$MODEL_NAME\",
    \"prompt\": \"$PROMPT\",
    \"size\": \"1024x1024\",
    \"output_format\": \"png\"
}"

curl -H "Authorization: Bearer ${IONOS_API_TOKEN}" \
     -H "Content-Type: application/json" \
     -d "$BODY" \
     https://openai.inference.de-txl.ionos.com/v1/images/generations
```

{% endtab %}
{% endtabs %}

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

The returned JSON includes several key fields, most importantly:

* **`data.[].b64_json`**: The generated image in base64 format.

## What you learned

In this guide, you learned how to:

1. Access available image generation models.
2. Use descriptive prompts to generate high-quality images, ideal for applications in design, creative work, and more.

For information on text generation, see our dedicated guide on [<mark style="color:blue;">Text Generation</mark>](/cloud/ai/ai-model-hub/how-tos/text-generation.md) models.


---

# 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
