Google Imagen¶
1. Overview¶
A Google text-to-image model family.
Description:
- Models in this family are not optimized for Chinese prompts. Use English natural-language prompts when possible.
2. Request¶
- Method:
POST -
Endpoint:
https://gateway.serevixai.ai/v1/images/generations
3. Parameters¶
3.1 Header Parameters¶
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
Content-Type |
string | Yes | Sets the request content type. It must be application/json |
application/json |
Accept |
string | Yes | Sets the response content type. The recommended value is application/json |
application/json |
Authorization |
string | Yes | API key required for authentication, in the format Bearer $YOUR_API_KEY. |
Bearer $YOUR_API_KEY |
3.2 Body Parameters (application/json)¶
| Parameter | Type | Required | Description | Example (default) |
|---|---|---|---|---|
| model | string | Yes | The model ID to use. See Model List for available versions, such as imagen-3.0-generate-001. | imagen-3.0-generate-001 |
| prompt | string | Yes | A text prompt describing the image to generate. imagen-3.0 supports prompts up to 480 tokens. imagegeneration@005, imagegeneration@006, and imagegeneration@002 support prompts up to 128 tokens. | A cute baby sea otter |
| n | number | No | Number of images to generate. Must be between 1 and 4. | 1 |
| aspect_ratio | string | No | Image aspect ratio. Supported values include 1:1, 9:16, 16:9, 3:4, and 4:3. Availability varies slightly by model. | 1:1 |
| output_format | string | No | Output image format. Supported values are png and jpeg. | png |
| style | string | No | Style preset for image generation. Supported values include photograph, digital_art, landscape, sketch, watercolor, cyberpunk, and pop_art. This parameter applies only to imagegeneration@002. | photograph |
| response_format | string | No | Response image format. Only b64_json is supported. | b64_json |
4. Request Examples¶
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"
}
curl https://gateway.serevixai.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\"
}"
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.serevixai.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)
}
5. Response Example¶
{
"created": 1589478378,
"data": [
{
"b64_json": "..."
},
{
"b64_json": "..."
}
]
}