Using the Taobao API to Retrieve Product Information and Implement Keyword Search

Introduction

The Taobao API provides powerful functionality for retrieving product information. Developers can use it to search for products based on keywords, product IDs, categories, and more. This article focuses on how to query product details and implement a keyword-based search using the Taobao API. We’ll also walk through the relevant parameters, pagination logic, filtering strategies, and provide complete code samples in Python and Node.js.

In this article, we’ll cover:

  • Fetching product details by product ID

  • Performing keyword-based product searches

  • Using pagination and sorting

  • Applying search filters

  • Code walkthroughs in Python and Node.js

1. Overview of Product Query API

Taobao offers multiple endpoints for product information retrieval. Here are the most commonly used ones:

Function

API Method

Description

Single Product Query

taobao.item.get

Retrieve product details using a product ID

Keyword Search

taobao.items.search

Search for products by keyword

Category Lookup

taobao.itemcats.get

Get category IDs and names for filtering

These APIs are widely used in:

  • Price comparison tools

  • E-commerce data integration

  • Taocode or affiliate links

  • Personalized search engines

2. Querying a Single Product: taobao.item.get

Example Request Parameters

Parameter

Description

method

taobao.item.get

num_iid

Unique product ID

fields

Fields to return (e.g., title, price, pic_url)

Example JSON Response

{

"item_get_response": {

"item": {

"title": "Wireless Bluetooth Headphones",

"price": "129.00",

"pic_url": "https://image.taobao.com/xxx.jpg",

"num_iid": 1234567890123,

"seller_id": "taobao_seller_01"

}

}

}

3. Performing a Keyword-Based Product Search: taobao.items.search

This is the primary endpoint used to implement a search function. You can filter by keyword, category, price range, and sort the results.

Common Parameters

Parameter

Description

q

Search keyword (e.g., “headphones”)

cat

Category ID (retrievable via itemcats.get)

start_price, end_price

Filter by price range

sort

Sort results (e.g., price_asc, price_desc)

page_no, page_size

Pagination controls

4. Python Example: Keyword Search

import requests

import hashlib

import time

API_URL = 'https://api.taobao.com/endpoint'

API_KEY = 'YOUR_API_KEY'

API_SECRET = 'YOUR_API_SECRET'

def generate_signature(params):

sorted_keys = sorted(params.keys())

base_string = API_SECRET + ''.join(f"{key}{params[key]}" for key in sorted_keys) + API_SECRET

return hashlib.md5(base_string.encode('utf-8')).hexdigest().upper()

def search_products(keyword, page=1, page_size=10):

params = {

'method': 'taobao.items.search',

'api_key': API_KEY,

'q': keyword,

'page_no': page,

'page_size': page_size,

'timestamp': int(time.time())

}

params['signature'] = generate_signature(params)

response = requests.get(API_URL, params=params)

if response.status_code == 200:

result = response.json()

items = result.get('items_search_response', {}).get('items', {}).get('item', [])

for item in items:

print(f"{item['title']} - NT${item['price']}")

else:

print(f"Error: {response.status_code}, {response.text}")

search_products("Bluetooth headphones", page=1)

5. Node.js Example: Get Product Details

const axios = require('axios');

const crypto = require('crypto');

const API_URL = 'https://api.taobao.com/endpoint';

const API_KEY = 'YOUR_API_KEY';

const API_SECRET = 'YOUR_API_SECRET';

function generateSignature(params) {

const sortedKeys = Object.keys(params).sort();

let baseString = API_SECRET;

sortedKeys.forEach(key => {

baseString += key + params[key];

});

baseString += API_SECRET;

return crypto.createHash('md5').update(baseString).digest('hex').toUpperCase();

}

async function getProductById(numIid) {

const params = {

method: 'taobao.item.get',

api_key: API_KEY,

num_iid: numIid,

fields: 'title,price,pic_url',

timestamp: Math.floor(Date.now() / 1000)

};

params.signature = generateSignature(params);

try {

const response = await axios.get(API_URL, { params });

console.log('Product Info:', response.data.item_get_response.item);

} catch (err) {

console.error('Error:', err.response ? err.response.data : err.message);

}

}

getProductById(1234567890123);

6. Best Practices for Product Search

Recommendation

Details

Match keywords precisely

Use categories to narrow down broad search results

Cache popular searches

Reduce API load by caching results using Redis or similar tools

Sorting options

Allow users to sort by price, popularity, or update time

Debounce user input

Prevent flooding the API with real-time queries by throttling input

Conclusion

In this article, we explored how to use the Taobao API to:

  • Retrieve a single product’s information

  • Perform advanced keyword searches with pagination and filters

These tools allow you to build powerful product discovery systems for various use cases.

Articles related to APIs :

If you need the Taobao API, feel free to contact us : support@luckdata.com