Skip to main content
This guide walks you through connecting to the filtered stream to receive real-time Posts matching your filter rules.
PrerequisitesBefore you begin, you’ll need:
  • A developer account
  • Your App’s Bearer Token (found in the Developer Console under “Keys and tokens”)

Create a filter rule

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

Build a rule

Learn rule syntax and operators

Add the rule to your stream

Add your rule using the rules endpoint. Include a tag to identify which rule matched each Post:
cURL
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"}
    ]
  }'
Response:
{
  "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
    }
  }
}

Verify your rules

List all active rules to confirm your rule was added:
cURL
curl "https://api.x.com/2/tweets/search/stream/rules" \
  -H "Authorization: Bearer $BEARER_TOKEN"

Connect to the stream

Open a persistent connection to receive matching Posts:
cURL
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"

Process incoming Posts

Matching Posts stream as JSON objects:
{
  "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"
    }
  ]
}
The matching_rules array shows which rules matched the Post, using the tags you defined.

Delete rules (optional)

Remove rules by their ID:
cURL
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"]
    }
  }'

Managing your connection

The stream sends blank lines (\r\n) every 20 seconds. If you don’t receive data or a keep-alive for 20 seconds, reconnect.
Press Ctrl+C to close the connection, or close your terminal window.
Only one connection per App is allowed. Opening a new connection will close any existing one.

Next steps