### Installation and Initialization Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Instructions on how to install the wrapper and initialize the client with your API key. ```APIDOC ## Installation Copy the `twttrapi.py` file into your project directory. ## Usage Import the `TwttrAPIClient` class and instantiate it with your RapidAPI key: ```python from twttrapi import TwttrAPIClient api_key = "your_rapidapi_key_here" client = TwttrAPIClient(api_key) ``` ``` -------------------------------- ### Get User Following Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves the list of users that a specific account follows. ```APIDOC ## Get User Following ### Description Retrieves the list of users that a specific account follows. ### Method GET ### Endpoint /user_following ### Parameters #### Query Parameters - **username** (string) - Required - The username of the account whose following list is to be retrieved. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get accounts followed by username following = client.user_following(username="twitter") ``` ### Response #### Success Response (200) - **data** (object) - Contains following list data. - **users** (array) - List of user objects that the specified user is following. - **screen_name** (string) - The screen name of the followed user. - **name** (string) - The display name of the followed user. #### Response Example ```json { "data": { "users": [ { "screen_name": "followed_user1", "name": "Followed User One" } ] } } ``` ``` -------------------------------- ### Get User Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Retrieves information about a specific user. ```APIDOC ## GET /user ### Description Retrieves profile information for a specified user, identified by username or user ID. ### Method GET ### Endpoint `/user` ### Parameters #### Query Parameters - **username** (string) - Optional - The username of the user. - **user_id** (string) - Optional - The user ID of the user. ### Response #### Success Response (200) - **user_object** (object) - The user object containing profile details. ### Request Example ```python # Example usage within the client username = "twitterdev" user_info = client.get_user(username=username) print(user_info) ``` ``` -------------------------------- ### Get User Following List Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves the list of users that a specific account follows. Initialize the client with your RapidAPI key. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get accounts followed by username following = client.user_following(username="twitter") # Process following list for user in following.get('data', {}).get('users', []): print(f"@{user['screen_name']} - {user['name']}") ``` -------------------------------- ### Get Search Suggestions Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves autocomplete suggestions for a given search query. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get search suggestions suggestions = client.search_suggestions(query="pytho") # Process suggestions for suggestion in suggestions.get('data', []): print(f"Suggestion: {suggestion}") ``` -------------------------------- ### Get For You Timeline with Pagination Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves the algorithmically curated 'For You' timeline for the authenticated user. Requires a session token. Supports pagination. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Get For You timeline timeline = client.for_you_timeline() # Process timeline tweets for tweet in timeline.get('data', {}).get('tweets', []): print(f"@{tweet['user']['screen_name']}: {tweet['text'][:100]}...") # Get more tweets with pagination if 'cursor' in timeline: more_tweets = client.for_you_timeline(cursor=timeline['cursor']) ``` -------------------------------- ### Get Tweet by ID Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Example of how to fetch a specific tweet using its ID with the TwttrAPIClient. Ensure you have instantiated the client with your API key first. ```python from twttrapi import TwttrAPIClient api_key = "your_rapidapi_key_here" client = TwttrAPIClient(api_key) # Get a tweet by its ID tweet_id = "1652849795336159233" tweet = client.get_tweet(tweet_id) print(tweet) ``` -------------------------------- ### Get User Followers with Pagination Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves the list of users who follow a specific account. Supports pagination. Initialize the client with your RapidAPI key. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get followers by username followers = client.user_followers(username="twitter") # Process followers for user in followers.get('data', {}).get('users', []): print(f"@{user['screen_name']} - {user['name']}") print(f" Followers: {user['followers_count']}") # Pagination if 'cursor' in followers: next_page = client.user_followers(username="twitter", cursor=followers['cursor']) ``` -------------------------------- ### Get User Followers Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves the list of users who follow a specific account. ```APIDOC ## Get User Followers ### Description Retrieves the list of users who follow a specific account. Supports pagination. ### Method GET ### Endpoint /user_followers ### Parameters #### Query Parameters - **username** (string) - Required - The username of the account whose followers are to be retrieved. - **cursor** (string) - Optional - Token for paginating through results. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get followers by username followers = client.user_followers(username="twitter") ``` ### Response #### Success Response (200) - **data** (object) - Contains follower data and cursor for pagination. - **users** (array) - List of user objects. - **screen_name** (string) - The screen name of the follower. - **name** (string) - The display name of the follower. - **followers_count** (integer) - The number of followers the user has. - **cursor** (string) - Token for the next page of results. #### Response Example ```json { "data": { "users": [ { "screen_name": "follower1", "name": "Follower One", "followers_count": 1000 } ], "cursor": "next_cursor_token" } } ``` ``` -------------------------------- ### Get User Media Posts Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves media posts (photos and videos) shared by a specific user. Initialize the client with your RapidAPI key. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get media posts by username media = client.user_media(username="NASA") # Process media posts for post in media.get('data', {}).get('tweets', []): if 'media' in post.get('entities', {}): for m in post['entities']['media']: print(f"Media URL: {m['media_url_https']}") print(f"Type: {m['type']}") ``` -------------------------------- ### Get User Media Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves media posts (photos and videos) shared by a specific user. ```APIDOC ## Get User Media ### Description Retrieves media posts (photos and videos) shared by a specific user. ### Method GET ### Endpoint /user_media ### Parameters #### Query Parameters - **username** (string) - Required - The username of the user whose media posts are to be retrieved. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get media posts by username media = client.user_media(username="NASA") ``` ### Response #### Success Response (200) - **data** (object) - Contains media data. - **tweets** (array) - List of tweet objects containing media. - **entities** (object) - Contains entities within the tweet. - **media** (array) - List of media objects. - **media_url_https** (string) - The HTTPS URL of the media. - **type** (string) - The type of media (e.g., 'photo', 'video'). #### Response Example ```json { "data": { "tweets": [ { "entities": { "media": [ { "media_url_https": "https://pbs.twimg.com/media/F123456X0AA7890.jpg", "type": "photo" } ] } } ] } } ``` ``` -------------------------------- ### Get Following Timeline Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves the chronological timeline showing tweets from accounts the authenticated user follows. Requires a session token. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Get Following timeline (chronological) timeline = client.following_timeline() # Process timeline for tweet in timeline.get('data', {}).get('tweets', []): print(f"@{tweet['user']['screen_name']}: {tweet['text']}") ``` -------------------------------- ### Get User Liked Tweets Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves tweets that a specific user has liked. Initialize the client with your RapidAPI key. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get liked tweets by username likes = client.user_likes(username="jack") # Process liked tweets for tweet in likes.get('data', {}).get('tweets', []): print(f"Liked: {tweet['text'][:100]}...") ``` -------------------------------- ### Get DM Conversations Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves a list of direct message conversations for the authenticated user. ```APIDOC ## GET /dm_conversations ### Description Retrieves the list of direct message conversations for the authenticated user. ### Method GET ### Endpoint /dm_conversations ### Parameters #### Path Parameters None #### Query Parameters - **cursor** (string) - Optional - Cursor for fetching the next page of conversations. #### Request Body None ### Request Example ```json { "cursor": "next_cursor_token" } ``` ### Response #### Success Response (200) - **data.conversations** (array) - A list of direct message conversations. - **participant.screen_name** (string) - The screen name of the other participant in the conversation. - **last_message.text** (string) - The text of the last message in the conversation. - **cursor** (string) - Optional - Cursor for fetching the next page of conversations. #### Response Example ```json { "data": { "conversations": [ { "participant": { "screen_name": "twitter" }, "last_message": { "text": "Hello there!" } } ] }, "cursor": "some_cursor_token" } ``` ``` -------------------------------- ### Get Direct Message Conversations Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Retrieves a list of direct message conversations. ```APIDOC ## GET /dm/conversations ### Description Fetches a list of direct message conversations for the authenticated user. ### Method GET ### Endpoint `/dm/conversations` ### Parameters #### Query Parameters - **cursor** (string) - Optional - A cursor for pagination. ### Response #### Success Response (200) - **conversations** (array) - A list of direct message conversation objects. - **next_cursor** (string) - The cursor for the next page of results. ### Request Example ```python # Example usage within the client conversations = client.get_dm_conversations() print(conversations) ``` ``` -------------------------------- ### Get Tweet Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Retrieves a specific tweet by its ID. ```APIDOC ## GET /tweet/{tweet_id} ### Description Retrieves a specific tweet using its unique identifier. ### Method GET ### Endpoint `/tweet/{tweet_id}` ### Parameters #### Path Parameters - **tweet_id** (string) - Required - The unique identifier of the tweet to retrieve. ### Response #### Success Response (200) - **tweet_object** (object) - The tweet object containing all tweet details. ### Request Example ```python # Example usage within the client tweet_id = "1652849795336159233" tweet = client.get_tweet(tweet_id) print(tweet) ``` ``` -------------------------------- ### Get User Profile Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieve detailed profile information for a Twitter user by their username. ```APIDOC ## Get User Profile The `get_user` method retrieves detailed profile information for a Twitter user by their username. ### Endpoint `/get_user` ### Parameters #### Query Parameters - **username** (string) - Required - The username of the Twitter user. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get user profile user = client.get_user(username="elonmusk") ``` ``` -------------------------------- ### Get DM Conversation Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves messages from a specific direct message conversation. ```APIDOC ## GET /dm_conversation ### Description Retrieves messages from a specific direct message conversation. ### Method GET ### Endpoint /dm_conversation ### Parameters #### Path Parameters None #### Query Parameters - **username** (string) - Optional - The username of the participant to get the conversation with. - **user_id** (string) - Optional - The user ID of the participant to get the conversation with. #### Request Body None ### Request Example ```json { "username": "twitter" } ``` ### Response #### Success Response (200) - **data.messages** (array) - A list of messages in the conversation. - **sender.screen_name** (string) - The screen name of the message sender. - **text** (string) - The text content of the message. #### Response Example ```json { "data": { "messages": [ { "sender": { "screen_name": "twitter" }, "text": "Hi!" } ] } } ``` ``` -------------------------------- ### Get Tweet Conversation Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Retrieves the entire conversation thread for a given tweet. ```APIDOC ## GET /tweet/conversation/{tweet_id} ### Description Retrieves all tweets that are part of a conversation thread, starting from a given tweet. ### Method GET ### Endpoint `/tweet/conversation/{tweet_id}` ### Parameters #### Path Parameters - **tweet_id** (string) - Required - The ID of the tweet that starts the conversation. ### Response #### Success Response (200) - **conversation_object** (object) - An object containing the tweets in the conversation. ### Request Example ```python # Example usage within the client tweet_id = "1652849795336159233" conversation = client.get_tweet_conversation(tweet_id) print(conversation) ``` ``` -------------------------------- ### Get User Tweets with Pagination Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves tweets posted by a specific user. Supports pagination using the cursor parameter. Initialize the client with your RapidAPI key. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get tweets by username tweets = client.user_tweets(username="elonmusk") # Or get tweets by user ID tweets = client.user_tweets(user_id="44196397") # Process tweets for tweet in tweets.get('data', {}).get('tweets', []): print(f"{tweet['created_at']}: {tweet['text'][:100]}...") # Pagination - get next page if 'cursor' in tweets: next_page = client.user_tweets(username="elonmusk", cursor=tweets['cursor']) ``` -------------------------------- ### Get Specific Direct Message Conversation Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Retrieves a specific direct message conversation. ```APIDOC ## GET /dm/conversation ### Description Retrieves a specific direct message conversation with a user. ### Method GET ### Endpoint `/dm/conversation` ### Parameters #### Query Parameters - **username** (string) - Optional - The username of the other participant in the conversation. - **user_id** (string) - Optional - The user ID of the other participant. - **cursor** (string) - Optional - A cursor for pagination. ### Response #### Success Response (200) - **conversation** (object) - The direct message conversation object. - **next_cursor** (string) - The cursor for the next page of results. ### Request Example ```python # Example usage within the client username = "otheruser" conversation = client.get_dm_conversation(username=username) print(conversation) ``` ``` -------------------------------- ### Get User Profile by Username Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieve detailed profile information for a Twitter user by specifying their username. This includes profile details, follower counts, and other relevant user data. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get user profile user = client.get_user(username="elonmusk") ``` -------------------------------- ### Get Direct Message Conversations Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieve a list of direct message conversations using `get_dm_conversations`. Supports pagination via the `cursor` parameter for fetching older conversations. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Get all DM conversations conversations = client.get_dm_conversations() # Process conversations for convo in conversations.get('data', {}).get('conversations', []): print(f"Conversation with: {convo.get('participant', {}).get('screen_name')}") print(f"Last message: {convo.get('last_message', {}).get('text')}") # Pagination if 'cursor' in conversations: more_convos = client.get_dm_conversations(cursor=conversations['cursor']) ``` -------------------------------- ### Get Tweet Conversation Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieve a tweet along with its entire reply thread to provide full conversation context. This is useful for understanding the flow of discussions. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get tweet with its conversation thread tweet_id = "1652849795336159233" conversation = client.get_tweet_conversation(tweet_id) # Access conversation data for tweet in conversation.get('data', {}).get('tweets', []): print(f"@{tweet['user']['screen_name']}: {tweet['text']}") ``` -------------------------------- ### Get Specific Direct Message Conversation Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Fetch messages from a specific direct message conversation using `get_dm_conversation`. You can identify the conversation by the recipient's username or user ID. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Get conversation by username messages = client.get_dm_conversation(username="twitter") # Or by user ID messages = client.get_dm_conversation(user_id="783214") # Process messages for msg in messages.get('data', {}).get('messages', []): sender = msg.get('sender', {}).get('screen_name') text = msg.get('text') print(f"@{sender}: {text}") ``` -------------------------------- ### Get Tweet by ID Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieve detailed information for a specific tweet using its unique ID. The response includes tweet content, engagement metrics, and author details. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get a specific tweet tweet_id = "1652849795336159233" tweet = client.get_tweet(tweet_id) # Access tweet data print(f"Text: {tweet['data']['text']}") print(f"Likes: {tweet['data']['favorite_count']}") print(f"Retweets: {tweet['data']['retweet_count']}") print(f"Author: {tweet['data']['user']['screen_name']}") ``` -------------------------------- ### Client Initialization Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Initialize the TwttrAPIClient with your RapidAPI key. Optionally, provide a session token for authenticated requests and proxy configuration. ```APIDOC ## Client Initialization The `TwttrAPIClient` class is the main entry point for all API interactions. It requires a RapidAPI key and optionally accepts a session token for authenticated requests and a proxy configuration. ### Request Example ```python from twttrapi import TwttrAPIClient # Basic initialization with just API key api_key = "your_rapidapi_key_here" client = TwttrAPIClient(api_key) # Initialization with session token for authenticated operations client = TwttrAPIClient( api_key="your_rapidapi_key_here", session="your_session_token", proxy="http://proxy.example.com:8080" # Optional proxy ) ``` ``` -------------------------------- ### Instantiate TwttrAPIClient Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Import the TwttrAPIClient class and instantiate it with your RapidAPI key to begin using the API wrapper. ```python from twttrapi import TwttrAPIClient api_key = "your_rapidapi_key_here" client = TwttrAPIClient(api_key) ``` -------------------------------- ### Login with Email/Username Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Logs in a user using their email or username and password. ```APIDOC ## POST /login/email_username ### Description Authenticates a user using their email address or username and password. ### Method POST ### Endpoint `/login/email_username` ### Parameters #### Request Body - **username_or_email** (string) - Required - The user's username or email address. - **password** (string) - Required - The user's password. ### Response #### Success Response (200) - **login_data** (object) - Data required for subsequent 2FA login or session information. ### Request Example ```python # Example usage within the client username = "user@example.com" password = "your_password" login_info = client.login_email_username(username_or_email=username, password=password) print(login_info) ``` ``` -------------------------------- ### Login with 2FA Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Completes the login process for users requiring two-factor authentication. ```APIDOC ## POST /login/2fa ### Description Completes the login process for accounts that require two-factor authentication (2FA). ### Method POST ### Endpoint `/login/2fa` ### Parameters #### Request Body - **login_data** (object) - Required - Data obtained from a previous login step (e.g., `login_email_username`). - **response** (string) - Required - The 2FA code or response. ### Response #### Success Response (200) - **session_info** (object) - Information about the authenticated session. ### Request Example ```python # Example usage within the client (assuming login_info from previous step) # login_info = client.login_email_username(...) # two_fa_code = "123456" # session = client.login_2fa(login_data=login_info, response=two_fa_code) # print(session) ``` ``` -------------------------------- ### Handle Two-Factor Authentication (2FA) Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Complete the login process when two-factor authentication is required. This method uses the login_data from the initial login attempt and the provided 2FA code. ```python from twttrapi import TwttrAPIClient from getpass import getpass client = TwttrAPIClient("your_rapidapi_key_here") # Initial login attempt response = client.login_email_username("your_username", "your_password") if "login_data" in response: # 2FA is required login_data = response["login_data"] tfa_code = getpass(response["message"] + ": ") # Complete 2FA response = client.login_2fa(login_data, tfa_code) if response.get("success") == True: print(f"Login successful! Session: {response['session']}") else: print(f"2FA Error: {response.get('error', 'Unknown error')}") ``` -------------------------------- ### User Following Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Fetches the list of users that a specific user is following. ```APIDOC ## GET /user/following ### Description Retrieves a list of users that a specified user is following. User can be identified by username or user ID. ### Method GET ### Endpoint `/user/following` ### Parameters #### Query Parameters - **username** (string) - Optional - The username of the user. - **user_id** (string) - Optional - The user ID of the user. - **cursor** (string) - Optional - A cursor for pagination. ### Response #### Success Response (200) - **following** (array) - A list of user objects that the specified user is following. - **next_cursor** (string) - The cursor for the next page of results. ### Request Example ```python # Example usage within the client username = "twitterdev" following = client.user_following(username=username) print(following) ``` ``` -------------------------------- ### Follow User Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Follows a specified user. ```APIDOC ## POST /user/follow ### Description Initiates a follow action on a specified user. ### Method POST ### Endpoint `/user/follow` ### Parameters #### Query Parameters - **username** (string) - Optional - The username of the user to follow. - **user_id** (string) - Optional - The user ID of the user to follow. ### Response #### Success Response (200) - **message** (string) - Confirmation message of the follow action. ### Request Example ```python # Example usage within the client username_to_follow = "targetuser" result = client.follow_user(username=username_to_follow) print(result) ``` ``` -------------------------------- ### Access User Data Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Prints various details about a user, such as name, username, bio, follower count, and verification status. Assumes a 'user' dictionary is already populated. ```python print(f"Name: {user['data']['name']}") print(f"Username: @{user['data']['screen_name']}") print(f"Bio: {user['data']['description']}") print(f"Followers: {user['data']['followers_count']}") print(f"Following: {user['data']['friends_count']}") print(f"Verified: {user['data']['verified']}") ``` -------------------------------- ### Get Tweet by ID Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieve detailed information about a specific tweet using its ID. ```APIDOC ## Get Tweet by ID The `get_tweet` method retrieves detailed information about a specific tweet by its ID, including text content, media, engagement metrics, and author information. ### Endpoint `/get_tweet` ### Parameters #### Query Parameters - **tweet_id** (string) - Required - The ID of the tweet to retrieve. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get a specific tweet tweet_id = "1652849795336159233" tweet = client.get_tweet(tweet_id) # Access tweet data print(f"Text: {tweet['data']['text']}") print(f"Likes: {tweet['data']['favorite_count']}") print(f"Retweets: {tweet['data']['retweet_count']}") print(f"Author: {tweet['data']['user']['screen_name']}") ``` ### Response #### Success Response (200) - **data** (object) - Contains the tweet details. - **text** (string) - The content of the tweet. - **favorite_count** (integer) - The number of likes. - **retweet_count** (integer) - The number of retweets. - **user** (object) - Information about the tweet author. - **screen_name** (string) - The author's screen name. ``` -------------------------------- ### Authentication - Two-Factor Authentication Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Handle two-factor authentication using the login data from an initial login attempt and the provided 2FA code. ```APIDOC ## Authentication - Two-Factor Authentication The `login_2fa` method handles two-factor authentication when required during the login process. It takes the login_data from the initial login response and the user's 2FA code. ### Request Example ```python from twttrapi import TwttrAPIClient from getpass import getpass client = TwttrAPIClient("your_rapidapi_key_here") # Initial login attempt response = client.login_email_username("your_username", "your_password") if "login_data" in response: # 2FA is required login_data = response["login_data"] tfa_code = getpass(response["message"] + ": ") # Complete 2FA response = client.login_2fa(login_data, tfa_code) if response.get("success") == True: print(f"Login successful! Session: {response['session']}") else: print(f"2FA Error: {response.get('error', 'Unknown error')}") ``` ``` -------------------------------- ### For You Timeline Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves the algorithmically curated "For You" timeline for the authenticated user. ```APIDOC ## For You Timeline ### Description Retrieves the algorithmically curated "For You" timeline for the authenticated user. Supports pagination. ### Method GET ### Endpoint /for_you_timeline ### Parameters #### Query Parameters - **cursor** (string) - Optional - Token for paginating through results. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Get For You timeline timeline = client.for_you_timeline() ``` ### Response #### Success Response (200) - **data** (object) - Contains timeline data and cursor for pagination. - **tweets** (array) - List of tweet objects. - **user** (object) - User who posted the tweet. - **screen_name** (string) - The screen name of the user. - **cursor** (string) - Token for the next page of results. #### Response Example ```json { "data": { "tweets": [ { "user": { "screen_name": "user1" }, "text": "Tweet text..." } ], "cursor": "next_cursor_token" } } ``` ``` -------------------------------- ### Get User Tweets Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Retrieves tweets posted by a specific user. Supports pagination via cursor parameter. ```APIDOC ## Get User Tweets ### Description Retrieves tweets posted by a specific user. Supports pagination via cursor parameter. ### Method GET ### Endpoint /user_tweets ### Parameters #### Query Parameters - **username** (string) - Required - The username of the user whose tweets are to be retrieved. - **user_id** (string) - Required - The user ID of the user whose tweets are to be retrieved. - **cursor** (string) - Optional - Token for paginating through results. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Get tweets by username tweets = client.user_tweets(username="elonmusk") # Or get tweets by user ID tweets = client.user_tweets(user_id="44196397") ``` ### Response #### Success Response (200) - **data** (object) - Contains tweet data and cursor for pagination. - **tweets** (array) - List of tweet objects. - **cursor** (string) - Token for the next page of results. #### Response Example ```json { "data": { "tweets": [ { "created_at": "2023-10-26T10:00:00Z", "text": "Example tweet text..." } ], "cursor": "next_cursor_token" } } ``` ``` -------------------------------- ### Create Tweet with Options Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Posts a new tweet, supporting simple text, replies, URL attachments, and uploaded media. Requires a session token. The response contains the ID of the created tweet. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Post a simple tweet response = client.create_tweet("Hello from TwttrAPI!") print(f"Tweet ID: {response['data']['id']}") # Post a reply to another tweet response = client.create_tweet( tweet_text="This is a reply!", in_reply_to_tweet_id="1652849795336159233" ) # Post a tweet with a URL attachment response = client.create_tweet( tweet_text="Check out this link!", attachment_url="https://example.com/article" ) # Post a tweet with uploaded media upload_response = client.upload_image("https://example.com/image.jpg") media_id = upload_response['data']['media_id'] response = client.create_tweet( tweet_text="Check out this photo!", media_id=media_id ) ``` -------------------------------- ### User Media Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Fetches media (images, videos) posted by a specific user. ```APIDOC ## GET /user/media ### Description Retrieves media content (images and videos) posted by a specified user. Identification can be done via username or user ID. ### Method GET ### Endpoint `/user/media` ### Parameters #### Query Parameters - **username** (string) - Optional - The username of the user. - **user_id** (string) - Optional - The user ID of the user. - **cursor** (string) - Optional - A cursor for pagination. ### Response #### Success Response (200) - **media** (array) - A list of media objects. - **next_cursor** (string) - The cursor for the next page of results. ### Request Example ```python # Example usage within the client username = "twitterdev" media = client.user_media(username=username) print(media) ``` ``` -------------------------------- ### Upload Image Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Uploads an image to be used as an attachment. ```APIDOC ## POST /media/upload/image ### Description Uploads an image from a URL to the API, returning a media ID that can be used in other requests (e.g., `create_tweet`, `send_dm`). ### Method POST ### Endpoint `/media/upload/image` ### Parameters #### Request Body - **image_url** (string) - Required - The URL of the image to upload. ### Response #### Success Response (200) - **media_id** (string) - The identifier for the uploaded media. ### Request Example ```python # Example usage within the client image_url = "http://example.com/image.jpg" media_id = client.upload_image(image_url=image_url) print(media_id) ``` ``` -------------------------------- ### Authentication - Login with Email/Username Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Authenticate a user with their Twitter credentials to obtain a session token for subsequent authenticated requests. ```APIDOC ## Authentication - Login with Email/Username The `login_email_username` method authenticates a user with their Twitter credentials and returns a session token for subsequent authenticated requests. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Login with username or email response = client.login_email_username("your_username", "your_password") if "success" in response and response["success"] == True: session_token = response["session"] print(f"Login successful! Session: {session_token}") # Create authenticated client auth_client = TwttrAPIClient("your_rapidapi_key_here", session=session_token) else: if "errors" in response: print(f"Error: {response['errors'][0]['message']}") elif "login_data" in response: print("2FA required") else: print("Login failed") ``` ``` -------------------------------- ### Logout Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Logs out the current user session. ```APIDOC ## POST /logout ### Description Ends the current user session, logging the user out. ### Method POST ### Endpoint `/logout` ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating successful logout. ### Request Example ```python # Example usage within the client result = client.logout() print(result) ``` ``` -------------------------------- ### Login with Email/Username Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Authenticate a user using their Twitter username and password. This method returns a session token upon successful login, which can then be used to create an authenticated client. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here") # Login with username or email response = client.login_email_username("your_username", "your_password") if "success" in response and response["success"] == True: session_token = response["session"] print(f"Login successful! Session: {session_token}") # Create authenticated client auth_client = TwttrAPIClient("your_rapidapi_key_here", session=session_token) else: if "errors" in response: print(f"Error: {response['errors'][0]['message']}") elif "login_data" in response: print("2FA required") else: print("Login failed") ``` -------------------------------- ### User Followers Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Fetches the list of users who follow a specific user. ```APIDOC ## GET /user/followers ### Description Retrieves a list of users who follow a specified user. User can be identified by username or user ID. ### Method GET ### Endpoint `/user/followers` ### Parameters #### Query Parameters - **username** (string) - Optional - The username of the user. - **user_id** (string) - Optional - The user ID of the user. - **cursor** (string) - Optional - A cursor for pagination. ### Response #### Success Response (200) - **followers** (array) - A list of user objects representing followers. - **next_cursor** (string) - The cursor for the next page of results. ### Request Example ```python # Example usage within the client username = "twitterdev" followers = client.user_followers(username=username) print(followers) ``` ``` -------------------------------- ### Search Videos Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Searches for videos based on a query. ```APIDOC ## GET /search/videos ### Description Searches for videos on Twitter based on a given query. ### Method GET ### Endpoint `/search/videos` ### Parameters #### Query Parameters - **query** (string) - Required - The search term for videos. - **cursor** (string) - Optional - A cursor for pagination. ### Response #### Success Response (200) - **videos** (array) - A list of video objects matching the query. - **next_cursor** (string) - The cursor for the next page of results. ### Request Example ```python # Example usage within the client query = "dogs" videos = client.search_videos(query=query) print(videos) ``` ``` -------------------------------- ### Like Tweet Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Likes (favorites) a tweet from the authenticated account. Requires a session token. ```APIDOC ## Like Tweet ### Description Likes (favorites) a tweet from the authenticated account. ### Method POST ### Endpoint /2/users/:id/like ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the tweet to like. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") tweet_id = "1652849795336159233" response = client.favorite_tweet(tweet_id) ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the tweet was liked successfully. - **error** (string) - Error message if the action failed. #### Response Example ```json { "success": true } ``` ``` -------------------------------- ### Search Suggestions Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Provides search suggestions based on a query. ```APIDOC ## GET /search/suggestions ### Description Returns search suggestions based on a given query string. ### Method GET ### Endpoint `/search/suggestions` ### Parameters #### Query Parameters - **query** (string) - Required - The search term to get suggestions for. - **cursor** (string) - Optional - A cursor for pagination. ### Response #### Success Response (200) - **suggestions** (array) - A list of search suggestion strings. - **next_cursor** (string) - The cursor for the next page of results. ### Request Example ```python # Example usage within the client query = "python" suggestions = client.search_suggestions(query=query) print(suggestions) ``` ``` -------------------------------- ### Authentication - Logout Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Terminate the current session and invalidate the session token. ```APIDOC ## Authentication - Logout The `logout` method terminates the current session and invalidates the session token. ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Logout and invalidate session response = client.logout() print(response) # Expected: {"success": true} ``` ``` -------------------------------- ### Send Direct Message Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Sends a direct message to a user. ```APIDOC ## POST /dm/send ### Description Sends a direct message to a specified user. Can optionally include media. ### Method POST ### Endpoint `/dm/send` ### Parameters #### Request Body - **message** (string) - Required - The content of the direct message. - **to_user_name** (string) - Optional - The username of the recipient. - **to_user_id** (string) - Optional - The user ID of the recipient. - **media_id** (string) - Optional - The ID of uploaded media to attach to the message. ### Response #### Success Response (200) - **message_object** (object) - The sent direct message object. ### Request Example ```python # Example usage within the client message_text = "Hello there!" recipient_username = "otheruser" result = client.send_dm(message=message_text, to_user_name=recipient_username) print(result) ``` ``` -------------------------------- ### Send Direct Message Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Send a direct message to a user using `send_dm`. Supports sending by username or user ID, and can include media uploaded via `upload_image`. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Send DM by username response = client.send_dm( message="Hello! This is a test message.", to_user_name="twitter" ) # Or send by user ID response = client.send_dm( message="Hello! This is a test message.", to_user_id="783214" ) # Send DM with media upload_response = client.upload_image("https://example.com/image.jpg") media_id = upload_response['data']['media_id'] response = client.send_dm( message="Check out this image!", to_user_name="twitter", media_id=media_id ) if response.get('success'): print("Message sent successfully") ``` -------------------------------- ### Like a Tweet Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Likes (favorites) a specified tweet from the authenticated account. Requires a session token. ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Like a tweet tweet_id = "1652849795336159233" response = client.favorite_tweet(tweet_id) if response.get('success'): print("Tweet liked successfully") ``` -------------------------------- ### Create Tweet Source: https://github.com/aimadnet/twttrapi-python/blob/main/README.md Posts a new tweet. ```APIDOC ## POST /tweet/create ### Description Creates and posts a new tweet. Can optionally include attachments or reply to another tweet. ### Method POST ### Endpoint `/tweet/create` ### Parameters #### Request Body - **tweet_text** (string) - Required - The content of the tweet. - **attachment_url** (string) - Optional - A URL for an image or video attachment. - **in_reply_to_tweet_id** (string) - Optional - The ID of the tweet this tweet is a reply to. - **media_id** (string) - Optional - The ID of uploaded media to attach. ### Response #### Success Response (200) - **tweet_object** (object) - The newly created tweet object. ### Request Example ```python # Example usage within the client tweet_text = "This is a test tweet! #PythonAPI" tweet = client.create_tweet(tweet_text=tweet_text) print(tweet) ``` ``` -------------------------------- ### Follow User Source: https://context7.com/aimadnet/twttrapi-python/llms.txt Follows a Twitter user from the authenticated account. Requires a session token. ```APIDOC ## Follow User ### Description Follows a Twitter user from the authenticated account. ### Method POST ### Endpoint /2/users/:id/following ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the user to follow. #### Query Parameters - **username** (string) - Required - The username of the user to follow (alternative to user ID). ### Request Example ```python from twttrapi import TwttrAPIClient client = TwttrAPIClient("your_rapidapi_key_here", session="your_session_token") # Follow by username response = client.follow_user(username="twitter") # Or follow by user ID response = client.follow_user(user_id="783214") ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the user was followed successfully. - **error** (string) - Error message if the action failed. #### Response Example ```json { "success": true } ``` ```