mcpField
N
npm Trends API

npm Trends API

远程服务
trendsapi.ai开发工具010 周前更新

关于 npm Trends API

npm Trends API 为精确的 npm 包名提供标准化下载关注度历史和增长数据。它面向分析包采用情况的开发者,提供 Python 客户端和 HTTP API。

主要功能

  1. 01标准化的 0–100 时间序列
  2. 02按预设或自定义周期计算增长
  3. 03获取热门趋势
  4. 04同步和异步 Python 客户端
  5. 05通过 HTTP API 调用

适用场景

  1. 01比较 npm 包关注度随时间的变化
  2. 02衡量不同周期内的包增长
  3. 03整理趋势数据供 Pandas 分析

配置

远程服务无需本地安装。选择你使用的客户端,复制配置或使用一键安装。

服务地址https://npm.api.trendsapi.ai/mcp
一键复制安装命令
Claude CodeTerminal
Cursor.cursor/mcp.json
VS Code.vscode/mcp.json
Claude Desktopclaude_desktop_config.json
Windsurf~/.codeium/windsurf/mcp_config.json

命令在终端中运行;JSON 粘贴到对应客户端的 MCP 配置后重启。需要授权的服务会在首次连接时提示登录。

全部服务地址
Streamable HTTPhttps://npm.api.trendsapi.ai/mcp需要授权

工具

详细介绍

npm 包下载趋势 API

通过 Trends API 查看 npm 下载热度。提供精确包名的标准化 0-100 历史数据和增长情况。

License: MIT PyPI Python

密钥:trendsapi.ai/#get-key。HTTP 接口规范和所有来源:trendsapi-ai/trendsapi。

身份验证

pip install trendsapi-npm
export TRENDSAPI_KEY=your_key

Python 3.9+。与 HTTP API 使用相同的密钥。

from trendsapi_npm import TrendsAPI

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

关键字辅助方法默认使用 source: "npm"。传入 source= 即可通过同一客户端访问其他平台。官方完整客户端(支持所有来源,无预设):trendsapi。

方法

方法REST mode返回值
get_time_series(keyword, source=, data_mode=)get_time_serieslist[TrendsDataPoint]
get_growth(keyword, percent_growth=, source=, data_mode=)get_growthGetGrowthResponse
get_top_trends(type=, ...)get_top_trendsGetTopTrendsResponse

source 使用小写(npm)。type 需完全匹配(Google Trends)。混用会返回 400 错误。

from trendsapi_npm import TrendsAPI

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

series = client.get_time_series("vite")
print(series[-1].date, series[-1].value)

growth = client.get_growth("vite", percent_growth=["3M", "12M"])
print(growth.results[0].growth, growth.results[0].direction)

get_time_series

points = client.get_time_series("vite")

每个数据点:

字段始终提供含义
date是YYYY-MM-DD
value是此序列的 0-100 指数
keyword是回显值
volume否可用时提供绝对量
source 或 datatype否数据管道标签

Python 返回 list[TrendsDataPoint]。使用 .date 和 .value,而不是 ["date"]。 JS 返回相同字段,作为对象属性提供。

get_growth

g = client.get_growth("vite", percent_growth=["12M", "3M", "YTD"])
print(g.results[0].growth, g.results[0].direction)

percent_growth 默认值:["12M"]。预设值:7D 14D 30D 1M 2M 3M 6M 9M 12M/1Y 18M 24M/2Y 36M/3Y 48M 60M/5Y MTD QTD YTD。自定义值:{"name": "Launch", "recent": "2024-06-01", "baseline": "2024-01-01"}。

字段含义
search_term关键字
data_source来源
results每个时间窗口对应一个对象(period、growth、direction、日期、值)
metadata计数 / 成功标志

多个时间窗口仍计为一次请求。Python:growth.results[0].growth。JS:growth.results[0].growth。

异步

import asyncio
from trendsapi_npm import AsyncTrendsAPI

async def main():
    c = AsyncTrendsAPI()
    return await asyncio.gather(
        c.get_time_series("vite"),
        c.get_time_series("vite", source="google search"),
    )

asyncio.run(main())

每个 200 响应均计为一次计费请求。

Pandas

from dataclasses import asdict
import pandas as pd
from trendsapi_npm import TrendsAPI

df = pd.DataFrame(asdict(p) for p in TrendsAPI().get_time_series("vite"))
df["date"] = pd.to_datetime(df["date"])
print(df.set_index("date")["value"].resample("ME").mean().tail())

调用(curl)

字段值
端点POST https://api.trendsapi.ai/api
身份验证Authorization: Bearer $TRENDSAPI_KEY
历史数据使用 get_time_series 或 get_growth,并设置 source: npm
关键字精确包名,区分大小写:vite、@tanstack/react-query
实时 type不适用
curl -sS -X POST https://api.trendsapi.ai/api \
  -H "Authorization: Bearer $TRENDSAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{"mode":"get_time_series","source":"npm","keyword":"vite"}'

来源说明

  • JSON 中的 @ 或 / 不要进行 URL 编码。
  • 每次请求仅查询一个包。
  • 如需精确整数计数,请使用 api.npmjs.org/downloads。

错误

HTTP客户端
200已解析的载荷。Python 数据类 / JS 类型化对象
400抛出错误。修正 source 或 type 的拼写
401抛出错误。检查 TRENDSAPI_KEY
404抛出错误。该关键字没有序列。不要重试
429抛出错误。配额不足
5xx客户端重试后抛出错误

HTTP 的 body 字段是 JSON 字符串。SDK 会对其解码。使用原始 curl 时,必须再次解析 body。

网站:https://trendsapi.ai/trends/npm-trends。

许可证

MIT。参见 LICENSE。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。