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"
response = client.posts.search_recent(query="api", max_results=10)

# Print the first Post's text
if response.data:
    print(f"Latest Post: {response.data[0]['text']}")
else:
    print("No Posts found.")
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.0.
  • 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.