Skip to main content
This example showcases how to quickly search for Posts using the XDK using Bearer Token authentication.

Step 1: Install the SDK

pip install xdk

Step 2: Get Your Bearer Token

  1. Log in to the X Developer Portal.
  2. Create or select an app.
  3. Under “Keys and Tokens,” generate a Bearer Token (app-only auth).

Step 3: Write and Run Your First Script

Create a file quickstart.py:
# Import the client
from xdk import Client
# Replace with your actual Bearer Token
client = Client(bearer_token="YOUR_BEARER_TOKEN_HERE")
# Fetch recent Posts mentioning "api"
# search_recent returns an Iterator, so iterate over it
for page in client.posts.search_recent(query="api", max_results=10):
    if page.data and len(page.data) > 0:
        # Access first Post - Pydantic models support both attribute and dict access
        first_post = page.data[0]
        post_text = first_post.text if hasattr(first_post, 'text') else first_post.get('text', '')
        print(f"Latest Post: {post_text}")
        break
    else:
        print("No Posts found.")
        break
Run it:
python quickstart.py
Expected Output:
Latest Post: Exciting updates on XDK Python SDK!
Troubleshooting: If you get a 401 error, double-check your Bearer Token. For rate limits (429), wait and retry.

Next Steps

  • Explore Authentication to understand how to use Bearer Token (app-only) auth, OAuth 2.0 with PKCE (user context), and OAuth 1.0a (legacy user context).
  • Learn about Pagination for use-cases where you want large number of results returned without worrying about making multiple API calls.
  • Dive into Streaming to learn how to work with real-time data. For detailed code examples using the Python XDK, check out our code samples GitHub repo.