Write operations require a login_cookies object obtained via the twitter/user_login_v2 endpoint.
Important Technical Details:
- Cookies:
login_cookies is a base64-encoded JSON string. Pass it back to the API verbatim. - Media Uploads: Use
multipart/form-data for upload_media_v2 and update_avatar_v2. Do not manually set the Content-Type header when using requests with the files parameter; let the library handle it. - Tweet Creation: Use
tweet_text (not text) in the JSON body for create_tweet_v2. - Profile Updates: Use
PATCH with JSON. Use description (not bio) to update the profile text. - Bookmarks: Use
count (not pageSize) in the body for bookmarks_v2.
# Example: Login, Upload Media, and Tweet
import os, requests
BASE = "https://api.twitterapi.io"
H = {"x-api-key": os.environ["TWITTERAPI_IO_KEY"], "Content-Type": "application/json"}
def login_v2(user_name, email, password, proxy, totp_secret=None):
body = {"user_name": user_name, "email": email, "password": password, "proxy": proxy}
if totp_secret: body["totp_secret"] = totp_secret
r = requests.post(f"{BASE}/twitter/user_login_v2", json=body, headers=H)
r.raise_for_status()
return r.json()["login_cookies"]
def upload_media(cookies, proxy, path, media_category=None, is_long_video=False):
mime = "video/mp4" if path.lower().endswith(".mp4") else "image/jpeg"
with open(path, "rb") as f:
files = {"file": (os.path.basename(path), f, mime)}
data = {"login_cookies": cookies, "proxy": proxy, "is_long_video": str(is_long_video).lower()}
if media_category: data["media_category"] = media_category
r = requests.post(f"{BASE}/twitter/upload_media_v2", files=files, data=data,
headers={"x-api-key": os.environ["TWITTERAPI_IO_KEY"]})
r.raise_for_status(); return r.json()
def create_tweet(cookies, proxy, text, *, media_ids=None):
body = {"login_cookies": cookies, "proxy": proxy, "tweet_text": text}
if media_ids: body["media_ids"] = media_ids
r = requests.post(f"{BASE}/twitter/create_tweet_v2", json=body, headers=H)
r.raise_for_status(); return r.json()
# Execution
cookies = login_v2(os.environ["X_USER"], os.environ["X_EMAIL"], os.environ["X_PASSWORD"], os.environ["X_PROXY"])
proxy = os.environ["X_PROXY"]
media_id = upload_media(cookies, proxy, "photo.jpg")["media_id"]
create_tweet(cookies, proxy, "Check this out", media_ids=[media_id])