# X (Twitter) 关键词搜索 API (twitter.search)

- Canonical page: https://everyinfra.com/twitter-api/search
- Endpoint: POST https://api.everyinfra.com/api/v1/social with {"platform": "twitter", "action": "search", "params": {...}}
- Price: $0.000792 per call ($0.79 per 1,000 requests); failed calls and empty results are not charged
- Returns: list, default 25, max 200 per call
- Mode: sync; add "mode": "async" for slow tasks

关键词搜索按 `keyword` 检索公开帖子。`keyword` 是 X 的帖子搜索表达式，可以直接写 from:、since: 这类平台搜索语法。每条返回正文、浏览与互动数、作者资料和 `media`。它回答「某个话题或词现在大家怎么说」，适合做舆情抽样、监测品牌词，或者找出讨论度高的帖子。`reply_to_id` 标出一条帖子回复的是哪条，`conversation_id` 把同一串对话归在一起。

要偏向新帖就给 `sort` 传 latest，不传时按 X 默认的热门排序。不传 `limit` 时读 25 条，单次最多 200 条。每条都带 `author_username` 和 `author_follower_count`，能顺带看出是谁在讨论、账号多大。

## Health

- Live health (24h percent, hourly and daily trends): https://api.everyinfra.com/api/v1/social/availability?platform=twitter&action=search

## Parameters

| name | type | required | default | description |
|---|---|---|---|---|
| keyword | not declared | yes | not declared | search 的 X 帖子搜索表达式，可包含平台搜索语法（如 from:、since:）；排序见 sort。 |
| sort | not declared | no | not declared | 排序偏好，latest 表示希望最新的帖子排在前面；X 默认按热门（Top）返回，传 latest 也不保证严格按时间排列。 |
| limit | integer (1–200) | no | 25 | 这次调用最多返回的条数，整数，1–200；不传按默认值，实际返回可能少于这个数。超过上限会在扣费前返回 422。 |

## Response fields (items in `results`)

| field | meaning |
|---|---|
| id | 该记录在原平台上的唯一标识。仅在同一平台内唯一，跨平台可能重复。 |
| url | 该记录在原平台上的可访问链接。 |
| text | 正文内容，已去除 HTML 标签，全文返回、不截断（个别平台的列表结果本身只有摘要，以该平台的字段说明为准）。 |
| like_count | 点赞数。null 表示该平台或该接口不公开此数据，不等于 0。 |
| retweet_count | 转发数。 |
| reply_count | 回复数，通常用于评论的子回复。null 表示不公开。 |
| view_count | 浏览/播放数。null 表示不公开，不等于 0。 |
| posted_at | 发布时间，ISO 8601 格式、UTC 时区（如 2026-08-07T12:34:56+00:00）。平台只给非标准字符串时原样透传，解析前建议做容错。null 表示平台未公开。 |
| author_username | 作者用户名（@handle），通常可用于拼 URL。 |
| author_name | 作者昵称（显示名）。 |
| author_url | 作者在原平台的主页链接。 |
| author_follower_count | 作者粉丝数。null 表示不公开。 |
| quote_count | platform-native meaning |
| bookmark_count | 书签（收藏）数。 |
| language | 内容语言代码（如 zh、en），由平台判定，可能不准。 |
| conversation_id | 所属对话串 ID（根推文 ID），同一串的回复据此归在一起。 |
| is_reply | platform-native meaning |
| is_quote | 是否引用推文。 |
| is_repost | platform-native meaning |
| is_pinned | platform-native meaning |
| reply_to_id | platform-native meaning |
| reply_to_user_id | 所回复用户的数字 ID。 |
| reply_to | platform-native meaning |
| source | 发推客户端（如 Twitter for iPhone）；平台没给时没有这个键。 |
| media | 推文图片 / 视频：type（photo / video / animated_gif）、url（图片或视频封面）、width、height、duration_sec（视频秒数）、video_url（码率最高的 mp4）。 |
| hashtags | 正文里的话题标签数组，按出现顺序；没有时不出现这个字段。 |
| mentions | 正文里 @ 到的用户名。 |
| links | 正文里的外链（展开后的完整网址）。 |
| author_id | 作者在原平台的唯一 ID。 |
| author_avatar_url | 作者头像图片链接（多数平台带签名、会过期）。 |
| author_bio | platform-native meaning |
| author_is_verified | platform-native meaning |
| author_following_count | platform-native meaning |
| author_post_count | 作者推文总数。 |
| author_media_count | 作者发过的媒体数。 |
| author_created_at | platform-native meaning |
| author_banner_url | 作者主页横幅图链接。 |
| platform | 数据来源平台标识，与请求里的 platform 一致（如 xiaohongshu、tiktok）。 |

