Bỏ qua đến nội dung
API nhà phát triển

Đưa tăng cường ảnh AI vào sản phẩm của bạn

REST API cho tăng cường AI (làm mịn da, phóng to, tự động nâng cấp, xóa nền) với webhook outbound. Xác thực bằng API key và nhận sự kiện đã ký khi job hoàn tất.

Xác thực

Mọi request được xác thực bằng API key gửi trong header x-api-key . Tạo và quản lý key trong dashboard. Key chỉ hiện đầy đủ một lần — hãy lưu an toàn. Không dùng Bearer token trên public API.

curl https://pickimg.com/api/ai/enhance \
  -H "x-api-key: YOUR_API_KEY"

Phạm vi (scope)

Mỗi key mang các scope. Thiếu scope bắt buộc sẽ trả 403.

image:readĐọc job và nguồn ảnh
image:writeTải lên và xử lý ảnh
ai:enhanceTải nguồn, tạo và poll job AI
webhook:manageTạo và xem webhook outbound

Key mới mặc định image:read, image:write và ai:enhance. webhook:manage cần bật thêm.

Lỗi HTTP

Dùng các mã này khi viết retry và luồng nâng cấp.

401Thiếu hoặc sai API key.
403Key thiếu scope cần thiết. Hết hạn mức tăng cường hoặc lưu trữ tháng — job bị chặn, không tính overage.
429Vượt RPM theo từng key. Đợi rồi gửi lại.

Phản hồi 429 gồm Retry-After, X-RateLimit-Limit, X-RateLimit-Remaining.

Tăng cường AI

POST/api/ai/uploadTải ảnh nguồn
POST/api/ai/enhanceTạo job tăng cường
GET/api/ai/enhance/:idLấy trạng thái & kết quả job
GET/api/ai/enhanceLiệt kê job của bạn

POST /api/ai/upload dùng multipart field file (JPEG, PNG, WebP hoặc AVIF, tối đa 20MB) và trả { url } cho sourceUrl.

Thao tác: smooth_skin, upscale, auto_enhance, bg_remove. level: low | medium | high.

Poll GET đến khi status là queued, processing, completed, failed.

Upload, enhance, poll

# 1. Upload a source image (multipart field: file, max 20MB)
curl -X POST https://pickimg.com/api/ai/upload \
  -H "x-api-key: YOUR_API_KEY" \
  -F "file=@./photo.jpg"
# { "url": "https://cdn.example.com/uploads/..." }

# 2. Queue enhancement
curl -X POST https://pickimg.com/api/ai/enhance \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "sourceUrl": "https://cdn.example.com/uploads/photo.jpg",
    "operation": "smooth_skin",
    "level": "medium"
  }'
# { "id": "job_123", "status": "queued", "operation": "smooth_skin" }

# 3. Poll until completed or failed
curl https://pickimg.com/api/ai/enhance/job_123 \
  -H "x-api-key: YOUR_API_KEY"
# { "id": "job_123", "status": "completed", "resultUrl": "https://cdn.example.com/out.jpg" }
// Node.js
const key = process.env.PICKIMG_KEY;

const form = new FormData();
form.append("file", blob, "photo.jpg");
const uploaded = await fetch("https://pickimg.com/api/ai/upload", {
  method: "POST",
  headers: { "x-api-key": key },
  body: form,
}).then((r) => r.json());

const job = await fetch("https://pickimg.com/api/ai/enhance", {
  method: "POST",
  headers: {
    "x-api-key": key,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    sourceUrl: uploaded.url,
    operation: "upscale",
  }),
}).then((r) => r.json());

const poll = await fetch(`https://pickimg.com/api/ai/enhance/${job.id}`, {
  headers: { "x-api-key": key },
}).then((r) => r.json());

SDK chính thức (v0.1.0)

JavaScript (@pickimg/sdk) và Python (pickimg) bọc upload → enhance → poll. Cài từ clone repo cho đến khi publish npm/PyPI. Không đưa token vào source.

# From a clone of the pickimg repo (v0.1.0)
pnpm add ./sdk/js
pip install ./sdk/python

# After npm / PyPI publish:
# npm install @pickimg/sdk
# pip install pickimg
import { PickimgClient } from "@pickimg/sdk";

const client = new PickimgClient({ apiKey: process.env.PICKIMG_API_KEY! });

// 1. Upload
const { url } = await client.uploadImage(file);
// 2. Enhance
const job = await client.enhance({ sourceUrl: url, operation: "smooth_skin" });
// 3. Poll
const done = await client.waitForEnhancement(job.id);
console.log(done.resultUrl);
from pickimg import PickimgClient

client = PickimgClient(api_key="YOUR_KEY")

# 1. Upload
with open("photo.jpg", "rb") as fh:
    upload = client.upload_image(fh)
# 2. Enhance
job = client.enhance(source_url=upload["url"], operation="smooth_skin")
# 3. Poll
done = client.wait_for_enhancement(job.id)
print(done.result_url)

Webhook

Đăng ký endpoint để nhận sự kiện khi job hoàn tất. Mỗi lần gửi đều được ký để bạn xác minh tính xác thực.

GET/api/webhooks/eventsLiệt kê loại sự kiện
POST/api/webhooks/endpointsTạo endpoint
GET/api/webhooks/deliveriesXem các lần gửi

Sự kiện: enhancement.queued, enhancement.completed, enhancement.failed, image.uploaded, quota.exceeded, ping, job.completed, upload.finished.

job.completed cũng được gửi cùng image.completed và enhancement.completed. upload.finished cũng được gửi cùng image.uploaded. Dùng alias nếu muốn một handler cho cả hai loại job.

Xác minh chữ ký

Mỗi lần gửi kèm header X-PickIMG-Signature theo định dạng t=<timestamp>,v1=<hmac>, trong đó HMAC-SHA256 được tính trên <timestamp>.<raw-body>.

import crypto from "crypto";

function verify(rawBody, header, secret) {
  const [t, v1] = header.split(",").map((p) => p.split("=")[1]);
  const expected = crypto
    .createHmac("sha256", secret)
    .update(`${t}.${rawBody}`)
    .digest("hex");
  return crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected));
}

Giới hạn tốc độ & hạn mức

RPM tính theo từng API key (trần gói). Số lần AI tháng và dung lượng dùng chung mọi key cùng tài khoản.

GóiRPMAI / thángLưu trữAPI key
Free30501 GB1
Personal1202,00050 GB3
Business60020,000500 GB15
Enterprisetùy chỉnhtùy chỉnhtùy chỉnhtùy chỉnh

Hết hạn mức trả 403. RPM trả 429 kèm Retry-After. Hạn mức Enterprise là tùy chỉnh — liên hệ sales, không giả định số self-serve.

Khi vượt hạn mức API trả 403 kèm gợi ý nâng cấp. Xem bảng giá.