Skip to main content
Your users paste X posts and your Python backend needs the video files. Without an X developer account there’s no stable way to get them from a server: page scraping and unofficial endpoints break, and requests from cloud IPs get blocked or rate-limited. This sends the post link to Importly and saves the finished MP4. It needs Python 3.10 or later and requests. The Node version does the download in JavaScript. Many pasted posts have only text or images. An import of one of those fails, but only after the post has been looked up, which can take a minute. So the script first asks for the post’s length, which usually comes back much sooner, and only downloads when there’s a video.

The code

download_x_video.py
Send the link as your users paste it: x.com and twitter.com post links both work, with or without tracking parameters such as ?s=20. The link needs its https://; a bare x.com/... is rejected with 400 INVALID_URL.

The length check

POST /metadata/basic returns four fields for $0.003, and status checks are free:
A text-only post comes back with duration: null. A post with a video has its length in seconds (X reports fractions, such as 114.322), so you can turn away long videos before you pay to download them. The 10-minute limit in the script is an example; set your own. Skip the check if your links always have a video, for example ones your own scraper collected from media posts. See Metadata.

What comes back

The finished job from GET /import/status:
X serves each video in several bitrates. You don’t pick one: Importly takes the best stream at or below videoQuality and gives you a single MP4. Set includeVideo: false for the audio only, or add includeTranscript: true for a timestamped transcript in the same job. All options are in Downloads.

Errors to handle

Importly fetches what’s public. Every error has a documentationUrl with the fix. See Media unavailable and Error handling. Only download videos your users have the right to use.

In production

  • Don’t make a web request wait on the polling loop. Run it in a worker (Celery, RQ, a cron job), or pass webhookUrl and save the file when Importly calls you back (Webhooks), keeping a status check as a fallback. The Celery guide and Django guide show both.
  • Copy the file somewhere permanent, or set store: true to have every import land in your own bucket (recipe).
  • For many links at once, see Batch imports.
Downloads cost $0.002 per MB, with a $0.01 minimum, so a one-minute clip at 720p is usually two to four cents. The length check is $0.003. Try it free, no card required. See Pricing & credits.