Skip to main content

XDK Python SDK

The official Python SDK for the X API v2 (v0.13.0).

Installation

uv add xdk

Or with pip:

pip install xdk

Quick Start

Bearer Token (App-Only Authentication)

from xdk import Client

client = Client(bearer_token="YOUR_BEARER_TOKEN")

# Look up posts by ID
posts = client.posts.get_by_ids(ids=["1234567890"])
print(posts.data)

# Search recent posts (auto-paginating iterator of pages)
for page in client.posts.search_recent(query="python lang:en", max_results=10):
    print(page.data)
    break

OAuth 2.0 PKCE (User Context + Auto Refresh)

from xdk import Client
from xdk.posts.models import CreateRequest

client = Client(
    client_id="YOUR_CLIENT_ID",
    redirect_uri="http://localhost:3000/callback",
    scope=["tweet.read", "tweet.write", "users.read", "offline.access"],
)

auth_url = client.get_authorization_url(state="csrf-state")
# After user authorizes and redirects back with ?code=...
token = client.exchange_code("AUTH_CODE_FROM_CALLBACK")

created = client.posts.create(body=CreateRequest(text="Hello from XDK Python!"))
print(created.data)

OAuth 1.0a (User Context)

from xdk import Client, OAuth1
from xdk.posts.models import CreateRequest

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)
post = client.posts.create(body=CreateRequest(text="Signed with OAuth 1.0a!"))

Pagination (Cursor) & Real-Time Streaming (StreamConfig)

from xdk import Client, Cursor, StreamConfig

client = Client(bearer_token="YOUR_BEARER_TOKEN")

# Flatten items across pages with Cursor
for post in Cursor(client.posts.search_recent, "xdk", max_results=10).items(limit=25):
    print(post)

# Resilient NDJSON streaming with idle timeout & heartbeat handling
stream_cfg = StreamConfig(
    max_retries=-1,
    initial_backoff=1.0,
    idle_timeout=60.0,
    on_keep_alive=lambda: None,
)
for event in client.stream.posts(stream_config=stream_cfg):
    print(event.data)

Runnable Use-Case Examples & Continuous Canary

See examples/README.md for runnable end-to-end examples covering all 24 X API endpoint groups and the continuous canary monitor:

# Run all 10 end-to-end use-case suites (self-verifying against local loopback simulator)
uv run python -m examples.use_cases

# Run a one-shot canary health check (synthetic + optional live api.x.com probes)
uv run python -m examples.canary --once --live

Features

  • Full coverage of all X API v2 endpoint groups with Pydantic v2 models
  • Bearer Token, OAuth 2.0 PKCE (confidential & public clients with automatic token refresh), and OAuth 1.0a RFC 5849 signing
  • Automatic cursor pagination (Iterator pages + Cursor(...).items() / .pages())
  • Resilient chunked NDJSON streaming with incremental UTF-8 decoding and exponential backoff reconnect
  • Optional default timeout and 429 Retry-After / 5xx transient retry support

Documentation

For more information, see the documentation.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for xdk 0.13.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xdk 0.13.0
File Size Uploaded Provenance Actions
xdk-0.13.0.tar.gz 672.5 kB With provenance Details for xdk-0.13.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xdk 0.13.0
File Interpreter ABI Platform Actions
xdk-0.13.0-py3-none-any.whl Python 3 none any Details for xdk-0.13.0-py3-none-any.whl

Total release size: 956.6 kB

Release files / xdk-0.13.0.tar.gz

Download URL xdk-0.13.0.tar.gz
Size 672.5 kB
Tags Source
SHA-256 checksum
How to use checksums
88980eb5ca075ba88d3b00572feef35645ed7f1545a0799b44b035d747745b1e
BLAKE2b-256 checksum
How to use checksums
9d4afb6a48dd40470b637950385a2906fb50c51bd5d679fc9f84b711a3e3a8a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / xdk-0.13.0-py3-none-any.whl

Download URL xdk-0.13.0-py3-none-any.whl
Size 284.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70d645122e28388b5c9f2c90ac4027934b5e1d8b90070cb6866efa92e04dbe99
BLAKE2b-256 checksum
How to use checksums
d50e3c10d01ff232e380f63f4f6471498b0db51110beec16c8539009b7e9e2d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for xdk 0.13.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for xdk-0.13.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.13.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page