Google Imagen 3 Image Generation API
Google's most advanced image model with photorealistic quality
✓ Photorealistic quality
✓ Multiple resolutions
✓ Fast generation
✓ Batch support
Xác thực
Để truy cập Google Imagen 3 API thông qua Doitong API, hãy sử dụng mã API (API key) của bạn. Hãy đính kèm mã này vào mutation GraphQL hoặc header của REST API.
// GraphQL Header
{
"Authorization": "Bearer YOUR_API_KEY"
}
// REST Header
"X-API-Key": "YOUR_API_KEY"
Bắt đầu nhanh
Bắt đầu với Google Imagen 3 API chỉ trong vài phút. Làm theo các bước đơn giản sau để tạo text to image đầu tiên của bạn.
Bước 1: Lấy mã API
Đăng ký tài khoản Doitong và đi tới bảng điều khiển để tạo mã API.
Bước 2: Thực hiện yêu cầu đầu tiên
Sử dụng một trong các ví dụ mã dưới đây để thực hiện lệnh gọi API đầu tiên.
curl -X POST https://api.doitong.com/graphql \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"query": "mutation Generate($input: GenerateInput!) { generate(input: $input) { id status type provider url metadata creditCost } }",
"variables": {
"input": {
"type": "IMAGE",
"provider": "imagen",
"input": {
"text": "A futuristic city with flying cars and neon lights"
},
"options": {
"width": 1024,
"height": 1024
}
}
}
}'
const response = await fetch('https://api.doitong.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
query: `
mutation GenerateImage($input: ImageGenerationInput!) {
generateImage(input: $input) {
id
url
width
height
}
}
`,
variables: {
input: {
service: 'imagen',
prompt: 'A futuristic city with flying cars and neon lights',
width: 1024,
height: 1024
}
}
})
});
const data = await response.json();
console.log('Image:', data.data.generateImage);
import requests
import json
url = "https://api.doitong.com/graphql"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
query = """
mutation Generate($input: GenerateInput!) {
generate(input: $input) {
id
status
type
provider
url
metadata
creditCost
}
}
"""
variables = {
"input": {
"type": "IMAGE",
"provider": "imagen",
"input": {
"text": "A futuristic city with flying cars and neon lights"
},
"options": {
"width": 1024,
"height": 1024
}
}
}
response = requests.post(url, json={
"query": query,
"variables": variables
}, headers=headers)
data = response.json()
print("Result:", data["data"]["generate"])
mutation Generate($input: GenerateInput!) {
generate(input: $input) {
id
status
type
provider
url
metadata
creditCost
createdAt
}
}
# Variables
{
"input": {
"type": "IMAGE",
"provider": "imagen",
"input": {
"text": "A futuristic city with flying cars and neon lights"
},
"options": {
"width": 1024,
"height": 1024,
"version": "imagen-3-fast-turbo",
"quality": "high"
}
}
}
Bảng giá
Google Imagen 3 API sử dụng mô hình tính phí theo tín dụng (credit). Tín dụng sẽ được tiêu thụ dựa trên độ phức tạp và thời lượng của nội dung được tạo.
| Tính năng | Tín dụng | Mô tả |
|---|---|---|
| Độ phân giải Tiêu chuẩn | 10-20 Tín dụng | 512x512 đến 1024x1024 pixel |
| Độ phân giải Cao | 30-50 Tín dụng | 2048x2048 pixel trở lên |
Điểm cuối API
Google Imagen 3 API khả dụng thông qua điểm cuối GraphQL hợp nhất của chúng tôi.
Điểm cuối GraphQL
POST https://api.doitong.com/graphql
Điểm cuối REST
POST https://api.doitong.com/v1/text_to_image
Tham số
Các tham số khả dụng cho yêu cầu Google Imagen 3 API:
| Tham số | Loại | Bắt buộc | Mô tả |
|---|---|---|---|
service |
Chuỗi | Yes | Định danh dịch vụ: "imagen" |
prompt |
Chuỗi | Yes | Mô tả bằng văn bản về nội dung cần tạo |
width |
Số nguyên | No | Chiều rộng hình ảnh tính bằng pixel (mặc định: 1024) |
height |
Số nguyên | No | Chiều cao hình ảnh tính bằng pixel (mặc định: 1024) |
version |
Chuỗi | No | Model version: "imagen-3-large", "imagen-3-medium", "imagen-3-fast-turbo" |
webhookUrl |
Chuỗi | No | URL để nhận thông báo khi hoàn tất |
Định dạng phản hồi
Tất cả phản hồi API đều tuân theo một định dạng nhất quán:
Phản hồi thành công
{
"data": {
"generateimage": {
"id": "abc123xyz",
"status": "processing",
"url": null,
"webhookUrl": "https://your-webhook.com/callback",
"createdAt": "2024-01-01T00:00:00Z"
}
}
}
Phản hồi hoàn tất
{
"data": {
"generateimage": {
"id": "abc123xyz",
"status": "completed",
"url": "https://cdn.doitong.com/outputs/abc123xyz.jpg",
"duration": null,
"createdAt": "2024-01-01T00:00:00Z"
}
}
}
Xử lý lỗi
API sử dụng các mã trạng thái HTTP tiêu chuẩn và trả về thông báo lỗi chi tiết.
Các mã lỗi phổ biến
| Mã trạng thái | Loại lỗi | Mô tả |
|---|---|---|
| 400 | Bad Request | Invalid parameters or malformed request |
| 401 | Unauthorized | Missing or invalid API key |
| 402 | Payment Required | Insufficient credits |
| 429 | Too Many Requests | Rate limit exceeded |
| 500 | Internal Server Error | Server error, please retry |
Định dạng phản hồi lỗi
{
"errors": [
{
"message": "Insufficient credits for this operation",
"extensions": {
"code": "INSUFFICIENT_CREDITS",
"creditsRequired": 100,
"creditsAvailable": 50
}
}
]
}
Webhooks
Nhận thông báo theo thời gian thực khi quá trình tạo text to image của bạn hoàn tất.
Thiết lập Webhooks
Include a <code>webhookUrl</code> parameter in your request to receive a POST notification when processing is complete.
Dữ liệu Webhook (Payload)
{
"id": "abc123xyz",
"status": "completed",
"url": "https://cdn.doitong.com/outputs/abc123xyz.jpg",
"service": "imagen",
"createdAt": "2024-01-01T00:00:00Z",
"completedAt": "2024-01-01T00:01:00Z",
"metadata": {
"duration": null,
"width": 1920,
"height": 1080
}
}
Bảo mật Webhook
Tất cả các yêu cầu webhook đều bao gồm một header chữ ký để xác thực:
X-Doitong-Signature: sha256=abc123...
Giới hạn tốc độ
Để đảm bảo sử dụng công bằng và ổn định hệ thống, các giới hạn tốc độ sau sẽ được áp dụng:
| Gói dịch vụ | Yêu cầu/Phút | Công việc đồng thời | Giới hạn hàng ngày |
|---|---|---|---|
| Miễn phí | 10 | 1 | 100 |
| Khởi đầu | 30 | 3 | 1,000 |
| Chuyên nghiệp | 60 | 10 | 10,000 |
| Doanh nghiệp | Tùy chỉnh | Tùy chỉnh | Không giới hạn |
X-RateLimit-Limit: Maximum requests per windowX-RateLimit-Remaining: Requests remainingX-RateLimit-Reset: Window reset timestamp
Lời khuyên hữu ích
1. Tối ưu hóa câu lệnh (Prompt)
Viết các câu lệnh rõ ràng, chi tiết để có kết quả tốt nhất:
- Be specific about visual elements, style, and mood
- Include details about lighting, camera angles, and composition
- Avoid contradictory or impossible requests
2. Xử lý bất đồng bộ
Quá trình tạo là bất đồng bộ. Hãy triển khai cơ chế polling hoặc webhook phù hợp:
// Polling example
async function pollStatus(jobId) {
let status = 'processing';
while (status === 'processing') {
await sleep(2000); // Wait 2 seconds
const result = await checkJobStatus(jobId);
status = result.status;
}
return result;
}
3. Khôi phục lỗi
Triển khai logic thử lại (retry) với khoảng thời gian chờ tăng dần (exponential backoff):
async function retryWithBackoff(fn, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
await sleep(Math.pow(2, i) * 1000);
}
}
}
4. Theo dõi mức sử dụng tín dụng
Theo dõi lượng tiêu thụ tín dụng của bạn để tránh bị gián đoạn:
- Check credit balance before large batch operations
- Set up alerts for low credit thresholds
- Implement credit-aware request queuing
Sẵn sàng bắt đầu?
Tham gia cùng hàng ngàn nhà phát triển đang sử dụng Google Imagen 3 API để tạo ra nội dung tuyệt vời