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

# Python SDK

> Use the TwexAPI Python SDK for tweet search, follower exports, DMs, and X API automation in sync or async jobs.

Use the Python SDK when you want typed request objects, response models, retries, and pagination helpers for TwexAPI REST workflows in scripts, workers, and agent backends.

| Python task    | SDK call                      | Save          |
| -------------- | ----------------------------- | ------------- |
| Search tweets  | `client.search.advanced`      | `next_cursor` |
| Read profile   | `client.users.get_about`      | `screen_name` |
| List followers | `client.users.followers.list` | cursor        |

## Install

```bash theme={null}
pip install x_api_scraper
```

Until PyPI is published, install from GitHub:

```bash theme={null}
pip install git+https://github.com/twexapi-dev/x-api-scraper-python.git
```

Source: [twexapi-dev/x-api-scraper-python](https://github.com/twexapi-dev/x-api-scraper-python)

## Authenticate

```bash theme={null}
export X_API_SCRAPER_KEY="YOUR_API_KEY"
```

```python theme={null}
import os
from x_api_scraper import XApiScraper

client = XApiScraper(
    bearer_auth=os.getenv("X_API_SCRAPER_KEY"),
)
```

## Basic example

```python theme={null}
import os
from x_api_scraper import XApiScraper

client = XApiScraper(
    bearer_auth=os.getenv("X_API_SCRAPER_KEY"),
)

result = client.search.advanced(
    search_terms=["from:elonmusk"],
    sort_by="Latest",
    next_cursor="",
)

about = client.users.get_about(screen_name="elonmusk")

followers = client.users.followers.list(username="elonmusk")
```

## Workflow: search tweets to JSON Lines

```python theme={null}
import json
import os
from x_api_scraper import XApiScraper

client = XApiScraper(bearer_auth=os.getenv("X_API_SCRAPER_KEY"))
query = "from:elonmusk AI"
cursor = ""
page_index = 0

while True:
    page = client.search.advanced(
        search_terms=[query],
        sort_by="Latest",
        next_cursor=cursor,
    )

    for tweet in page.tweets or []:
        row = {
            "source": "twexapi.python.search",
            "query": query,
            "tweet_id": tweet.id,
            "text": tweet.text,
            "page_index": page_index,
            "next_cursor": page.next_cursor,
        }
        print(json.dumps(row))

    if not page.has_next_page or not page.next_cursor:
        break

    cursor = page.next_cursor
    page_index += 1
```

## Workflow: post a tweet

Write actions need a Twitter cookie or `auth_token` on the request:

```python theme={null}
tweet = client.tweets.actions.create(
    tweet_content="Hello from the TwexAPI Python SDK.",
    cookie=os.getenv("TWITTER_COOKIE"),
)
```

## Error handling

```python theme={null}
from x_api_scraper.models.errors import XAPIScraperError

try:
    client.search.advanced(
        search_terms=["from:elonmusk"],
        sort_by="Latest",
        next_cursor="",
    )
except XAPIScraperError as error:
    print(error.status_code, error.body)
```

Enable debug logging with `X_API_SCRAPER_DEBUG=true`.

## References

* [SDKs overview](/sdks)
* [Authentication](/authentication)
* [Prefect integration](/guides/prefect)
* [Haystack integration](/guides/haystack)
* [Advanced Twitter Search](/api-reference/search-endpoints/advanced-twitter-search)
* [Source Repository](https://github.com/twexapi-dev/x-api-scraper-python)
