> ## Documentation Index
> Fetch the complete documentation index at: https://docs.toapis.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Grok Image Generation

> Generate images using Grok models, supporting text-to-image and image-to-image

> [!NOTE]
>
> **Note for users in mainland China:** Please use `https://toapis.cn` as the API endpoint (Base URL). Replace `https://toapis.com` with `https://toapis.cn` in the examples in this document.

* Unified image generation API
* Use model parameter to select `grok-4-1-image` or `grok-4-2-image` model
* Supports text-to-image and image-to-image
* Async task management, query results by task ID

> [!WARNING]
>
> **Important Change**: For better performance and cost control, we no longer support passing base64 image data directly in `image_urls`. Please use the [Upload Image API](https://docs.toapis.com/docs/en/api-reference/uploads/images.md) first to upload images and get URLs, then call this endpoint.

## Authorizations

**Authorization** `string` *required*

All endpoints require Bearer Token authentication

Get your API Key from the [API Key Management Page](https://toapis.com/console/token)

Add to request header:

```
Authorization: Bearer YOUR_API_KEY
```

## Body

**model** `string` *required*

Image generation model name

Supported models:

* `grok-4-1-image` - Grok 4.1 image generation model
* `grok-4-2-image` - Grok 4.2 image generation model

Example: `"grok-4-1-image"`

**prompt** `string` *required*

Text description for image generation

Maximum 1000 characters

**size** `string` (default: 1:1)

Image aspect ratio

Supported formats:

* `1:1` - Square (default)
* `2:3` - Portrait
* `3:2` - Landscape
* `16:9` - Widescreen landscape
* `9:16` - Widescreen portrait

**n** `integer` (default: 1)

Number of images to generate

Supports 1, 2, 4. Charges will be pre-deducted based on the number

Default: 1

**⚠️ Note:** Must enter a plain number (e.g., `1`), do not use quotes or it will cause an error

**image\_urls** `string[]`

Reference image URL list for image-to-image generation

**⚠️ URL format only (base64 no longer supported)**

* Publicly accessible image URL (http\:// or https\://)
* You can use the [Upload Image API](https://docs.toapis.com/docs/en/api-reference/uploads/images.md) to upload local images and get URLs

**Limitations:**

* Maximum 5 images
* Single image up to 10MB
* Supported formats: jpeg, jpg, png, webp

## Response

**id** `string`

Unique task identifier for status queries

**object** `string`

Object type, always `generation.task`

**model** `string`

Model name used

**status** `string`

Task status

* `queued` - Queued for processing
* `in_progress` - Processing
* `completed` - Successfully completed
* `failed` - Failed

**progress** `integer`

Task progress percentage (0-100)

**created\_at** `integer`

Task creation timestamp (Unix timestamp)

**metadata** `object`

Task metadata

```bash cURL theme={null}
curl --request POST \
  --url https://toapis.com/v1/images/generations \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "grok-4-1-image",
    "prompt": "A cyberpunk futuristic city at night",
    "size": "16:9",
    "n": 1
  }'
```

```bash cURL (Image-to-Image) theme={null}
curl --request POST \
  --url https://toapis.com/v1/images/generations \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "grok-4-1-image",
    "prompt": "Transform this image into watercolor painting style",
    "size": "1:1",
    "n": 1,
    "image_urls": [
      "https://example.com/photo.jpg"
    ]
  }'
```

```python Python theme={null}
import requests

response = requests.post(
    "https://toapis.com/v1/images/generations",
    headers={
        "Authorization": "Bearer your-ToAPIs-key",
        "Content-Type": "application/json"
    },
    json={
        "model": "grok-4-1-image",
        "prompt": "A cyberpunk futuristic city at night",
        "size": "16:9",
        "n": 1
    }
)

task = response.json()
print(f"Task ID: {task['id']}")
print(f"Status: {task['status']}")
```

```javascript JavaScript theme={null}
const response = await fetch('https://toapis.com/v1/images/generations', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your-ToAPIs-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    model: 'grok-4-1-image',
    prompt: 'A cyberpunk futuristic city at night',
    size: '16:9',
    n: 1
  })
});

const task = await response.json();
console.log(`Task ID: ${task.id}`);
console.log(`Status: ${task.status}`);
```

```json 200 theme={null}
{
  "id": "img_abc123def456",
  "object": "generation.task",
  "model": "grok-4-1-image",
  "status": "queued",
  "progress": 0,
  "created_at": 1768380224,
  "metadata": {}
}
```

```json 400 theme={null}
{
  "error": {
    "code": "invalid_request",
    "message": "The 'prompt' field is required.",
    "param": "prompt",
    "type": "invalid_request_error"
  }
}
```

```json 401 theme={null}
{
  "error": {
    "code": "unauthorized",
    "message": "Invalid API key",
    "type": "authentication_error"
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.