メインコンテンツへスキップ
このガイドでは、リアルタイムのアカウントアクティビティイベントを受信するためのアクティビティストリームサブスクリプションの設定方法を説明します。
前提条件始める前に、次のものが必要です。

サブスクリプションを作成する

ユーザーのアクティビティイベントをサブスクライブします。
curl -X POST "https://api.x.com/2/activity/subscriptions" \
  -H "Authorization: Bearer $BEARER_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "user_id": "2244994945",
    "event_types": ["tweet_create_events", "favorite_events", "follow_events"]
  }'
レスポンス:
{
  "data": {
    "id": "1234567890",
    "user_id": "2244994945",
    "event_types": ["tweet_create_events", "favorite_events", "follow_events"],
    "created_at": "2024-01-15T10:00:00.000Z"
  }
}

ストリームに接続する

イベントを受信するために、永続的な接続を開きます。
curl "https://api.x.com/2/activity/stream" \
  -H "Authorization: Bearer $BEARER_TOKEN"

受信イベントを処理する

イベントは JSON オブジェクトとしてストリームされます。
{
  "for_user_id": "2244994945",
  "event_type": "tweet_create_events",
  "created_at": "2024-01-15T10:30:00.000Z",
  "tweet_create_events": [
    {
      "id": "1234567890",
      "text": "Hello from the stream!",
      "author_id": "2244994945"
    }
  ]
}

利用可能なイベントタイプ

イベント説明
tweet_create_eventsユーザーが新しいポストを投稿する
favorite_eventsユーザーがポストにいいねする
follow_eventsユーザーがほかのアカウントをフォローする、またはフォローされる
direct_message_eventsユーザーがDMを送信または受信する
block_eventsユーザーがブロックまたはブロック解除する
mute_eventsユーザーがミュートまたはミュート解除する

サブスクリプションを管理する

すべての有効なサブスクリプションを取得します:
curl "https://api.x.com/2/activity/subscriptions" \
  -H "Authorization: Bearer $BEARER_TOKEN"
サブスクリプションのイベントタイプを変更します:
curl -X PUT "https://api.x.com/2/activity/subscriptions/1234567890" \
  -H "Authorization: Bearer $BEARER_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "event_types": ["tweet_create_events", "favorite_events"]
  }'
サブスクリプションを削除します:
curl -X DELETE "https://api.x.com/2/activity/subscriptions/1234567890" \
  -H "Authorization: Bearer $BEARER_TOKEN"

次のステップ

Account Activity API

Webhookベースの代替API

APIリファレンス

エンドポイントの詳細なドキュメント