Skip to main content
This guide builds on the Python SDK setup, which covers install and authentication.

Workflow to post media tweets and DM attachments

Use this workflow to post a media tweet or reply. Use it too to DM 1 uploaded media item. Tweet and reply media posts use public media URLs directly on client.x.tweets.create. Send up to 4 image URLs or exactly 1 MP4 video URL. The video can be up to 100 MB. Do not mix video with other media. Do not upload first when the media URL is already public.
Use with_raw_response.create to capture the durable write action. Send a unique Idempotency-Key. Store id, request.hash, billing, result, and statusUrl. Poll while terminal is false. Retry only when safeToRetry is true, using a new key.
To post an image reply, add reply_to_tweet_id.
For DM attachments, upload the local file first. Then pass the returned media.media_id as the only media_ids item.
client.x.tweets.create returns tweet.tweet_id for confirmed posts. Raw create responses can also include the pending write fields above while confirmation runs. client.x.media.upload returns media.media_id for DM attachments. client.x.dm.send returns dm.message_id for support tickets, CRM records, queue jobs, or agent memory. Keep DM body text in private systems. Shared logs, public artifacts, queue status, and agent handoffs should omit full DM bodies. They should store message_id, optional media_id, account, user_id, and send status. Leave reply_to_message_id unset even if generated SDK types expose it. The REST endpoint rejects DM reply threading. client.x.dm.delete_message deletes a DM. Text-only tweet and reply writes cost 30 credits. Tweet media adds 2 credits per started MB across attached files. Uploading media costs 10 credits. Sending the DM costs 10 credits. Do not pass uploaded media.media_id values to client.x.tweets.create. That method uses media with public media URLs.

REST routes

Next steps