Skip to content

Code Mẫu: Python

Hướng dẫn tích hợp ImageCoreAPI bằng ngôn ngữ Python sử dụng thư viện httpx (hoặc requests).


Cài Đặt Thư Viện

bash
pip install httpx

1. Kiểm Tra Thông Tin Key (/v1/me)

python
import httpx

API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.yourdomain.com/v1"

headers = {
    "X-API-Key": API_KEY,
}

response = httpx.get(f"{BASE_URL}/me", headers=headers)
print("Trạng thái Key:", response.json())

2. Tìm Kiếm Sản Phẩm Shopee

Tìm kiếm sản phẩm từ đường dẫn URL ảnh hoặc ảnh Shopee có sẵn, và tự động in ra đường link sản phẩm để xem trực tiếp:

python
import httpx

API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.yourdomain.com/v1"

payload = {
    "cookie": "SPC_EC=your_cookie_here; SPC_F=your_spc_f_here...",
    "image_url": "https://example.com/sample-clothing.jpg",
    "country": "VN",
    "page": 1,
}

headers = {
    "Content-Type": "application/json",
    "X-API-Key": API_KEY,
}

with httpx.Client(timeout=60.0) as client:
    resp = client.post(f"{BASE_URL}/shopee/search", headers=headers, json=payload)
    data = resp.json()

    if resp.status_code == 200:
        items = data.get("data", {}).get("items", [])
        print(f"Tìm thấy {len(items)} sản phẩm tương tự trên Shopee:\n")
        
        for item in items[:5]:
            name = item.get("name")
            price = item.get("price")
            shopid = item.get("shopid")
            itemid = item.get("itemid")
            
            # Ghép URL xem sản phẩm Shopee
            product_url = f"https://shopee.vn/product/{shopid}/{itemid}"
            print(f"- {name}")
            print(f"  Giá: {price:,} đ | Link: {product_url}\n")
    else:
        print("Lỗi tìm kiếm Shopee:", data)

3. Tìm Kiếm Sản Phẩm TikTok Shop

Hàm tiện ích chuyển đổi tệp ảnh trên máy sang Base64 và tìm kiếm sản phẩm:

python
import base64
import httpx

API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.yourdomain.com/v1"

def search_tiktok_by_image(image_path: str):
    # 1. Đọc và mã hóa ảnh sang Base64
    with open(image_path, "rb") as img_file:
        b64_str = base64.b64encode(img_file.read()).decode("utf-8")

    payload = {"image_base64": b64_str}
    headers = {
        "Content-Type": "application/json",
        "X-API-Key": API_KEY,
    }

    # 2. Gửi request tới Gateway
    with httpx.Client(timeout=60.0) as client:
        resp = client.post(f"{BASE_URL}/tiktok/search", headers=headers, json=payload)
        data = resp.json()

        if resp.status_code == 200:
            products = data.get("data", {}).get("products", [])
            print(f"Tìm thấy {len(products)} sản phẩm trên TikTok Shop:\n")
            
            for prod in products[:5]:
                title = prod.get("title")
                real_price = prod.get("price", {}).get("real_price")
                prod_id = prod.get("product_id")
                
                # Ghép URL xem sản phẩm TikTok Shop
                product_url = f"https://shop.tiktok.com/view/product/{prod_id}"
                print(f"- {title}")
                print(f"  Giá: {real_price} đ | Link: {product_url}\n")
        else:
            print("Lỗi tìm kiếm TikTok:", data)

# Thực thi tìm kiếm
search_tiktok_by_image("san_pham_test.jpg")

4. Upload Tệp Ảnh Lên TikTok CDN

python
import httpx

API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.yourdomain.com/v1"

def upload_tiktok_image(image_path: str):
    with open(image_path, "rb") as f:
        files = {"file": (image_path, f, "image/jpeg")}
        headers = {"X-API-Key": API_KEY}
        
        with httpx.Client(timeout=60.0) as client:
            resp = client.post(f"{BASE_URL}/tiktok/upload", headers=headers, files=files)
            print("Kết quả Upload:", resp.json())

upload_tiktok_image("san_pham_test.jpg")

ImageCoreAPI — Cổng API tìm kiếm hình ảnh Shopee & TikTok Shop