Google Trends API in JavaScript: extract Google Trends data from Node.js

The Google Trends API runs from Node.js with the apify-client package: call the Actor kwerix/google-trends-api with a keyword list, then list the items of the run's dataset. A single HTTP call to the run-sync endpoint does the same with fetch, and the Apify API accepts browser requests, so a front end can run it with the user's own token.

KwerixPublished

JavaScript code that calls the Google Trends API in JavaScript from Node.js with apify-client

There are 4 steps to call the Google Trends API from Node.js.

  1. Install the client with npm install apify-client.
  2. Set your Apify API token in the APIFY_TOKEN environment variable.
  3. Call the Actor kwerix/google-trends-api with the keywords and a location.
  4. List the items of the run’s dataset, one row per keyword.
import { ApifyClient } from "apify-client";

const client = new ApifyClient({ token: process.env.APIFY_TOKEN });
const run = await client.actor("kwerix/google-trends-api").call({
  keywords: ["sunscreen", "retinol"],
  geo: "US",
});
const { items } = await client.dataset(run.defaultDatasetId).listItems();
for (const row of items) console.log(row.keyword, row.seasonality?.publishBy, row.breakoutQueries.slice(0, 3));

The Google Trends API answers one HTTP call on the run-sync endpoint, which runs the Actor and returns the rows for runs under 5 minutes. maxTotalChargeUsd in the query string caps the cost of the run.

const res = await fetch(
  "https://api.apify.com/v2/acts/kwerix~google-trends-api/run-sync-get-dataset-items?maxTotalChargeUsd=0.5",
  {
    method: "POST",
    headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.APIFY_TOKEN}` },
    body: JSON.stringify({ keywords: ["sunscreen", "retinol"], geo: "US", timeRange: "today 5-y" }),
  },
);
const rows = await res.json();

Trending Now data in JavaScript takes the same call with mode set to trending and a list of countries, and returns one row per trend with its volume bucket.

const run = await client.actor("kwerix/google-trends-api").call({
  mode: "trending",
  trendingGeos: ["US", "GB"],
  trendingHours: "24",
});

The Google Trends API and the npm package google-trends-api differ in how they reach Google Trends.

Criterionnpm google-trends-apiKwerix Google Trends API
RunsIn your Node.js processAs a hosted Apify Actor
Last releaseDecember 28, 2020Maintained
429 handlingIn your codeRetries on a fresh session with proxies
OutputGoogle Trends responses as JSON textOne summarized row per keyword

Can I call the Google Trends API from the browser?

Yes. The Apify API accepts cross-origin requests with an Authorization header, so a page can call it with the visitor's own token; a shared token belongs on a server, never in front-end code.

Is the npm package google-trends-api the same thing?

No. The npm package google-trends-api is an unofficial library last published on December 28, 2020 that reads Google Trends directly from Node.js, while the Kwerix Google Trends API runs as a hosted Actor.

Sources