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

How to call the Google Trends API from Node.js
There are 4 steps to call the Google Trends API from Node.js.
- Install the client with
npm install apify-client. - Set your Apify API token in the
APIFY_TOKENenvironment variable. - Call the Actor
kwerix/google-trends-apiwith the keywords and a location. - 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));How to call the Google Trends API with plain HTTP
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();How to get Trending Now data in JavaScript
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",
});Google Trends API vs the npm package google-trends-api
The Google Trends API and the npm package google-trends-api differ in how they reach Google Trends.
| Criterion | npm google-trends-api | Kwerix Google Trends API |
|---|---|---|
| Runs | In your Node.js process | As a hosted Apify Actor |
| Last release | December 28, 2020 | Maintained |
| 429 handling | In your code | Retries on a fresh session with proxies |
| Output | Google Trends responses as JSON text | One summarized row per keyword |
- The Google Trends data API answers Node.js, plain HTTP and browser calls with the same rows.
Google Trends API in JavaScript FAQ
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.