Video download API for Kotlin: YouTube, TikTok, Instagram and more
Kotlin backends and Android apps can both use CrawlFetch through OkHttp. On Android, call the API from your own backend so the key never ships in the APK — the download URL is what the app needs.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- Kotlin 1.9+ and OkHttp 4.
- kotlinx.serialization (or Moshi) for JSON.
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
implementation("com.squareup.okhttp3:okhttp:4.12.0")implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
Resolve a video URL in Kotlin
Call GET /v1/resolve with the video URL. Watch, Shorts, youtu.be and embed URLs are all accepted. Optional format (mp4, webm, m4a, opus) and res (360p–2160p) parameters pick the file; the API falls back to the closest available resolution.
import kotlinx.serialization.Serializableimport kotlinx.serialization.json.Jsonimport okhttp3.HttpUrl.Companion.toHttpUrlimport okhttp3.OkHttpClientimport okhttp3.Request@Serializabledata class Video(val id: String, val title: String, val bestQuality: String, val downloadUrl: String)val json = Json { ignoreUnknownKeys = true }val client = OkHttpClient()fun resolve(videoUrl: String, format: String = "mp4", res: String = "1080p"): Video {val url = "https://api.crawlfetch.com/v1/resolve".toHttpUrl().newBuilder().addQueryParameter("url", videoUrl).addQueryParameter("format", format).addQueryParameter("res", res).build()val req = Request.Builder().url(url).header("Authorization", "Bearer " + System.getenv("CRAWLFETCH_KEY")).build()client.newCall(req).execute().use { resp ->check(resp.isSuccessful) { "resolve failed: " + resp.code }return json.decodeFromString(resp.body!!.string())}}
Download the file
downloadUrl is a direct download link on the CrawlFetch domain — a plain file URL, not a stream manifest or player page. Fetch it like any other file and stream the body to disk (or to your own storage) rather than buffering whole videos in memory.
import java.io.Filefun download(video: Video, file: File) {client.newCall(Request.Builder().url(video.downloadUrl).build()).execute().use { resp ->check(resp.isSuccessful) { "download failed: " + resp.code }resp.body!!.byteStream().use { input -> file.outputStream().use { input.copyTo(it) } }}}
Handle errors and rate limits
Check resp.isSuccessful and resp.code; read resp.header("Retry-After") on 429. Run calls on Dispatchers.IO in coroutines, and on Android hand long downloads to WorkManager so they survive process death.
| Status | Meaning | What to do |
|---|---|---|
| 400 | Missing or malformed url parameter | Validate the URL before calling; don't retry |
| 401 | Missing, invalid or revoked API key | Check the key and the env var it is read from |
| 404 | Video is private, removed or region-locked | Tell the user; retrying will not help |
| 429 | Quota or rate limit exceeded | Wait for the Retry-After header, then retry |
| 500 | Upstream manifest fetch failed | Safe to retry with exponential backoff |
Production checklist
- Call the API from your server, not from browser or mobile code that would expose the key.
- Cache the resolve response for up to a few hours —
downloadUrlstays valid for roughly six. - On Android, request the URL from your backend and download with
DownloadManageror WorkManager.
FAQ
Can I put the API key in my Android app?
No. APKs are easy to decompile. Keep the key on a server you control and expose only the resolved URL. See offline playback apps.
Does it work with Ktor client?
Yes — any client that can set headers and stream a body works the same way.
Related
Last updated 2026-10-25