Skip to content

Repository files navigation

trendsapi (Python)

Official Python client for Trends API. Three methods. Decoded payloads. You never parse the HTTP body string.

HTTP contract and field tables: trendsapi-ai/trendsapi.

License: MITPyPIPython

Authentication

pip install trendsapi
export TRENDSAPI_KEY=your_key

Key: trendsapi.ai/#get-key. Python 3.9+.

fromtrendsapiimportTrendsAPIclient=TrendsAPI() # TRENDSAPI_KEY# client = TrendsAPI(api_key="YOUR_KEY")

Methods

MethodREST modeRequired argumentsReturns
get_time_seriesget_time_seriessource, keywordlist[TrendsDataPoint]
get_growthget_growthsource, keywordGetGrowthResponse
get_top_trendsget_top_trendstypeGetTopTrendsResponse
weekly=client.get_time_series(source="google search", keyword="solar battery")
growth=client.get_growth(source="amazon", keyword="solar battery", percent_growth=["3M", "12M"])
now=client.get_top_trends(type="Google Trends", limit=10)

source is lowercase (google search). type is exact (Google Trends). Mixing them is a 400.

get_time_series

points=client.get_time_series(source="google search", keyword="bitcoin")
print(points[-1].date, points[-1].value)

Python returns dataclasses. Use .date / .value, not ["date"].

Each point:

FieldAlwaysMeaning
dateyesYYYY-MM-DD
valueyes0-100 index for this series
keywordyesEcho
volumenoAbsolute volume when available
source or datatypenoPipeline label

get_growth

g=client.get_growth(source="google search", keyword="nike", percent_growth=["12M", "3M", "YTD"])
print(g.results[0].growth, g.results[0].direction)

percent_growth default: ["12M"]. Presets: 7D14D30D1M2M3M6M9M12M/1Y18M24M/2Y36M/3Y48M60M/5YMTDQTDYTD. Custom: {"name": "Launch", "recent": "2024-06-01", "baseline": "2024-01-01"}.

FieldMeaning
search_termKeyword
data_sourceSource
resultsOne object per window (period, growth, direction, dates, values)
metadataCounts / success flag

Several windows still count as one request.

get_top_trends

chart=client.get_top_trends(type="TikTok Trending Hashtags", limit=10)
# chart.data == [[1, "matcha"], ...]
FieldMeaning
as_of_tsSnapshot time
typeFeed name
limit, offset, countPagination
data[rank, label] rows

Optional offset=, category= (Amazon Best Sellers by Category, Top Websites only).

Keyword sources

Pass as source=. Full notes: hub README.

sourcekeyword
google search, google images, google news, google shoppingAny phrase
youtubeAny phrase
tiktokHashtag or topic
redditSubreddit, no r/
amazonProduct phrase
wikipediaArticle title
news volume, news sentimentAny phrase
app downloads, app rankingsAndroid bundle ID (com.openai.chatgpt)
npmExact package name
steamGame display name

Live feeds

Pass as type= on get_top_trends. Exact strings: Google Trends, Google News Top News, TikTok Trending Hashtags, TikTok Trending Searches, TikTok Shop Hot Products, YouTube Trending, X (Twitter) Trending, Reddit Hot Posts, Reddit World News, Wikipedia Trending, Amazon Best Sellers Top Rated, Amazon Best Sellers by Category, App Store Top Free, App Store Top Paid, Google Play, Top Websites, Spotify Top Podcasts, Steam Most Played, GitHub Trending Repos, IMDb MOVIEmeter, Open Library Trending Books.

Async

importasynciofromtrendsapiimportAsyncTrendsAPIasyncdefcompare(term: str):
c=AsyncTrendsAPI()
returnawaitasyncio.gather(
c.get_time_series(source="google search", keyword=term),
c.get_time_series(source="google shopping", keyword=term),
c.get_time_series(source="wikipedia", keyword=term),
)
asyncio.run(compare("solar battery"))

Each 200 is one billed request.

Pandas

fromdataclassesimportasdictimportpandasaspdfromtrendsapiimportTrendsAPIdf=pd.DataFrame(asdict(p) forpinTrendsAPI().get_time_series(source="google search", keyword="solar battery"))
df["date"] =pd.to_datetime(df["date"])
print(df.set_index("date")["value"].resample("ME").mean().tail())

Errors

CodeClient
200Returns parsed payload
400Raises. Fix source / type
401Raises. Check TRENDSAPI_KEY
404Raises. No series. Do not retry
429Raises. Quota
5xxRetries, then raises

Raw requests (second parse required): see hub, Raw HTTP.

License

MIT. See LICENSE.