Created Files:
app/api/shopee/sync/route.ts- Sync endpointapp/api/chats/route.ts- Get conversationssrc/lib/shopee-token.ts- Token utilities
What it does:
- Fetches conversations from Shopee API for all connected shops
- Stores in MongoDB
shopee_conversationscollection - Supports manual sync and auto-sync via cron
- Filter by country, status, etc.
Usage:
# Sync all shops
POST /api/shopee/sync
# Get conversations
GET /api/chats?country=SGModified Files:
app/(main)/chats/[id]/page.tsx- Chat UI with real API integrationapp/api/chats/[id]/messages/route.ts- Get messagesapp/api/chats/[id]/send/route.ts- Send message
What it does:
- Chat detail page loads real messages from Shopee
- Send button posts to Shopee API
- Real-time UI updates with loading states
- Toast notifications for success/errors
Features:
- ✅ Load real conversation messages
- ✅ Send messages to customers
- ✅ Loading and sending states
- ✅ Error handling with user feedback
Created Files:
app/api/shopee/refresh-tokens/route.ts- Cron endpointvercel.json- Cron configurationsrc/lib/shopee-token.ts- Auto-refresh logic
What it does:
- Automatically refreshes tokens expiring within 24 hours
getValidToken()checks and refreshes before API calls- Cron job runs every 12 hours via Vercel Cron
- Batch processes all connected shops
Cron Schedule:
{
"crons": [
{
"path": "/api/shopee/refresh-tokens",
"schedule": "0 */12 * * *"
},
{
"path": "/api/shopee/sync",
"schedule": "*/15 * * * *"
}
]
}Created Files:
app/api/shopee/webhook/route.ts- Webhook receiver
What it does:
- Receives real-time events from Shopee:
- New messages (code 1)
- Messages read (code 2)
- Conversation updates (code 3)
- Verifies HMAC-SHA256 signatures
- Auto-updates conversations in database
- Increments unread count for new messages
Webhook URL:
https://yourdomain.com/api/shopee/webhook
Setup: Configure in Shopee Open Platform → Webhooks → Add URL
Modified Files:
app/(main)/settings/page.tsx- Country selector + sync allapp/api/shopee/connect/route.ts- Accept country paramapp/api/shopee/callback/route.ts- Store country
Supported Countries(7か国):
- 🇸🇬 SG - Singapore
- 🇵🇭 PH - Philippines
- 🇲🇾 MY - Malaysia
- 🇹🇼 TW - Taiwan
- 🇹🇭 TH - Thailand
- 🇻🇳 VN - Vietnam
- 🇧🇷 BR - Brazil
Features:
- Dropdown to select country before connecting
- Display flags for each connected shop
- "Sync All" button to sync all shops at once
- Filter conversations by country
Libraries:
src/lib/shopee-api.ts- Shopee API wrappersrc/lib/shopee-token.ts- Token management
API Routes (9):
3. app/api/shopee/connect/route.ts
4. app/api/shopee/callback/route.ts
5. app/api/shopee/status/route.ts
6. app/api/shopee/sync/route.ts
7. app/api/shopee/refresh-tokens/route.ts
8. app/api/shopee/webhook/route.ts
9. app/api/chats/route.ts
10. app/api/chats/[id]/messages/route.ts
11. app/api/chats/[id]/send/route.ts
UI (2):
12. app/(main)/settings/page.tsx (modified)
13. app/(main)/chats/[id]/page.tsx (modified)
Config:
14. vercel.json - Cron jobs
Documentation:
15. SHOPEE_INTEGRATION.md
16. SHOPEE_COMPLETE.md
17. README_SHOPEE.md (this file)
src/components/AppLayout.tsx- Added Settings to nav.env- Added Shopee credentialsapp/login/page.tsx- Session persistence
{
shop_id: number,
shop_name: string,
country: "SG" | "PH" | "MY" | "TW" | "TH" | "VN" | "BR",
access_token: string,
refresh_token: string,
expires_at: Date,
created_at: Date,
updated_at: Date
}{
conversation_id: string,
shop_id: number,
country: string,
customer_id: number,
customer_name: string,
last_message: string,
last_message_time: Date,
unread_count: number,
pinned: boolean,
status: "active" | "resolved" | "archived",
assigned_staff?: string,
created_at: Date,
updated_at: Date
}# .env
SHOPEE_PARTNER_ID=your_partner_id
SHOPEE_PARTNER_KEY=your_partner_key
SHOPEE_REDIRECT_URL=http://localhost:3000/api/shopee/callback
CRON_SECRET=random_secret_for_cron_jobs- Start:
npm run dev - Login to Chapee
- Go to 設定 (Settings)
- Select country: SG - Singapore
- Enter your Shop ID and Authorization Code
- Click アカウント接続
- Click 全店舗同期 in Settings, OR
- Run:
curl -X POST http://localhost:3000/api/shopee/sync
- Go to ダッシュボード (Dashboard)
- Click on any conversation
- View messages from Shopee
- Type reply and click send
- Token Refresh: Every 12 hours
- Chat Sync: Every 15 minutes
# Refresh tokens
curl http://localhost:3000/api/shopee/refresh-tokens
# Sync conversations
curl -X POST http://localhost:3000/api/shopee/sync| Endpoint | Method | Description |
|---|---|---|
/api/shopee/connect |
POST | Connect shop manually |
/api/shopee/callback |
GET | OAuth callback |
/api/shopee/status |
GET | List connected shops |
/api/shopee/sync |
POST | Sync conversations |
/api/shopee/refresh-tokens |
GET | Refresh tokens (cron) |
/api/shopee/webhook |
POST | Receive webhooks |
/api/chats |
GET | Get conversations |
/api/chats/[id]/messages |
GET | Get messages |
/api/chats/[id]/send |
POST | Send message |
- Connect Singapore store via Settings
- Sync conversations:
POST /api/shopee/sync - View conversations in Dashboard
- Open chat detail and view messages
- Send a test message to customer
- Test token refresh:
GET /api/shopee/refresh-tokens - (Optional) Connect additional countries (PH, MY, etc.)
- (Optional) Set up webhook in Shopee platform
✅ Fetch real chats - Sync API implemented
✅ Send messages - Chat detail integrated
✅ Auto-refresh tokens - Background job + cron
✅ Webhooks - Real-time event handling
✅ Multi-country - SG, PH, MY, TW, TH, VN, BR(7か国)
The Shopee integration is complete and production-ready!
- SHOPEE_INTEGRATION.md - Initial setup guide
- SHOPEE_COMPLETE.md - Detailed feature documentation
- README_SHOPEE.md - This summary file
For detailed API documentation and troubleshooting, see SHOPEE_COMPLETE.md.