Google Imagen 3 Image Generation API

Google's most advanced image model with photorealistic quality

✓ Photorealistic quality

✓ Multiple resolutions

✓ Fast generation

✓ Batch support

احراز هویت

برای دسترسی به Google Imagen 3 API از طریق API دوی‌تانگ (Doitong)، از کلید API خود استفاده کنید. این کلید را در هدرهای REST API یا Mutationهای GraphQL قرار دهید.

مهم: کلید API خود را در مکانی امن نگه دارید و هرگز آن را در کدهای سمت کلاینت (Client-side) فاش نکنید. همیشه فراخوانی‌های API را از سمت سرور (Backend) انجام دهید.
// GraphQL Header
{
  "Authorization": "Bearer YOUR_API_KEY"
}

// REST Header
"X-API-Key": "YOUR_API_KEY"

شروع سریع

در عرض چند دقیقه کار با Google Imagen 3 API را شروع کنید. این مراحل ساده را برای ساخت اولین text to image خود دنبال کنید.

مرحله ۱: دریافت کلید API

در سایت دوی‌تانگ ثبت‌نام کنید و برای ساخت کلید API به پنل کاربری خود بروید.

مرحله ۲: اولین درخواست خود را ارسال کنید

از نمونه کدهای زیر برای ارسال اولین درخواست API خود استفاده کنید.

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"
    }
  }
}

قیمت‌گذاری

سرویس Google Imagen 3 API از مدل قیمت‌گذاری مبتنی بر اعتبار (Credit) استفاده می‌کند. میزان مصرف اعتبار بر اساس پیچیدگی و زمان محتوای تولید شده محاسبه می‌شود.

قابلیت اعتبار توضیحات
رزولوشن استاندارد 10-20 اعتبار ۵۱۲x۵۱۲ تا ۱۰۲۴x۱۰۲۴ پیکسل
رزولوشن بالا 30-50 اعتبار ۲۰۴۸x۲۰۴۸ پیکسل و بالاتر

نقاط اتصال API

سرویس Google Imagen 3 API از طریق نقطه اتصال یکپارچه GraphQL ما در دسترس است.

نقطه اتصال GraphQL

POST https://api.doitong.com/graphql

نقطه اتصال REST

POST https://api.doitong.com/v1/text_to_image

پارامترها

پارامترهای موجود برای درخواست‌های Google Imagen 3 API:

پارامتر نوع اجباری توضیحات
service رشته (String) Yes شناسه سرویس: "imagen"
prompt رشته (String) Yes توضیح متنی از آنچه می‌خواهید تولید شود
width عدد صحیح (Integer) No عرض تصویر به پیکسل (پیش‌فرض: ۱۰۲۴)
height عدد صحیح (Integer) No ارتفاع تصویر به پیکسل (پیش‌فرض: ۱۰۲۴)
version رشته (String) No Model version: "imagen-3-large", "imagen-3-medium", "imagen-3-fast-turbo"
webhookUrl رشته (String) No آدرس URL برای دریافت اعلان‌های تکمیل عملیات

فرمت پاسخ

تمامی پاسخ‌های API از یک ساختار ثابت پیروی می‌کنند:

پاسخ موفق (Success)

{
  "data": {
    "generateimage": {
      "id": "abc123xyz",
      "status": "processing",
      "url": null,
      "webhookUrl": "https://your-webhook.com/callback",
      "createdAt": "2024-01-01T00:00:00Z"
    }
  }
}

پاسخ تکمیل شده (Completed)

{
  "data": {
    "generateimage": {
      "id": "abc123xyz",
      "status": "completed",
      "url": "https://cdn.doitong.com/outputs/abc123xyz.jpg",
      "duration": null,
      "createdAt": "2024-01-01T00:00:00Z"
    }
  }
}

مدیریت خطاها

این API از کدهای وضعیت استاندارد HTTP استفاده کرده و پیام‌های خطای دقیقی را برمی‌گرداند.

کدهای خطای رایج

کد وضعیت نوع خطا توضیحات
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

فرمت پاسخ خطا

{
  "errors": [
    {
      "message": "Insufficient credits for this operation",
      "extensions": {
        "code": "INSUFFICIENT_CREDITS",
        "creditsRequired": 100,
        "creditsAvailable": 50
      }
    }
  ]
}

وبهوک‌ها (Webhooks)

هنگام اتمام فرآیند تولید text to image، اعلان‌های لحظه‌ای دریافت کنید.

راه‌اندازی وبهوک‌ها

Include a <code>webhookUrl</code> parameter in your request to receive a POST notification when processing is complete.

پیکره (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
  }
}

امنیت وبهوک

تمامی درخواست‌های وبهوک شامل یک هدر امضا (Signature) برای تایید اصالت هستند:

X-Doitong-Signature: sha256=abc123...

محدودیت نرخ فراخوانی

برای اطمینان از استفاده منصفانه و پایداری سیستم، محدودیت‌های زیر اعمال می‌شوند:

طرح درخواست در دقیقه پردازش‌های همزمان محدودیت روزانه
رایگان 10 1 100
استارتر 30 3 1,000
حرفه‌ای 60 10 10,000
سازمانی سفارشی سفارشی نامحدود
هدرهای محدودیت نرخ: برای مشاهده وضعیت فعلی محدودیت‌ها، هدرهای پاسخ را بررسی کنید:
  • X-RateLimit-Limit: Maximum requests per window
  • X-RateLimit-Remaining: Requests remaining
  • X-RateLimit-Reset: Window reset timestamp

توصیه‌های کلیدی

۱. بهینه‌سازی پرامپت‌ها

برای دریافت بهترین نتیجه، پرامپت‌های دقیق و توصیفی بنویسید:

  • Be specific about visual elements, style, and mood
  • Include details about lighting, camera angles, and composition
  • Avoid contradictory or impossible requests

۲. مدیریت پردازش‌های نامتقارن

فرآیند تولید محتوا به‌صورت نامتقارن (Async) است. از روش Polling یا Webhook استفاده کنید:

// 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;
}

۳. بازیابی خطا

منطق تلاش مجدد (Retry) را با استفاده از عقب‌نشینی نمایی (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);
    }
  }
}

۴. نظارت بر مصرف اعتبار

میزان مصرف اعتبار خود را زیر نظر داشته باشید تا از قطع سرویس جلوگیری کنید:

  • Check credit balance before large batch operations
  • Set up alerts for low credit thresholds
  • Implement credit-aware request queuing

آماده شروع هستید؟

به هزاران توسعه‌دهنده‌ای بپیوندید که از Google Imagen 3 API برای خلق محتوای شگفت‌انگیز استفاده می‌کنند.

دریافت کلید API مشاهده قیمت‌ها