Glanceway Glanceway
All sources

Product Hunt

News v1.0.0

Today's top products from Product Hunt

@codytseng #producthunt #products #startups

Configuration

Name Key Type Required Default Description
API Token API_TOKEN secret Yes Product Hunt Developer Token (get one at https://api.producthunt.com/v2/oauth/applications)

Source Code

version: 1.0.0
name: Product Hunt
description: Today's top products from Product Hunt
author: codytseng
author_url: https://github.com/codytseng
category: News
tags:
  - producthunt
  - products
  - startups

config:
  - key: API_TOKEN
    name: API Token
    type: secret
    required: true
    description: Product Hunt Developer Token (get one at https://api.producthunt.com/v2/oauth/applications)
module.exports = async (api) => {
  const token = api.config.get("API_TOKEN");

  async function fetchData() {
    const query = `{
      posts(order: RANKING) {
        edges {
          node {
            id
            name
            tagline
            url
            votesCount
            createdAt
          }
        }
      }
    }`;

    const response = await api.fetch("https://api.producthunt.com/v2/api/graphql", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ query }),
    });

    if (!response.ok || !response.json) {
      throw new Error(`Failed to fetch Product Hunt posts (HTTP ${response.status})`);
    }

    api.emit(
      response.json.data.posts.edges.map((edge) => ({
        id: edge.node.id,
        title: edge.node.name,
        subtitle: edge.node.tagline,
        url: edge.node.url,
        timestamp: edge.node.createdAt,
      })),
    );
  }

  await fetchData();

  return {
    refresh: fetchData,
  };
};