> For the complete documentation index, see [llms.txt](https://docs.console.zenlayer.com/api-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.console.zenlayer.com/api-reference/compute/aig/image-generation/google-imagen-image-generation.md).

# Google Imagen

## 1. Overview

A text-to-image model launched by Google.

**Model List:**

* `imagen-3.0-generate-001`
* `imagen-3.0-generate-002`
* `imagen-3.0-fast-generate-001`

> **Note:**
>
> 1. This series of models has limited support for Chinese. It is recommended to use English when providing natural language descriptions.

## 2. Request Description

* **Request Method**: `POST`
* **Request URL**:

  > `https://gateway.theturbo.ai/v1/images/generations`

***

## 3. Request Parameters

### 3.1 Header Parameters

| Parameter Name  | Type   | Required | Description                                                | Example                |
| --------------- | ------ | -------- | ---------------------------------------------------------- | ---------------------- |
| `Content-Type`  | string | Yes      | Set the request header type, must be `application/json`    | `application/json`     |
| `Accept`        | string | Yes      | Set the response type, recommended as `application/json`   | `application/json`     |
| `Authorization` | string | Yes      | API\_KEY for authentication, format `Bearer $YOUR_API_KEY` | `Bearer $YOUR_API_KEY` |

***

### 3.2 Body Parameters (application/json)

<table><thead><tr><th width="114.59375">Parameter Name</th><th width="108.4765625">Type</th><th width="78.66796875">Required</th><th width="241.89453125">Description</th><th>Example (Default)</th></tr></thead><tbody><tr><td><strong>model</strong></td><td>string</td><td>Yes</td><td>The model ID to use. See the available versions listed in <a href="#id-1.-overview">Overview</a>, such as <code>imagen-3.0-generate-001</code>.</td><td><code>imagen-3.0-generate-001</code></td></tr><tr><td><strong>prompt</strong></td><td>string</td><td>Yes</td><td>A text description of the desired image. <code>imagen-3.0</code> has a maximum description length of 480 tokens. <code>imagegeneration@005</code> <code>imagegeneration@006</code> have a maximum description length of 128 tokens. <code>imagegeneration@002</code> has a maximum description length of 128 tokens.</td><td><code>A cute baby sea otter</code></td></tr><tr><td>n</td><td>number</td><td>No</td><td>The number of images to generate, must be between 1 and 4.</td><td><code>1</code></td></tr><tr><td>aspect_ratio</td><td>string</td><td>No</td><td>The aspect ratio of the image. Supports <code>1:1</code> <code>9:16</code> <code>16:9</code> <code>3:4</code> <code>4:3</code>. Supported ratios may vary slightly between models.</td><td><code>1:1</code></td></tr><tr><td>output_format</td><td>string</td><td>No</td><td>The format of the generated image. Supports <code>png</code> <code>jpeg</code>.</td><td><code>png</code></td></tr><tr><td>style</td><td>string</td><td>No</td><td>The style of the generated image. Supports <code>photograph</code> <code>digital_art</code> <code>landscape</code> <code>sketch</code> <code>watercolor</code> <code>cyberpunk</code> <code>pop_art</code>. This parameter is only applicable to <code>imagegeneration@002</code>.</td><td><code>photograph</code></td></tr><tr><td>response_format</td><td>string</td><td>No</td><td>The format in which the generated images are returned. Only supports <code>b64_json</code>.</td><td><code>b64_json</code></td></tr></tbody></table>

***

## 4. Request Examples

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

```http
POST /v1/images/generations
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "imagen-3.0-generate-001",
	"prompt": "A cute baby sea otter",
	"n": 1,
	"aspect_ratio": "1:1"
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
curl https://gateway.theturbo.ai/v1/images/generations \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
	\"model\": \"imagen-3.0-generate-001\",
	\"prompt\": \"A cute baby sea otter\",
	\"n\": 1,
	\"aspect_ratio\": \"1:1\"
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
)

const (
	YOUR_API_KEY    = "sk-123456789012345678901234567890123456789012345678"
	REQUEST_PAYLOAD = `{
	"model": "imagen-3.0-generate-001",
	"prompt": "A cute baby sea otter",
	"n": 1,
	"aspect_ratio": "1:1"
}`
	EXAMPLE_FILE_PATH = "example.png"
)

type Data struct {
	B64Json string `json:"b64_json"`
}

type ResponseJSON struct {
	Data []Data `json:"data"`
}

func main() {

	requestURL := "https://gateway.theturbo.ai/v1/images/generations"
	requestMethod := "POST"
	requestPayload := strings.NewReader(REQUEST_PAYLOAD)

	req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
	if err != nil {
		fmt.Println("Create request failed, err: ", err)
		return
	}

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Accept", "application/json")
	req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)

	client := &http.Client{}

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Do request failed, err: ", err)
		return
	}
	defer resp.Body.Close()

	respBodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Read response body failed, err: ", err)
		return
	}

	if resp.StatusCode != 200 {
		fmt.Printf("status code: %d\n", resp.StatusCode)
		fmt.Println(string(respBodyBytes))
		return
	}

	var responseJSON ResponseJSON
	if err := json.Unmarshal(respBodyBytes, &responseJSON); err != nil {
		fmt.Println("Parse response body failed, err: ", err)
		return
	}

	if len(responseJSON.Data) == 0 || responseJSON.Data[0].B64Json == "" {
		fmt.Println("Parse response body failed, err: empty b64_json")
		return
	}

	imageData, err := base64.StdEncoding.DecodeString(responseJSON.Data[0].B64Json)
	if err != nil {
		fmt.Println("Parse b64_json failed, err: ", err)
		return
	}

	imageFile, err := os.Create(EXAMPLE_FILE_PATH)
	if err != nil {
		fmt.Printf("Create file %s failed, err: %v\n", EXAMPLE_FILE_PATH, err)
		return
	}

	if _, err := imageFile.Write(imageData); err != nil {
		fmt.Printf("Write to file %s failed, err: %v\n", EXAMPLE_FILE_PATH, err)
		imageFile.Close()
		return
	}

	imageFile.Close()

	fmt.Println("success to write to file ", EXAMPLE_FILE_PATH)
}
```

{% endtab %}
{% endtabs %}

## 5. Response Example

```json
{
	"created": 1589478378,
	"data": [
		{
			"b64_json": "..."
		},
		{
			"b64_json": "..."
		}
	]
}
```


---

# 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.console.zenlayer.com/api-reference/compute/aig/image-generation/google-imagen-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.
