A small Mastra-based agent + static UI that can:
- Fetch top 3 Product Hunt products for any given day.
- Search products using the Product Hunt Algolia index.
- Answer questions about launching on Product Hunt.
- Launch confetti on the front end.
- Host a dummy Product Hunt–style page on GitHub Pages and embed the agent there.
This folder contains:
src/— Agent, tools, a lightweight server (src/server.ts).web/— Static site that looks like a PH page, with a chat widget and confetti.
Live demo: cometchat.github.io/ai-agent-mastra-examples/product-hunt-agent/web
Prereqs: Node 20+.
-
Install deps:
- If needed, run
npm installin the repo root.
- If needed, run
-
Build the server code:
npm run build:server
-
Start the API server:
npm run serve- It listens on
http://localhost:8787.
-
Open the static UI:
- Open
web/index.htmlin your browser (or serveweb/with any static server). - The UI calls the local API by default.
- Open
For real Product Hunt post data and agent answers:
PRODUCTHUNT_API_TOKEN— Bearer token for Product Hunt GraphQL v2 (https://api.producthunt.com/v2/api/graphql). Required for the/api/top*endpoints to return live posts; if omitted, those endpoints respond with empty arrays.OPENAI_API_KEY— For the agent’s Q&A about launching.
Search note: Product search uses the public Product Hunt Algolia index with fixed public credentials (from the PH wiki) and does not require any env vars.
GET /api/health→{ ok: true }GET /api/top?limit=3→{ posts: PHPost[], first: number, order: 'VOTES' }- Top all-time by votes.
limit(akafirst) is clamped to 1–10.
- Top all-time by votes.
GET /api/top-week?limit=3&days=7→{ posts, first, days, order: 'RANKING', window: 'rolling-week' }GET /api/top-range?timeframe=today&tz=America/New_York&limit=3→{ posts, first, timeframe, tz, order: 'RANKING' }- Timeframes supported: today, yesterday, this-week, last-week, this-month, last-month, YYYY-MM-DD, or ranges like
from:2024-08-01 to:2024-08-15.
- Timeframes supported: today, yesterday, this-week, last-week, this-month, last-month, YYYY-MM-DD, or ranges like
GET /api/search?q=term&limit=10→{ hits: Hit[], q, limit }- Backed by Product Hunt’s public Algolia index using this request:
- URL:
https://0h4smabbsg-dsn.algolia.net/1/indexes/Post_production?query={q}&hitsPerPage={limit} - Headers:
X-Algolia-API-Key: 9670d2d619b9d07859448d7628eea5f3,X-Algolia-Application-Id: 0H4SMABBSG
- URL:
- Backed by Product Hunt’s public Algolia index using this request:
POST /api/chat→{ reply: string }with JSON body{ message: string }
CORS is enabled for demo usage.
-
Push the
web/directory to your public repo and enable GitHub Pages (source:/(root)or/docs, or an action). If you useweb/as Pages root, set that in the repo settings. -
In
web/index.html, setwindow.PH_AGENT_APIto your deployed API server URL (Render/Vercel/Fly/etc.). Example:<script> window.PH_AGENT_API = 'https://your-api.example.com'; </script>
-
The page includes a chat widget with a Product Hunt–style icon, and a Launch button that fires confetti.
- Without
PRODUCTHUNT_API_TOKEN, top posts endpoints return empty arrays; search still works using the public Algolia index. - The chat agent uses OpenAI via
@ai-sdk/openaiand Mastra’s Agent. ProvideOPENAI_API_KEYfor real answers. - This is a minimal demo; harden auth, rate limits, and error handling before production.
npm run build:server— TS →dist/for the server.npm run serve— Start the server at:8787.npm run dev/npm run start— Mastra CLI (if you prefer Mastra’s workflow).
This app is self-contained. Make the repo public (or copy this folder to a new public repo) to share the source code.
- Guide:
docs/mastra-product-hunt-agent.md— Step-by-step on building, running, wiring frontend actions, and connecting with CometChat.