{"id":2329,"library":"tweepy","title":"Tweepy - Python client for X API","description":"Tweepy is a popular, easy-to-use Python library for accessing the X API (formerly Twitter API). It provides a Pythonic way to interact with both v1.1 and v2 of the API, handling authentication, requests, and response parsing. The current version is 4.16.0, and it generally releases new versions frequently, often tied to changes or additions in the X API.","status":"active","version":"4.16.0","language":"en","source_language":"en","source_url":"https://github.com/tweepy/tweepy","tags":["twitter","x-api","social-media","api-client","async"],"install":[{"cmd":"pip install tweepy","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Client","correct":"from tweepy import Client"},{"note":"API is imported directly from the top-level tweepy package, not from a submodule.","wrong":"from tweepy.client import API","symbol":"API","correct":"from tweepy import API"},{"symbol":"StreamingClient","correct":"from tweepy import StreamingClient"},{"symbol":"OAuth1UserHandler","correct":"from tweepy import OAuth1UserHandler"},{"symbol":"TweepyException","correct":"from tweepy import TweepyException"}],"quickstart":{"code":"import os\nimport tweepy\n\n# For Twitter API v2, using a Bearer Token (read-only for public data)\nbearer_token = os.environ.get(\"TWITTER_BEARER_TOKEN\", \"YOUR_BEARER_TOKEN\")\n\nif not bearer_token or bearer_token == \"YOUR_BEARER_TOKEN\":\n    print(\"Warning: TWITTER_BEARER_TOKEN environment variable not set or is default. Quickstart may fail.\")\n    print(\"To run this example, set TWITTER_BEARER_TOKEN or replace 'YOUR_BEARER_TOKEN' with an actual key.\")\n\ntry:\n    client = tweepy.Client(bearer_token)\n\n    # Example: Get recent tweets by a user\n    username = \"tweepy\"\n    response = client.get_user(username=username)\n    user_data = response.data\n\n    if user_data:\n        user_id = user_data.id\n        print(f\"User ID for @{username}: {user_id}\")\n\n        response = client.get_users_tweets(user_id, tweet_fields=[\"created_at\", \"author_id\"], max_results=5)\n        if response.data:\n            print(f\"Latest tweets from @{username}:\")\n            for tweet in response.data:\n                print(f\"- [{tweet.created_at.strftime('%Y-%m-%d %H:%M')}] {tweet.text}\")\n        else:\n            print(f\"No tweets found for @{username}.\")\n    else:\n        print(f\"User @{username} not found or error occurred.\")\n\nexcept tweepy.TweepyException as e:\n    print(f\"An error occurred with Tweepy: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n","lang":"python","description":"This quickstart demonstrates how to use `tweepy.Client` for accessing the Twitter API v2. It fetches the user ID for a specified username and then retrieves their latest tweets. For public data access, a Bearer Token (Developer Portal > Projects & Apps > your_app > Keys and tokens) is typically sufficient. For user-specific actions (like posting tweets), more complex OAuth 2.0 PKCE or OAuth 1.0a authentication is required."},"warnings":[{"fix":"Upgrade your Python environment to version 3.9 or newer. Alternatively, pin Tweepy to a version prior to 4.15.0 if Python upgrade is not feasible (e.g., `pip install 'tweepy<4.15.0'`).","message":"Tweepy v4.15.0 and newer dropped support for Python 3.7 and 3.8. Users on these older Python versions must upgrade to Python 3.9 or newer to use the latest Tweepy versions.","severity":"breaking","affected_versions":">=4.15.0"},{"fix":"Migrate to Twitter API v2 streaming endpoints using `tweepy.StreamingClient`. The API v2 offers a more robust and flexible streaming experience.","message":"Twitter API v1.1 streaming methods (e.g., `Stream.sample`, `AsyncStream.sample`) were removed in Tweepy v4.13.0 due to Twitter API retirement of these features.","severity":"breaking","affected_versions":">=4.13.0"},{"fix":"For new development, prioritize `tweepy.Client` and the Twitter API v2. Use `tweepy.API` only when specifically interacting with legacy v1.1 features not yet available in v2 or when specific OAuth 1.0a features are required. Always ensure your authentication method matches the client being used.","message":"A crucial distinction exists between `tweepy.API` (for Twitter API v1.1) and `tweepy.Client` (for Twitter API v2). They use different authentication schemes (OAuth 1.0a vs. Bearer Token/OAuth 2.0 PKCE) and target different API versions. Using the wrong client for an endpoint will result in errors.","severity":"gotcha","affected_versions":"All 4.x"},{"fix":"Consult the official Tweepy documentation (e.g., the 'Authentication' section) for detailed OAuth 2.0 PKCE or OAuth 1.0a setup instructions when user-context actions are needed. A Bearer Token alone is insufficient for these operations.","message":"Twitter API v2 authentication via `tweepy.Client` primarily uses Bearer Tokens for public data access. For user-specific actions (e.g., posting tweets, sending DMs, liking), OAuth 2.0 with PKCE or OAuth 1.0a User Context is required, which is more complex to set up and manage.","severity":"gotcha","affected_versions":"All 4.x"},{"fix":"Implement robust error handling around API calls, specifically catching `tweepy.TweepyException`. Integrate a retry logic with exponential backoff, potentially using a library like `tenacity`, to gracefully handle rate limit exhaustion.","message":"The Twitter API has strict rate limits. Exceeding these limits will result in `tweepy.TweepyException` (specifically a 429 HTTP error for 'Too Many Requests'). Continuously hitting rate limits can lead to temporary blocks.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}