| title | Python XDK | |||||||
|---|---|---|---|---|---|---|---|---|
| sidebarTitle | Python | |||||||
| description | Install and use the official Python XDK client library for the X API v2, including authentication setup, paginated requests, and streaming examples. | |||||||
| keywords |
|
The Python XDK is the official client library for the X API v2. It handles authentication, pagination, and streaming so you can focus on building.
Source code, issues, and releases.pip install xdkRequires Python 3.8+.
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# Search for posts
for page in client.posts.search_recent(query="X API", max_results=10):
if page.data and len(page.data) > 0:
print(page.data[0].text)
break| Feature | Description |
|---|---|
| OAuth support | Bearer Token, OAuth 2.0 with PKCE, and OAuth 1.0a |
| Automatic pagination | Iterate through results without manual next_token handling |
| Streaming | Real-time data via persistent connections (filtered stream, etc.) |
| Full API coverage | All X API v2 endpoints — search, timelines, filtered stream, and more |
```python from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
</Tab>
<Tab title="OAuth 2.0">
```python
from xdk import Client
from xdk.oauth2_auth import OAuth2PKCEAuth
auth = OAuth2PKCEAuth(
client_id="YOUR_CLIENT_ID",
redirect_uri="YOUR_CALLBACK_URL",
scope="tweet.read users.read offline.access"
)
auth_url = auth.get_authorization_url()
tokens = auth.fetch_token(authorization_response=callback_url)
client = Client(bearer_token=tokens["access_token"])
oauth1 = OAuth1( api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET", access_token="YOUR_ACCESS_TOKEN", access_token_secret="YOUR_ACCESS_TOKEN_SECRET" )
client = Client(auth=oauth1)
</Tab>
</Tabs>
---
## Common methods
| Category | Method |
|:---------|:-------|
| **Posts** | `client.posts.search_recent()` |
| **Users** | `client.users.get_me()` |
| **Spaces** | `client.spaces.get()` |
| **Lists** | `client.lists.get()` |
| **DMs** | `client.direct_messages.get()` |
---
## Learn more
<CardGroup cols={2}>
<Card title="Installation" icon="download" href="/xdks/python/install">
Development install, prerequisites, and verification.
</Card>
<Card title="Quickstart" icon="rocket" href="/xdks/python/quickstart">
Step-by-step first request walkthrough.
</Card>
<Card title="Authentication" icon="key" href="/xdks/python/authentication">
Detailed guide for all auth methods.
</Card>
<Card title="Pagination" icon="arrows-left-right" href="/xdks/python/pagination">
Automatic pagination and iterators.
</Card>
<Card title="Streaming" icon="satellite-dish" href="/xdks/python/streaming">
Real-time data via filtered stream.
</Card>
<Card title="API Reference" icon="book" href="/xdks/python/reference/modules">
Complete client and model reference.
</Card>
</CardGroup>
For code examples, see the [samples repo](https://github.com/xdevplatform/samples/tree/main/python).