crawlfetch

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.Serializable
import kotlinx.serialization.json.Json
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
@Serializable
data 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.File
fun 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.

StatusMeaningWhat to do
400Missing or malformed url parameterValidate the URL before calling; don't retry
401Missing, invalid or revoked API keyCheck the key and the env var it is read from
404Video is private, removed or region-lockedTell the user; retrying will not help
429Quota or rate limit exceededWait for the Retry-After header, then retry
500Upstream manifest fetch failedSafe 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 — downloadUrl stays valid for roughly six.
  • On Android, request the URL from your backend and download with DownloadManager or 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

Add video downloads to your product today.

1,000 credits on signup plus 500 every month. That is roughly 180 YouTube clips to start, with no card.

Create free account