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

# Quickstart

> This guide walks you through connecting to the filtered stream to receive near real-time. Reference for the X API v2 standard tier covering filtered stream.

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>;
};

This guide walks you through connecting to the filtered stream to receive near real-time Posts matching your filter rules.

<Note>
  **Prerequisites**

  Before you begin, you'll need:

  * A [developer account](https://developer.x.com/en/portal/petition/essential/basic-info)
  * Your App's Bearer Token (found in the Developer Console under "Keys and tokens")
</Note>

***

<Steps>
  <Step title="Create a filter rule" icon="filter">
    Rules define which Posts to receive. Use operators to match on keywords, hashtags, users, and more.

    **Example rule:** Match Posts containing "cat" with images:

    ```
    cat has:images
    ```

    <Card title="Build a rule" icon="filter" href="/x-api/posts/filtered-stream/integrate/build-a-rule">
      Learn rule syntax and operators
    </Card>
  </Step>

  <Step title="Add the rule to your stream" icon="plus">
    Add your rule using the rules endpoint. Include a `tag` to identify which rule matched each Post:

    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl -X POST "https://api.x.com/2/tweets/search/stream/rules" \
        -H "Authorization: Bearer $BEARER_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
          "add": [
            {"value": "cat has:images", "tag": "cats with images"}
          ]
        }'
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_BEARER_TOKEN")

      # Add a rule to the filtered stream
      response = client.filtered_stream.add_rules(
          add=[{"value": "cat has:images", "tag": "cats with images"}]
      )

      for rule in response.data:
          print(f"Rule added: {rule.id} - {rule.value}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

      // Add a rule to the filtered stream
      const response = await client.filteredStream.addRules({
        add: [{ value: "cat has:images", tag: "cats with images" }],
      });

      response.data?.forEach((rule) => {
        console.log(`Rule added: ${rule.id} - ${rule.value}`);
      });
      ```
    </CodeGroup>

    **Response:**

    ```json theme={null}
    {
      "data": [
        {
          "id": "1273026480692322304",
          "value": "cat has:images",
          "tag": "cats with images"
        }
      ],
      "meta": {
        "sent": "2024-01-15T10:30:00.000Z",
        "summary": {
          "created": 1,
          "not_created": 0,
          "valid": 1,
          "invalid": 0
        }
      }
    }
    ```
  </Step>

  <Step title="Verify your rules" icon="check">
    List all active rules to confirm your rule was added:

    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl "https://api.x.com/2/tweets/search/stream/rules" \
        -H "Authorization: Bearer $BEARER_TOKEN"
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_BEARER_TOKEN")

      # Get all active rules
      response = client.filtered_stream.get_rules()

      for rule in response.data:
          print(f"Active rule: {rule.id} - {rule.value}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

      // Get all active rules
      const response = await client.filteredStream.getRules();

      response.data?.forEach((rule) => {
        console.log(`Active rule: ${rule.id} - ${rule.value}`);
      });
      ```
    </CodeGroup>
  </Step>

  <Step title="Connect to the stream" icon="plug">
    Open a persistent connection to receive matching Posts:

    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl "https://api.x.com/2/tweets/search/stream?\
      tweet.fields=created_at,author_id&\
      expansions=author_id&\
      user.fields=username" \
        -H "Authorization: Bearer $BEARER_TOKEN"
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_BEARER_TOKEN")

      # Connect to the filtered stream
      for post in client.filtered_stream.stream(
          tweet_fields=["created_at", "author_id"],
          expansions=["author_id"],
          user_fields=["username"]
      ):
          print(f"New post: {post.data.text}")
          print(f"Matching rules: {post.matching_rules}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

      // Connect to the filtered stream
      const stream = client.filteredStream.stream({
        tweetFields: ["created_at", "author_id"],
        expansions: ["author_id"],
        userFields: ["username"],
      });

      for await (const post of stream) {
        console.log(`New post: ${post.data?.text}`);
        console.log(`Matching rules: ${post.matching_rules}`);
      }
      ```
    </CodeGroup>
  </Step>

  <Step title="Process incoming Posts" icon="message">
    Matching Posts stream as JSON objects:

    ```json theme={null}
    {
      "data": {
        "id": "1234567890",
        "text": "Look at this cute cat! 🐱",
        "author_id": "9876543210",
        "created_at": "2024-01-15T10:35:00.000Z",
        "edit_history_tweet_ids": ["1234567890"]
      },
      "includes": {
        "users": [
          {
            "id": "9876543210",
            "username": "catperson"
          }
        ]
      },
      "matching_rules": [
        {
          "id": "1273026480692322304",
          "tag": "cats with images"
        }
      ]
    }
    ```

    <Tip>
      The `matching_rules` array shows which rules matched the Post, using the tags you defined.
    </Tip>
  </Step>

  <Step title="Delete rules (optional)" icon="trash">
    Remove rules by their ID:

    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl -X POST "https://api.x.com/2/tweets/search/stream/rules" \
        -H "Authorization: Bearer $BEARER_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
          "delete": {
            "ids": ["1273026480692322304"]
          }
        }'
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_BEARER_TOKEN")

      # Delete rules by ID
      response = client.filtered_stream.delete_rules(
          delete={"ids": ["1273026480692322304"]}
      )
      print(f"Deleted: {response.meta.summary.deleted}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

      // Delete rules by ID
      const response = await client.filteredStream.deleteRules({
        delete: { ids: ["1273026480692322304"] },
      });
      console.log(`Deleted: ${response.meta?.summary?.deleted}`);
      ```
    </CodeGroup>
  </Step>
</Steps>

***

## Managing your connection

<AccordionGroup>
  <Accordion title="Keep-alive signals">
    The stream sends blank lines (`\r\n`) every 20 seconds. If you don't receive data or a keep-alive for 20 seconds, reconnect.
  </Accordion>

  <Accordion title="Disconnecting">
    Press `Ctrl+C` to close the connection, or close your terminal window.
  </Accordion>

  <Accordion title="Connection limits">
    Only one connection per App is allowed. Opening a new connection will close any existing one.
  </Accordion>
</AccordionGroup>

***

## Next steps

<CardGroup cols={2}>
  <Card title="Build a rule" icon="filter" href="/x-api/posts/filtered-stream/integrate/build-a-rule">
    Learn rule syntax
  </Card>

  <Card title="Operator reference" icon="list-check" href="/x-api/posts/filtered-stream/integrate/operators">
    All available operators
  </Card>

  <Card title="Handling disconnections" icon="plug" href="/x-api/fundamentals/handling-disconnections">
    Reconnect gracefully
  </Card>

  <Card title="API Reference" icon="code" href="/x-api/posts/filtered-stream">
    Full endpoint documentation
  </Card>
</CardGroup>
