Video download API for Swift: YouTube, TikTok, Instagram and more
Swift's URLSession with async/await makes calling CrawlFetch straightforward on macOS, Linux (Vapor) or iOS. For iOS, route the resolve call through your backend; the app then downloads from the returned URL with a background session.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- Swift 5.7+ (async/await).
- iOS 15 / macOS 12 or Swift on Linux with FoundationNetworking.
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
Resolve a video URL in Swift
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 Foundationstruct ResolvedVideo: Decodable {let id: Stringlet title: Stringlet bestQuality: Stringlet downloadUrl: URLlet audioUrl: URL}func resolve(_ videoURL: String, format: String = "mp4", res: String = "1080p") async throws -> ResolvedVideo {var comps = URLComponents(string: "https://api.crawlfetch.com/v1/resolve")!comps.queryItems = [.init(name: "url", value: videoURL),.init(name: "format", value: format),.init(name: "res", value: res),]var req = URLRequest(url: comps.url!)let key = ProcessInfo.processInfo.environment["CRAWLFETCH_KEY"] ?? ""req.setValue("Bearer " + key, forHTTPHeaderField: "Authorization")let (data, resp) = try await URLSession.shared.data(for: req)guard (resp as? HTTPURLResponse)?.statusCode == 200 else { throw URLError(.badServerResponse) }return try JSONDecoder().decode(ResolvedVideo.self, from: data)}
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.
let video = try await resolve("https://youtu.be/aqz-KE-bpKQ")let (tempFile, _) = try await URLSession.shared.download(from: video.downloadUrl)let dest = FileManager.default.temporaryDirectory.appendingPathComponent(video.id + ".mp4")try FileManager.default.moveItem(at: tempFile, to: dest)
Handle errors and rate limits
Cast the response to HTTPURLResponse and switch on statusCode; value(forHTTPHeaderField: "Retry-After") gives the 429 delay. Use Task.sleep(for:) between retries and respect task cancellation.
| 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 iOS use a background
URLSessionConfigurationfor large downloads.
FAQ
Can an iOS app call the API directly?
It shouldn't — the key would be embedded in the app. Proxy the resolve call through your backend.
Does it work with Vapor on Linux?
Yes. Import FoundationNetworking on Linux, or use Vapor's own Client.
Related
Last updated 2026-10-25