X API Python SDK (xdk)
raw JSON → 0.9.0 verified Fri May 01 auth: no python
The official Python SDK for the X (formerly Twitter) API, currently at v0.9.0. Provides asynchronous and synchronous clients, OAuth 2.0 support, and wrappers for endpoints like tweets, users, media, and direct messages. Active development with monthly releases.
pip install xdk Common errors
error ImportError: cannot import name 'X' from 'xdk' ↓
cause The class was renamed from `X` to `XClient` in v0.5.0.
fix
Change import to
from xdk import XClient. error TypeError: __init__() got an unexpected keyword argument 'bearer_token' ↓
cause Prior to v0.8.0, you could pass `bearer_token` directly to the client constructor. This was removed.
fix
Use
from xdk.auth import OAuth2Bearer and pass auth=OAuth2Bearer(token=...). error AttributeError: module 'xdk' has no attribute 'client' ↓
cause The internal module structure changed; direct imports from `xdk.client` are no longer supported.
fix
Use top-level import
from xdk import XClient. Warnings
breaking In v0.5.0, the client class was renamed from `X` to `XClient`. Old code using `from xdk import X` will break. ↓
fix Use `from xdk import XClient` instead.
breaking In v0.8.0, the `auth` parameter changed from accepting a tuple to requiring an Auth object. Passing `(bearer_token,)` no longer works. ↓
fix Create an `OAuth2Bearer` object and pass it as `auth=OAuth2Bearer(token=...)`.
deprecated The `requests`-based fallback HTTP client is deprecated and will be removed in v1.0.0. Use `httpx` (the default). ↓
fix Ensure `httpx` is installed and do not force `use_requests=True`.
gotcha Async client methods must be awaited. Failing to `await` a coroutine will yield a warning or error. ↓
fix Always use `await client.some_method()` when using `AsyncXClient`.
Imports
- XClient wrong
from xdk.client import XClientcorrectfrom xdk import XClient - AsyncXClient
from xdk import AsyncXClient - OAuth2Bearer wrong
from xdk import OAuth2Bearercorrectfrom xdk.auth import OAuth2Bearer
Quickstart
import os
from xdk import XClient
from xdk.auth import OAuth2Bearer
bearer_token = os.environ.get('X_BEARER_TOKEN', '')
auth = OAuth2Bearer(token=bearer_token)
client = XClient(auth=auth)
# Get a user's info
user = client.users.get_me()
print(user)