Auto loop bot for Shopee Affiliate / TikTok Shop Affiliate: discover trending products β generate a caption with Claude β post to your own social channels β track clicks/orders/commission. Runs as a Cloudflare Worker on a Cron Trigger (hourly by default), backed by a D1 database.
FORCE_MOCK_MODE = "true" in wrangler.toml is on by default. In this mode
every step (product discovery, content generation, publishing) uses fake
data so you can see the full loop run end-to-end (/status after /run)
before connecting any real account. Nothing is posted to real social
media or fetched from Shopee/TikTok while this is on.
Turn it off ("false") only after you've configured the real secrets below
for the platforms you want live.
- Anthropic API key β https://console.anthropic.com β used for caption generation. Without it, captions fall back to a mock template.
- Shopee Affiliate Program β apply at https://affiliate.shopee.co.th
(or your country's Shopee Affiliate site). Approval is required before
Shopee issues Open API (
AppId/AppSecret) credentials for the Affiliate Open API (GraphQL,open-api.affiliate.shopee.com/graphql). - TikTok Shop Partner Center β https://partner.tiktokshop.com. TikTok's
affiliate/creator marketplace API access and auth flow depend on your
region and approval status β
src/providers/affiliate/tiktok.tsis left unimplemented on purpose until you have real docs/access to verify against (do not trust a fabricated endpoint here). - Facebook Page + Graph API app β https://developers.facebook.com,
create an app, add a Page, generate a long-lived Page Access Token with
pages_manage_posts. Needed to auto-post to your Page. - Instagram Business/Creator account linked to that Facebook Page β posting goes through the same Graph API token as Facebook.
- TikTok Content Posting API β separate from TikTok Shop; needs its own
developer app +
video.publishscope + content-posting audit. Left unimplemented insrc/providers/publish/tiktok.tsfor the same reason as above β TikTok requires a multi-step upload flow, not a single caption+image call.
Once you have credentials, set them as Worker secrets (never commit them):
wrangler secret put ANTHROPIC_API_KEY
wrangler secret put SHOPEE_APP_ID
wrangler secret put SHOPEE_APP_SECRET
wrangler secret put TIKTOK_APP_KEY
wrangler secret put TIKTOK_APP_SECRET
wrangler secret put TIKTOK_ACCESS_TOKEN
wrangler secret put FACEBOOK_PAGE_ID
wrangler secret put FACEBOOK_PAGE_ACCESS_TOKEN
wrangler secret put INSTAGRAM_BUSINESS_ACCOUNT_ID
For local dev, copy .dev.vars.example to .dev.vars and fill in the same
values instead.
npm install
wrangler login
# create the D1 database and paste the returned database_id into wrangler.toml
wrangler d1 create affiliate_bot
npm run db:migrate:local # local dev DB
npm run db:migrate:remote # production DBnpm run dev
curl -X POST http://localhost:8787/run
curl http://localhost:8787/statusnpm run deployThis registers the Worker and the hourly Cron Trigger (wrangler.toml
[triggers]). Adjust the cron schedule and PRODUCTS_PER_RUN to taste.
src/
index.ts Worker entrypoint: scheduled handler (cron) + /run, /status
orchestrator.ts Ties discovery -> content -> publish -> tracking together
types.ts Shared types (Env bindings, domain types)
db/
schema.sql D1 tables: products, posts, commission_snapshots, run_logs
client.ts Query helpers
providers/
affiliate/ Product discovery per platform (Shopee, TikTok, mock)
content/ Caption generation (Claude, mock fallback)
publish/ Posting adapters (Facebook, Instagram, TikTok, mock)
tracking/ Commission/click/order snapshot capture
Every provider category has a mock implementation and a factory
(getAffiliateProvider, getPublishAdapters) that falls back to mock mode
whenever real credentials are absent or FORCE_MOCK_MODE is on β so the
loop is always runnable, and you can light up one real integration at a
time without breaking the others.
ShopeeAffiliateProviderimplements the documented request-signing pattern but its GraphQL query shape is not verified against a live account β confirm field names in Shopee's GraphQL playground once approved.TikTokAffiliateProvider(discovery) andTikTokPublishAdapter(posting) are stubs β TikTok's APIs need to be implemented against real Partner Center / Developer docs once you have access.- No image generation yet β
imageUrlis passed through from whatever the affiliate API returns; Instagram in particular requires a public image URL to post. - No dedup/rate-limiting logic yet to avoid re-posting the same product
across runs β add a check against
posts/productsinorchestrator.tsbefore scaling up posting frequency.


