# Image Generation

The IONOS AI Model Hub 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>](https://docs.ionos.com/sections-test/guides/ai/ai-model-hub/models).

## Overview

In this guide, you will learn how to generate images using foundation models through the IONOS 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 IONOS AI Model Hub.
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 easily access image generation-specific scripts and examples and generate the intended output:

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

{% file src="<https://1737632334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MifAzdGvKLDTtvJP8sm%2Fuploads%2Fgit-blob-6a167af3ba58865aa7ec747232ef65009f4342a4%2Fai-model-hub-image-generation.ipynb?alt=media&token=d83c050c-2598-4f5f-99f1-4868c9f01bdc>" %}
{% endtab %}

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

{% file src="<https://1737632334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MifAzdGvKLDTtvJP8sm%2Fuploads%2Fgit-blob-fbca19a61b0554b259dfb7cfa9ee8bfbb3f1d888%2Fai-model-hub-image-generation.py?alt=media&token=5540e30d-36cf-4000-bccf-2e5fbc748407>" %}
{% endtab %}

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

{% file src="<https://1737632334-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MifAzdGvKLDTtvJP8sm%2Fuploads%2Fgit-blob-46dba52d65f3580d883f654680c5da4377242b7c%2Fai-model-hub-image-generation.sh?alt=media&token=09893863-4efa-4c8f-83ce-cb833127e8aa>" %}
{% 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 with Your Prompt

To generate an image, send a prompt to the `/images/generations` endpoint. Customize parameters like `size` for the resolution of the output image.

{% 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: Extract and Interpret the Result

The returned JSON includes several key fields, most importantly:

* **`data.[].b64_json`**: The generated image in base64 format.
* **`usage.prompt_tokens`**: Token count for the input prompt.
* **`usage.total_tokens`**: Token count for the entire process (usually zero for image generation, as billing is per image).

## Summary

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>](https://docs.ionos.com/sections-test/guides/ai/ai-model-hub/how-tos/text-generation) models.
