I build Replit apps where a user pastes a video link and the app saves the video. It's a common thing to ask Replit Agent for, and it works in the workspace preview with a test clip. Then you publish it and the save breaks. I kept hitting the same things, so here's what's going on and how I handle it now.
There are two separate problems
First, a TikTok, YouTube, Instagram or X link is a web page, not a file. fetch on that URL gets you an HTML document, not an .mp4.
Second, files you write to the deployment's disk don't stick. Autoscale deployments scale to zero when idle and restart regularly by design, so anything on local disk is gone after a restart. Files belong in App Storage.
So you need to (a) turn the link into an actual file and (b) put that file somewhere that survives a restart.
App Storage, briefly
A quick note, since App Storage behaves differently from S3:
- Buckets are always private. There's no public URL, so your app serves files to the users it has authorized.
- You reach the bucket through Replit's SDK (
@replit/object-storage), with no access keys to manage. - No lifecycle rules. To delete old files, run a Scheduled deployment.
The easy case: a direct file link
If the link already points straight at a file (an .mp4 on a CDN), don't read it into memory or write it to disk. Stream it into App Storage:
import { Readable } from 'node:stream' import type { ReadableStream } from 'node:stream/web' import { Client } from '@replit/object-storage' const storage = new Client() export const saveFileFromUrl = async (fileUrl: string, key: string) => { const res = await fetch(fileUrl) if (!res.ok || !res.body) throw new Error(`Source returned ${res.status}`) await storage.uploadFromStream(key, Readable.fromWeb(res.body as ReadableStream)) } That's the whole fix when you have a file URL, and it costs nothing.
Where that stops
The trouble is users rarely paste a file URL. They paste the page they watched the video on. Getting the video out of that page takes a downloader, and two things make that hard in a published app:
- You have to keep the downloader working every time a site changes its pages.
- The published app runs on datacenter IPs, which a lot of sites block, so the downloader that worked in your preview gets errors or bot checks in production.
You can run and maintain a downloader yourself - plenty of people do. I got tired of babysitting it on every deploy, so I built importly.io: you send it the link, it gets the file, and it calls your webhook when the file is ready. Then your webhook copies the file into App Storage. Free to try, no card.
Here's the backend I use. Three routes: start the import, receive the webhook and copy the file, serve the file back to your users.
import express from 'express' import { Readable } from 'node:stream' import type { ReadableStream } from 'node:stream/web' import { Client } from '@replit/object-storage' const app = express() app.use(express.json()) const storage = new Client() // the bucket you created in the App Storage tool const importlyHeaders = { Authorization: `Bearer ${process.env.IMPORTLY_API_KEY}` } // 1. The user pastes a link: start the import and return straight away app.post('/api/videos', async (req, res) => { const response = await fetch('https://api.importly.io/import', { method: 'POST', headers: { ...importlyHeaders, 'Content-Type': 'application/json' }, body: JSON.stringify({ url: req.body.url, // the TikTok, YouTube or X link the user pasted videoQuality: '720p', maxCostDollars: 0.5, webhookUrl: `${process.env.APP_URL}/api/webhooks/importly`, }), }) const body = await response.json() if (!response.ok || !body.success) { return res.status(response.status).json({ error: body.error, code: body.code }) } // save body.data.jobId on the user's video record, with status 'pending' res.json({ jobId: body.data.jobId, status: body.data.status }) }) // 2. Importly calls back when the file is ready: copy it into App Storage const saveFile = async (jobId: string) => { const response = await fetch(`https://api.importly.io/import/status?id=${encodeURIComponent(jobId)}`, { headers: importlyHeaders, }) const body = await response.json() if (!body.success) return const job = body.data if (job.status === 'failed') { // mark the record failed with job.code and job.message return } if (job.status !== 'completed') return const extension = job.result.filename.split('.').pop() const key = `videos/${jobId}.${extension}` const already = await storage.exists(key) if (already.ok && already.value) return // a retried webhook for a file you already copied // server to server: from Importly's storage straight into your bucket, no temp file const file = await fetch(job.result.mediaUrl) if (!file.ok || !file.body) throw new Error(`Download returned ${file.status}`) await storage.uploadFromStream(key, Readable.fromWeb(file.body as ReadableStream)) // find the record by jobId and save key, job.result.title and job.result.duration } app.post('/api/webhooks/importly', async (req, res) => { const { data } = req.body ?? {} if (data?.type !== 'import' || typeof data.jobId !== 'string') return res.sendStatus(200) try { await saveFile(data.jobId) res.sendStatus(200) } catch (error) { console.error('Copy to App Storage failed', data.jobId, error) res.sendStatus(500) // Importly retries the delivery later } }) // 3. Your users get the file through your app (buckets are private) app.get('/api/videos/:jobId/file', (req, res) => { // check the signed-in user owns this video, and read its key from the record const key = `videos/${req.params.jobId}.mp4` storage .downloadAsStream(key) .on('error', () => res.status(404).end()) .pipe(res.type('video/mp4')) }) app.listen(Number(process.env.PORT ?? 5000), '0.0.0.0') A few things that bit me:
- The webhook route takes only the
jobIdfrom the request and reads the result with my key, so a forged request can't drop a file into my bucket. - A long video can take more than the 10 seconds the webhook waits for an answer, so a retry may arrive for a file that's already saved. The
existscheck skips it. - The webhook URL has to be public, so while you're still working in the workspace, poll
GET /import/statusinstead of waiting on the webhook.
The download URL stays valid for 7 days, so the copy can also run later from a status check. Set includeVideo: false for audio only, and if you only want a transcript there's no file to move at all.
Full write-up, with the Python version and the audio-only and transcript variants: Save videos from links into Replit App Storage.