快速开始

1 · 检查你的密钥

第一次调用不花钱、不改变任何东西,只告诉你这把密钥能做什么、已经用了多少。如果返回 200,本文档里的其他一切都对你可用。

curl https://api.xsignalfeed.com/v1/account \
  -H "Authorization: Bearer xsf_your_key"
200 OK
{
  "name": "acme-trading",
  "plan": "pro",
  "key_id": "3f9a1c2b7d4e",
  "scopes": [
    "targets:read",
    "targets:write",
    "stream",
    "replay",
    "reads",
    "webhook",
    "keywords:read",
    "keywords:write"
  ],
  "event_types": [],
  "event_types_all": true,
  "limits": {
    "max_targets": 200,
    "max_conns": 5,
    "max_keywords": 20,
    "requests_per_minute": 600
  },
  "usage": {
    "targets": 12,
    "keywords": 3
  }
}

2 · 监控一个账号

用用户名指定账号,不带 @。如果我们从未见过它,我们会到 X 上解析它并激活投递——整个过程不到一分钟,不需要你做任何事。响应会告诉你发生的是哪种情况。

请求
curl -X POST https://api.xsignalfeed.com/v1/monitor/targets \
  -H "Authorization: Bearer xsf_your_key" -H "Content-Type: application/json" \
  -d '{"screen_name": "elonmusk", "event_types": ["followers.verified.add"]}'
201 · 202
{
  "status": "subscribed",
  "state": "subscribed",
  "id": "44196397",
  "screen_name": "elonmusk",
  "name": "Elon Musk",
  "event_types": [
    "followers.verified.add"
  ],
  "event_types_all": false
}
{
  "status": "queued",
  "state": "queued",
  "screen_name": "newaccount",
  "user_id": null,
  "event_types": [],
  "event_types_all": true,
  "note": "target will be resolved and placed shortly; poll GET /v1/monitor/targets/{screen_name}"
}

我们已知的账号会立即订阅(201;若你已订阅则为 200)。从未见过的账号返回 202:轮询 GET /v1/monitor/targets/{target} 直到 state 变为 subscribed;或者会得到 404,其 details.state 为 failed,details.reason 说明原因:user_not_found、user_suspended 或 user_protected。如果 X 在最近十分钟内已告诉我们这一点,POST 本身就会直接返回这个 404。默认会送达密钥允许的每一种事件类型;传 event_types 就只收其中几种,例如只要 followers.verified.add(设置方法)。要一次添加很多?用批量接口。

3 · 打开流

连接之后,事件以单行 JSON 到达。不需要其他任何操作——没有订阅消息,也没有频道。

import asyncio, json, websockets

async def main():
    url = "wss://ws.xsignalfeed.com/v1/stream?api_key=xsf_your_key"
    async with websockets.connect(url) as ws:
        async for raw in ws:
            ev = json.loads(raw)
            if ev["t"].startswith("system."): continue
            print(ev["t"], ev["target"]["screen_name"], ev["d"].get("text", "")[:60])

asyncio.run(main())

4 · 读取你的第一个事件

第一帧始终是 system.hello。之后就是真正的事件:

一条新帖子,在流上到达时的样子
{
  "v": 1,
  "t": "tweet.create",
  "op": "create",
  "id": "tweet:2097879002992070835",
  "ts": 1789012345678,
  "sid": "1789012346012-0",
  "target": {
    "id": "1652541",
    "screen_name": "Reuters",
    "name": "Reuters"
  },
  "d": {
    "tweet_id": "2097879002992070835",
    "kind": "tweet",
    "text": "Ash-covered shoes and a president in crisis…",
    "lang": "en",
    "author": {
      "id": "1652541",
      "screen_name": "Reuters",
      "name": "Reuters",
      "bio": "Top and breaking news, pictures and videos from Reuters.",
      "avatar": "https://pbs.twimg.com/profile_images/1874154135869616128/nJDmubGJ_normal.jpg",
      "banner": "https://pbs.twimg.com/profile_banners/1652541/1573687397",
      "followers": 25900000,
      "following": 1150,
      "tweets": 1080000,
      "location": "Around the world",
      "url": "https://www.reuters.com",
      "verified": true,
      "verified_type": "Business",
      "protected": false,
      "created_at": "2007-03-20T17:46:05Z",
      "category": null
    },
    "reply_to": null,
    "retweet_of": null,
    "quoted": null,
    "media": [
      "https://pbs.twimg.com/media/….jpg"
    ],
    "media_items": [
      {
        "type": "photo",
        "url": "https://pbs.twimg.com/media/….jpg",
        "expanded_url": "https://x.com/Reuters/status/2097879002992070835/photo/1",
        "width": 1200,
        "height": 675
      }
    ],
    "urls": [
      "https://reut.rs/…"
    ],
    "counts": {
      "reply": 12,
      "rt": 40,
      "like": 131,
      "quote": 3,
      "bookmark": 9
    },
    "views": 20411,
    "url": "https://x.com/Reuters/status/2097879002992070835",
    "full": true,
    "kind_final": true
  }
}