Video download API for Go: YouTube, TikTok, Instagram and more
Go services usually need the download URL, not a CLI dependency. This guide calls CrawlFetch with net/http, decodes the response into a struct and streams the file with io.Copy — standard library only.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- Go 1.21 or newer.
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
Resolve a video URL in Go
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.
package mainimport ("encoding/json""fmt""net/http""net/url""os""time")type Format struct {Label string `json:"label"`Container string `json:"container"`Kind string `json:"kind"`SizeMb float64 `json:"sizeMb"`URL string `json:"url"`}type Video struct {ID string `json:"id"`Title string `json:"title"`BestQuality string `json:"bestQuality"`Formats []Format `json:"formats"`DownloadURL string `json:"downloadUrl"`AudioURL string `json:"audioUrl"`}var client = &http.Client{Timeout: 15 * time.Second}func resolve(videoURL, format, res string) (*Video, error) {q := url.Values{"url": {videoURL}, "format": {format}, "res": {res}}req, _ := http.NewRequest("GET", "https://api.crawlfetch.com/v1/resolve?"+q.Encode(), nil)req.Header.Set("Authorization", "Bearer "+os.Getenv("CRAWLFETCH_KEY"))resp, err := client.Do(req)if err != nil {return nil, err}defer resp.Body.Close()if resp.StatusCode != http.StatusOK {return nil, fmt.Errorf("resolve: status %d", resp.StatusCode)}var v Videoreturn &v, json.NewDecoder(resp.Body).Decode(&v)}
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.
func download(v *Video, path string) error {resp, err := http.Get(v.DownloadURL) // no client timeout: large filesif err != nil {return err}defer resp.Body.Close()f, err := os.Create(path)if err != nil {return err}defer f.Close()_, err = io.Copy(f, resp.Body)return err}
Handle errors and rate limits
Check resp.StatusCode before decoding. For 429, parse resp.Header.Get("Retry-After") with strconv.Atoi and time.Sleep; for 500, retry with exponential backoff and a context.Context deadline so retries stop when the caller gives up.
| 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. - Use
http.NewRequestWithContextso cancelled requests stop promptly. - Share one
http.Clientacross goroutines — it is safe for concurrent use.
FAQ
Why not use a Go YouTube library?
Libraries that decode YouTube's player locally need updates whenever YouTube changes it. An API keeps that maintenance off your release cycle.
Can I resolve many videos concurrently?
Yes. Bound concurrency with a semaphore or errgroup.SetLimit and stay within your plan's rate limit. See bulk YouTube downloads.
Related
Last updated 2026-10-25