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
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:
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 fromGET /import/status:
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
webhookUrland 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: trueto 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.