Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions x-api/fundamentals/consuming-streaming-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,47 @@ def connect_to_stream(url, bearer_token):
print(line.decode("utf-8"))
```

### Server-Sent Events (SSE)

All streaming endpoints also support [Server-Sent Events (SSE)](https://www.w3.org/TR/2012/WD-eventsource-20120426/). SSE is a standard protocol that enables servers to push data to clients over HTTP, and is natively supported by most browsers and HTTP client libraries.

To use SSE, include the following header when establishing your connection:

```
Content-Type: text/event-stream
```

<Tabs>
<Tab title="Python">
```python
import requests

def connect_to_stream_sse(url, bearer_token):
headers = {
"Authorization": f"Bearer {bearer_token}",
"Content-Type": "text/event-stream"
}

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

for line in response.iter_lines():
if line:
print(line.decode("utf-8"))
```
</Tab>
<Tab title="cURL">
```bash
curl -X GET "https://api.x.com/2/tweets/search/stream" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-H "Content-Type: text/event-stream"
```
</Tab>
</Tabs>

<Note>
SSE support is currently available on the staging environment only. This will be rolled out to production in a future update.
</Note>

---

## Consuming data
Expand Down