Video download API for C# / .NET: YouTube, TikTok, Instagram and more
.NET developers often reach for YoutubeExplode, which reverse-engineers YouTube and needs updates when it changes. With CrawlFetch, HttpClient and System.Text.Json are enough — and the download streams straight to a FileStream.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- .NET 6 or newer.
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
Resolve a video URL in C# / .NET
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.
using System.Net.Http.Headers;using System.Net.Http.Json;public record MediaFormat(string Label, string Container, string Kind, double SizeMb, string Url);public record ResolvedVideo(string Id, string Title, string BestQuality,MediaFormat[] Formats, string DownloadUrl, string AudioUrl);var http = new HttpClient { BaseAddress = new Uri("https://api.crawlfetch.com"), Timeout = TimeSpan.FromSeconds(15) };http.DefaultRequestHeaders.Authorization =new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("CRAWLFETCH_KEY"));var url = Uri.EscapeDataString("https://youtu.be/aqz-KE-bpKQ");var video = await http.GetFromJsonAsync<ResolvedVideo>($"/v1/resolve?url={url}&format=mp4&res=1080p");Console.WriteLine($"{video!.Title} ({video.BestQuality})");
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.
using var dl = new HttpClient { Timeout = Timeout.InfiniteTimeSpan };using var resp = await dl.GetAsync(video.DownloadUrl, HttpCompletionOption.ResponseHeadersRead);resp.EnsureSuccessStatusCode();await using var file = File.Create($"{video.Id}.mp4");await resp.Content.CopyToAsync(file);
Handle errors and rate limits
GetFromJsonAsync throws HttpRequestException with a StatusCode property on failure. For 429 use GetAsync and read resp.Headers.RetryAfter?.Delta. Polly (or Microsoft.Extensions.Http.Resilience) adds retries with backoff to a typed HttpClient in one line of configuration.
| 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. - Register the client with
IHttpClientFactoryinstead of creatingHttpClientper request. - Use
HttpCompletionOption.ResponseHeadersReadfor downloads so the body is streamed.
FAQ
Is this an alternative to YoutubeExplode?
For getting metadata and download URLs, yes — the extraction runs as a hosted service instead of inside your process.
Does it work in ASP.NET Core?
Yes. See the ASP.NET Core integration for a minimal API endpoint.
Related
Last updated 2026-10-25