文檔

快速開始

在短短幾分鐘內開始使用照片修復 API。本指南將帶您完成將 AI 驅動的照片修復整合到應用程式中的基本步驟。

前置需求

在開始之前,請確保您擁有:

  • 帳戶:在 restoreoldphotos.online 註冊
  • API 金鑰:在設定中建立 API 金鑰
  • 點數:購買照片修復點數
  • 圖片 URL:用於測試的公開可存取圖片 URL

步驟 1:取得您的 API 金鑰

  1. 造訪 restoreoldphotos.online
  2. 使用您的 Google 帳戶登入
  3. 導航至設定 > API 金鑰
  4. 點擊「建立新 API 金鑰」按鈕
  5. 輸入金鑰名稱(例如:「我的應用程式」)
  6. 建立金鑰後,複製完整的金鑰值(請妥善保管!)

API 金鑰格式

  • sk- 開頭
  • 包含隨機產生的字元
  • 範例:sk-abc123def456ghi789...

步驟 2:進行您的第一次 API 呼叫

使用 cURL

curl -X POST https://restoreoldphotos.online/api/api-call/photo-restore \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "imageUrl": "https://example.com/old-photo.jpg"
  }'

使用 JavaScript

async function restorePhoto(imageUrl, apiKey) {
  try {
    const response = await fetch('https://restoreoldphotos.online/api/api-call/photo-restore', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`
      },
      body: JSON.stringify({
        imageUrl: imageUrl
      })
    });
 
    if (!response.ok) {
      throw new Error(`API Error: ${response.status}`);
    }
 
    const result = await response.json();
    return result.output; // URL 或 base64 字串
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}
 
// 使用方式
const restoredImage = await restorePhoto(
  'https://example.com/old-photo.jpg',
  'your-api-key-here'
);
console.log('Restored image:', restoredImage);

步驟 3:處理回應

API 會回傳包含修復後圖片的 JSON 回應:

{
  "output": "https://replicate.delivery/pbxt/xxx.png"
}

或以 base64 格式:

{
  "output": "data:image/png;base64,iVBORw0KGgoAAAANS..."
}

output 欄位包含:

  • 圖片 URL:修復後圖片的直接連結
  • Base64 字串:編碼為 base64 的圖片資料

步驟 4:顯示結果

在 HTML 中

<img src="https://replicate.delivery/pbxt/xxx.png" alt="Restored Photo" />

在 JavaScript 中

// 適用於 URL 或 base64
const img = document.createElement('img');
img.src = restoredImage; // API 回應中的 output 欄位
document.body.appendChild(img);

步驟 5:錯誤處理

始終實作適當的錯誤處理:

try {
  const restoredImage = await restorePhoto(imageUrl, apiKey);
  // 處理成功
} catch (error) {
  if (error.message.includes('401')) {
    console.error('無效的 API 金鑰');
  } else if (error.message.includes('402')) {
    console.error('點數不足');
  } else if (error.message.includes('429')) {
    console.error('超過速率限制');
  } else {
    console.error('API 錯誤:', error.message);
  }
}

API 金鑰管理

在設定中管理金鑰

  • 檢視所有 API 金鑰清單
  • 檢視金鑰使用統計
  • 停用或刪除未使用的金鑰
  • 檢視金鑰的最後使用時間

安全最佳實踐

  • 切勿在客戶端程式碼中硬編碼 API 金鑰
  • 使用環境變數儲存金鑰
  • 定期輪換 API 金鑰
  • 為不同環境使用不同的金鑰

環境變數範例

# .env 檔案
PHOTO_RESTORE_API_KEY=sk-your-api-key-here
// 在程式碼中使用
const apiKey = process.env.PHOTO_RESTORE_API_KEY;