メインコンテンツへスキップ
X 上のすべての返信は、いずれかの会話スレッドに属します。conversation_id フィールドを使用すると、会話ツリー全体を特定・追跡・再構築できます。

動作の仕組み

誰かがポストを投稿し、他の人が返信すると、すべての返信は同じ conversation_id を共有します。これは、その会話を開始した元のポストのidです。
Original post (ID: 1234567890)  ← conversation_id for all replies
├── Reply 1 (ID: 1234567891)    → conversation_id: 1234567890
│   └── Reply to Reply 1        → conversation_id: 1234567890
└── Reply 2 (ID: 1234567892)    → conversation_id: 1234567890
    └── Reply to Reply 2        → conversation_id: 1234567890
スレッドがどれだけ深くなっても、すべての投稿は同じ conversation_id を共有します。

conversation_id の取得

tweet.fieldsconversation_id を追加します。
curl "https://api.x.com/2/tweets/1234567891?tweet.fields=conversation_id,in_reply_to_user_id,referenced_tweets" \
  -H "Authorization: Bearer $TOKEN"
レスポンス:
{
  "data": {
    "id": "1234567891",
    "text": "@user Great point!",
    "conversation_id": "1234567890",
    "in_reply_to_user_id": "2244994945",
    "referenced_tweets": [{
      "type": "replied_to",
      "id": "1234567890"
    }]
  }
}

会話全体の取得

conversation_id を検索オペレーターとして使用して、スレッド内のすべての投稿を取得します。
curl "https://api.x.com/2/tweets/search/recent?\
query=conversation_id:1234567890&\
tweet.fields=author_id,created_at,in_reply_to_user_id&\
expansions=author_id" \
  -H "Authorization: Bearer $TOKEN"
これにより元のポストへのすべての返信が、逆時系列で返されます。

使用例

会話ツリー全体を構築します:
import requests

conversation_id = "1234567890"
url = f"https://api.x.com/2/tweets/search/recent"
params = {
    "query": f"conversation_id:{conversation_id}",
    "tweet.fields": "author_id,in_reply_to_user_id,referenced_tweets,created_at",
    "max_results": 100
}

response = requests.get(url, headers=headers, params=params)
replies = response.json()["data"]

# created_at でソートして時系列順にする
replies.sort(key=lambda x: x["created_at"])

フィールド説明
conversation_idスレッドを開始した元のポストのID
in_reply_to_user_id返信対象のポストのユーザーID
referenced_tweetstype: "replied_to" と親ポストのIDを持つ配列

例:スレッド全体の取得

{
  "data": [
    {
      "id": "1234567893",
      "text": "@user2 @user1 I agree with you both!",
      "conversation_id": "1234567890",
      "author_id": "3333333333",
      "created_at": "2024-01-15T12:05:00.000Z",
      "in_reply_to_user_id": "2222222222",
      "referenced_tweets": [{"type": "replied_to", "id": "1234567892"}]
    },
    {
      "id": "1234567892",
      "text": "@user1 That's interesting!",
      "conversation_id": "1234567890",
      "author_id": "2222222222",
      "created_at": "2024-01-15T12:03:00.000Z",
      "in_reply_to_user_id": "1111111111",
      "referenced_tweets": [{"type": "replied_to", "id": "1234567890"}]
    },
    {
      "id": "1234567891",
      "text": "@user1 Great point!",
      "conversation_id": "1234567890",
      "author_id": "4444444444",
      "created_at": "2024-01-15T12:02:00.000Z",
      "in_reply_to_user_id": "1111111111",
      "referenced_tweets": [{"type": "replied_to", "id": "1234567890"}]
    }
  ],
  "meta": {
    "result_count": 3
  }
}

注意事項

  • 元の投稿の conversation_id は、その投稿自身の id と等しくなります
  • conversation_id は、投稿を返すすべての v2 エンドポイントで利用できます
  • 会話をリアルタイムで監視するために、filtered stream と組み合わせて使用します
  • 大規模なスレッドには、pagination と組み合わせて使用します

次のステップ

投稿を検索

conversation_id で検索します。

フィルタ済みストリーム

会話をリアルタイムで監視します。