## Example

```bash
curl -X POST 'https://api.everyinfra.com/api/v1/social' \
  -H "Authorization: Bearer $EVERYINFRA_API_KEY" \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "platform": "twitter",
  "action": "search",
  "params": {
    "keyword": "open source"
  }
}'
```

```python
import os
import requests

response = requests.post(
    "https://api.everyinfra.com/api/v1/social",
    headers={"Authorization": f"Bearer {os.environ['EVERYINFRA_API_KEY']}"},
    json={
        "platform": "twitter",
        "action": "search",
        "params": {
            "keyword": "open source",
        },
    },
    timeout=120,
)
print(response.status_code)
print(response.json())
```

## Response example

Real response to the example request above, captured 2026-09-26T08:56:04Z. `results` and `count` are real with personal information (names, handles, avatars, profile links, contact details) removed; the list keeps the first 2 of 39. `id`, `billing` and `quota` differ on every call, so only their shape is shown.

```text
{
  "id": "req_…",
  "platform": "twitter",
  "action": "search",
  "results": [
    {
      "id": "•••",
      "url": "https://x.com/steipete/status/2103491173927272531",
      "text": "Microsoft shipped a really compelling product on top of @••• today. We worked with them since March to make the codebase ready for large-scale deployments, they are a great partner and open-source contributor. 🙏🦞",
      "like_count": 2605,
      "retweet_count": 217,
      "reply_count": 131,
      "view_count": 383868,
      "posted_at": "Fri Sep 25 14:25:51 +0000 2026",
      "author_username": "•••",
      "author_name": "•••",
      "author_url": "https://x.com/•••",
      "author_follower_count": 592294,
      "platform": "twitter"
    },
    {
      "id": "•••",
      "url": "https://x.com/muellerberndt/status/2103069347724116201",
      "text": "I put a Cadence patchnet into a virtual fruit fly. This creates an actual fly matrix in your browser that the fly experiences as a solid world (observation is substrate-independent). All open-source, Github link on the page.\n\nhttps://t.co/vImD21Si1x https://t.co/WAY3aIwq4v",
      "like_count": 3837,
      "retweet_count": 270,
      "reply_count": 80,
      "view_count": 2622193,
      "posted_at": "Thu Sep 24 10:29:40 +0000 2026",
      "author_username": "•••",
      "author_name": "•••",
      "author_url": "https://x.com/•••",
      "author_follower_count": 28591,
      "platform": "twitter"
    }
  ],
  "count": 39,
  "billing": {
    "charged": …,
    "charged_usd": …,
    "balance_after_usd": …,
    …
  },
  "quota": { … }
}
```

## FAQ

### `sort` 传 latest 就能保证按时间排序吗？

不能。latest 只表示希望最新的帖子排在前面；不传 `sort` 时 X 默认按热门（Top）返回，传了 latest 也不保证严格按时间排列。需要严格的时间顺序时，拿到结果后自己按 `posted_at` 排。`posted_at` 是 UTC 时间。

### 想只搜某个账号发的帖子，怎么办？

在 `keyword` 里用 from: 这类搜索语法限定账号就行，还能和关键词、since: 组合。如果只是要一个账号的帖子，直接用用户内容那一页，传用户名，不用拼表达式；关键词搜索更适合跨账号、按词找。

### 搜到 200 条就等于拿到了这个词的全部帖子吗？

不等于。200 条只是单次返回的上限，检索范围是公开帖子，结果数量不代表这个词的全部历史。做声量统计时，要把它当作样本，并记下查询词、`sort` 和读取时间，方便日后对照。汇总互动数时，`like_count`、`view_count` 的空值单独记，别当 0 加进去。

## Also

- Capability catalog (no key): https://api.everyinfra.com/api/v1/social/catalog?platform=twitter
- OpenAPI: https://everyinfra.com/openapi.json · Agents guide: https://everyinfra.com/agents.md
