Hey there, fellow dev! Ready to dive into the world of Shopee API integration? You're in for a treat. Shopee's API is a powerhouse for e-commerce, and mastering it can seriously level up your game. Let's get cracking!
Before we jump in, make sure you've got:
Alright, let's lay the groundwork:
mkdir shopee-api-integration cd shopee-api-integration npm init -y npm install axios crypto-js
Easy peasy, right? We're using axios
for HTTP requests and crypto-js
for generating signatures.
Now for the fun part - getting that access token:
const crypto = require('crypto-js'); function generateSignature(partnerId, apiKey, apiSecret, timestamp) { const baseString = partnerId + apiKey + timestamp; return crypto.HmacSHA256(baseString, apiSecret).toString(); } // Use this to get your access token
Pro tip: Store that access token securely. You'll need it for every request.
Here's where the magic happens:
const axios = require('axios'); async function makeApiRequest(endpoint, params) { try { const response = await axios.post(`https://partner.shopeemobile.com/api/v2${endpoint}`, params, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${YOUR_ACCESS_TOKEN}` } }); return response.data; } catch (error) { console.error('API request failed:', error); throw error; } }
Let's tackle some key operations:
async function getProductList() { return await makeApiRequest('/product/get_item_list', { // Add necessary params }); }
async function getOrderDetails(orderSn) { return await makeApiRequest('/order/get_order_detail', { order_sn_list: [orderSn] }); }
Shopee loves to keep you in the loop. Set up a webhook endpoint and let the data flow:
app.post('/shopee-webhook', (req, res) => { const event = req.body; // Handle the event based on its type console.log('Received webhook:', event); res.sendStatus(200); });
Shopee's got limits, so play nice:
const rateLimit = require('express-rate-limit'); const apiLimiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 100 // limit each IP to 100 requests per windowMs }); app.use('/api/', apiLimiter);
Postman is your best friend here. Create a collection, save your requests, and test away. And don't forget to log everything - future you will thank present you.
When you're ready to go live:
And there you have it! You're now armed and dangerous with Shopee API integration skills. Remember, the API docs are your north star - keep them close.
Happy coding, and may your integration be ever smooth and your errors be few!