文档

快速上手

在几分钟内开始使用照片修复API。本指南将带您完成将AI驱动的照片修复集成到应用程序中的基本步骤。

前置条件

在开始之前,请确保您拥有:

  • 账户:在 restoreoldphotos.online 注册账户
  • API密钥:在设置中创建API密钥
  • 积分:购买照片修复积分
  • 图片URL:用于测试的可公开访问的图片URL

第一步:获取您的API密钥

  1. 访问 restoreoldphotos.online
  2. 使用您的Google账户登录
  3. 导航到设置 > API密钥
  4. 点击"创建新API密钥"按钮
  5. 输入密钥名称(例如:"我的应用")
  6. 创建密钥后,复制完整的密钥值(请妥善保管!)

API密钥格式

  • sk- 开头
  • 包含随机生成的字符
  • 例如:sk-abc123def456ghi789...

第二步:发起您的第一个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错误: ${response.status}`);
    }
 
    const result = await response.json();
    return result.output; // URL或base64字符串
  } catch (error) {
    console.error('错误:', error);
    throw error;
  }
}
 
// 使用方法
const restoredImage = await restorePhoto(
  'https://example.com/old-photo.jpg',
  'your-api-key-here'
);
console.log('修复后的图片:', restoredImage);

第三步:处理响应

API返回包含修复图片的JSON响应:

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

或base64格式:

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

output字段包含:

  • 图片URL:修复图片的直接链接
  • Base64字符串:编码为base64的图片数据

第四步:显示结果

在HTML中

<img src="https://replicate.delivery/pbxt/xxx.png" alt="修复后的照片" />

在JavaScript中

// 对于URL或base64
const img = document.createElement('img');
img.src = restoredImage; // 来自API响应的output字段
document.body.appendChild(img);

第五步:错误处理

始终实现适当的错误处理:

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;