Working Python examples for the X API v2.
pip install -r requirements.txtThe requirements.txt file includes:
xdk>=0.4.5- X Developer Kit for Python
For read-only operations (search, lookup):
export BEARER_TOKEN='your_bearer_token'For user actions (post, like, repost, bookmark, mute, etc.):
export CLIENT_ID='your_client_id'
export CLIENT_SECRET='your_client_secret'Note: Most user action examples (create post, like, repost, bookmark, mute, block, etc.) require OAuth 2.0 authentication with CLIENT_ID and CLIENT_SECRET. Read-only examples (search, lookup) only require BEARER_TOKEN.
posts/create_post.py- Create a new post (requiresCLIENT_ID,CLIENT_SECRET)posts/delete_post.py- Delete a post (requiresCLIENT_ID,CLIENT_SECRET)posts/get_liking_users.py- Get users who liked a postposts/get_post_counts_all.py- Get post counts (full archive)posts/get_post_counts_recent.py- Get post counts (recent)posts/get_posts_by_ids.py- Look up posts by IDposts/get_quoted_posts.py- Get posts that quote a postposts/get_reposted_by.py- Get users who reposted a postposts/search_all.py- Full archive searchposts/search_recent.py- Recent search (last 7 days)
users/get_users_by_usernames.py- Look up users by username (bearer token)users/get_users_by_usernames_user_context.py- Look up user by username (user context) (requiresCLIENT_ID,CLIENT_SECRET)users/get_users_me.py- Get authenticated user (me) (requiresCLIENT_ID,CLIENT_SECRET)
users/block/get_blocking.py- Get users blocked by a user (requiresCLIENT_ID,CLIENT_SECRET)
users/bookmark/get_bookmarks.py- Get user's bookmarks (requiresCLIENT_ID,CLIENT_SECRET)users/bookmark/create_bookmark.py- Create a bookmark (requiresCLIENT_ID,CLIENT_SECRET)users/bookmark/delete_bookmark.py- Delete a bookmark (requiresCLIENT_ID,CLIENT_SECRET)
users/follow/get_followers.py- Get user's followersusers/follow/get_following.py- Get users a user is following
users/like/get_liked_posts.py- Get posts liked by a user (requiresCLIENT_ID,CLIENT_SECRET)users/like/like_post.py- Like a post (requiresCLIENT_ID,CLIENT_SECRET)users/like/unlike_post.py- Unlike a post (requiresCLIENT_ID,CLIENT_SECRET)
users/lists/follow_list.py- Follow a list (requiresCLIENT_ID,CLIENT_SECRET)users/lists/get_followed_lists.py- Get lists followed by a userusers/lists/get_list_memberships.py- Get list membershipsusers/lists/get_owned_lists.py- Get lists owned by a userusers/lists/get_pinned_lists.py- Get pinned lists (requiresCLIENT_ID,CLIENT_SECRET)users/lists/pin_list.py- Pin a list (requiresCLIENT_ID,CLIENT_SECRET)users/lists/unfollow_list.py- Unfollow a list (requiresCLIENT_ID,CLIENT_SECRET)users/lists/unpin_list.py- Unpin a list (requiresCLIENT_ID,CLIENT_SECRET)
users/mute/get_muting.py- Get users muted by a user (requiresCLIENT_ID,CLIENT_SECRET)users/mute/mute_user.py- Mute a user (requiresCLIENT_ID,CLIENT_SECRET)users/mute/unmute_user.py- Unmute a user (requiresCLIENT_ID,CLIENT_SECRET)
users/repost/repost_post.py- Repost a post (requiresCLIENT_ID,CLIENT_SECRET)users/repost/unrepost_post.py- Undo a repost (requiresCLIENT_ID,CLIENT_SECRET)
users/timeline/get_mentions.py- Get user mentions timelineusers/timeline/get_posts.py- Get user's posts timelineusers/timeline/get_home_timeline.py- Get home timeline (requiresCLIENT_ID,CLIENT_SECRET)
streams/stream_posts_filtered.py- Filtered stream with rulesstreams/stream_posts_sample.py- Sampled stream
lists/add_member.py- Add member to a list (requiresCLIENT_ID,CLIENT_SECRET)lists/create_list.py- Create a new list (requiresCLIENT_ID,CLIENT_SECRET)lists/delete_list.py- Delete a list (requiresCLIENT_ID,CLIENT_SECRET)lists/get_list_by_id.py- Get list by IDlists/get_list_followers.py- Get list followerslists/get_list_members.py- Get list memberslists/get_list_posts.py- Get posts from a listlists/remove_member.py- Remove member from a list (requiresCLIENT_ID,CLIENT_SECRET)lists/update_list.py- Update a list (requiresCLIENT_ID,CLIENT_SECRET)
spaces/get_spaces_by_ids.py- Look up Spaces by IDspaces/search_spaces.py- Search for Spaces
direct_messages/get_events_by_conversation.py- Get DM events by conversation (requiresCLIENT_ID,CLIENT_SECRET)direct_messages/get_one_to_one_conversation_events.py- Get one-to-one conversation events (requiresCLIENT_ID,CLIENT_SECRET)direct_messages/get_user_conversation_events.py- Get user conversation events (requiresCLIENT_ID,CLIENT_SECRET)direct_messages/post_dm_to_conversation.py- Post DM to conversation (requiresCLIENT_ID,CLIENT_SECRET)direct_messages/post_group_conversation_dm.py- Post group conversation DM (requiresCLIENT_ID,CLIENT_SECRET)direct_messages/post_one_to_one_dm.py- Post one-to-one DM (requiresCLIENT_ID,CLIENT_SECRET)
media/media_upload_v2.py- Media upload v2 (requiresCLIENT_ID,CLIENT_SECRET)media/upload.py- Media upload (requiresCLIENT_ID,CLIENT_SECRET)
compliance/create_jobs.py- Create compliance jobcompliance/download_results.py- Download compliance resultscompliance/get_jobs_by_id.py- Get compliance job by IDcompliance/get_jobs.py- Get compliance jobscompliance/upload_ids.py- Upload IDs for compliance
usage/get_usage.py- Get API usage information
# Make sure environment variables are set
python posts/search_recent.py