> ## Documentation Index
> Fetch the complete documentation index at: https://generaltranslation.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# クイックスタート

> アクティビティストリームのサブスクリプションを設定してイベントを受信する

export const Button = ({href, children}) => {
  return <div className="not-prose group">
    <a href={href}>
      <button className="flex items-center space-x-2.5 py-1 px-4 bg-primary-dark dark:bg-white text-white dark:text-gray-950 rounded-full group-hover:opacity-[0.9] font-medium">
        <span>
          {children}
        </span>
        <svg width="3" height="24" viewBox="0 -9 3 24" class="h-6 rotate-0 overflow-visible"><path d="M0 0L3 3L0 6" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path></svg>
      </button>
    </a>
  </div>;
};

このガイドでは、リアルタイムのアカウントアクティビティイベントを受信するためのアクティビティストリームサブスクリプションの設定方法を説明します。

<Note>
  **前提条件**

  始める前に、次のものが必要です。

  * 承認済みの App を持つ[開発者アカウント](https://developer.x.com/en/portal/petition/essential/basic-info)
  * App のベアラートークン
</Note>

***

<Steps>
  <Step title="サブスクリプションを作成する" icon="bell">
    ユーザーのアクティビティイベントをサブスクライブします。

    <Tabs>
      <Tab title="cURL">
        ```bash theme={null}
        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"]
          }'
        ```
      </Tab>

      <Tab title="Python">
        ```python theme={null}
        import requests

        bearer_token = "YOUR_BEARER_TOKEN"

        url = "https://api.x.com/2/activity/subscriptions"
        headers = {
            "Authorization": f"Bearer {bearer_token}",
            "Content-Type": "application/json"
        }
        payload = {
            "user_id": "2244994945",
            "event_types": ["tweet_create_events", "favorite_events", "follow_events"]
        }

        response = requests.post(url, headers=headers, json=payload)
        print(response.json())
        ```
      </Tab>
    </Tabs>

    **レスポンス:**

    ```json theme={null}
    {
      "data": {
        "id": "1234567890",
        "user_id": "2244994945",
        "event_types": ["tweet_create_events", "favorite_events", "follow_events"],
        "created_at": "2024-01-15T10:00:00.000Z"
      }
    }
    ```
  </Step>

  <Step title="ストリームに接続する" icon="plug">
    イベントを受信するために、永続的な接続を開きます。

    <Tabs>
      <Tab title="cURL">
        ```bash theme={null}
        curl "https://api.x.com/2/activity/stream" \
          -H "Authorization: Bearer $BEARER_TOKEN"
        ```
      </Tab>

      <Tab title="Python">
        ```python theme={null}
        import requests

        bearer_token = "YOUR_BEARER_TOKEN"

        url = "https://api.x.com/2/activity/stream"
        headers = {"Authorization": f"Bearer {bearer_token}"}

        response = requests.get(url, headers=headers, stream=True)

        for line in response.iter_lines():
            if line:
                print(line.decode("utf-8"))
        ```
      </Tab>
    </Tabs>
  </Step>

  <Step title="受信イベントを処理する" icon="message">
    イベントは JSON オブジェクトとしてストリームされます。

    ```json theme={null}
    {
      "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"
        }
      ]
    }
    ```
  </Step>
</Steps>

***

<div id="available-event-types">
  ## 利用可能なイベントタイプ
</div>

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

***

<div id="manage-subscriptions">
  ## サブスクリプションを管理する
</div>

<AccordionGroup>
  <Accordion title="サブスクリプションを一覧表示する">
    すべての有効なサブスクリプションを取得します:

    ```bash theme={null}
    curl "https://api.x.com/2/activity/subscriptions" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Accordion>

  <Accordion title="サブスクリプションを更新する">
    サブスクリプションのイベントタイプを変更します:

    ```bash theme={null}
    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"]
      }'
    ```
  </Accordion>

  <Accordion title="サブスクリプションを削除する">
    サブスクリプションを削除します:

    ```bash theme={null}
    curl -X DELETE "https://api.x.com/2/activity/subscriptions/1234567890" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Accordion>
</AccordionGroup>

***

<div id="next-steps">
  ## 次のステップ
</div>

<CardGroup cols={2}>
  <Card title="Account Activity API" icon="bell" href="/ja/x-api/account-activity/introduction">
    Webhookベースの代替API
  </Card>

  <Card title="APIリファレンス" icon="code" href="/ja/x-api/activity/activity-stream">
    エンドポイントの詳細なドキュメント
  </Card>
</CardGroup>
