crawlfetch

Video download API for Ruby: YouTube, TikTok, Instagram and more

Ruby's standard library is enough to call CrawlFetch: Net::HTTP for the request, JSON for the body and a streamed read for the download. No gems, no shelling out to yt-dlp from your app server.

What you need

  • A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
  • Ruby 3.0 or newer.
  • The key stored in an environment variable named CRAWLFETCH_KEY, never in source control.

Resolve a video URL in Ruby

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.

require "net/http"
require "json"
require "uri"
API = URI("https://api.crawlfetch.com")
def resolve(url, format: "mp4", res: "1080p")
uri = URI.join(API, "/v1/resolve")
uri.query = URI.encode_www_form(url: url, format: format, res: res)
req = Net::HTTP::Get.new(uri)
req["Authorization"] = "Bearer #{ENV.fetch("CRAWLFETCH_KEY")}"
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 15) { |h| h.request(req) }
raise "resolve failed: #{res.code}" unless res.is_a?(Net::HTTPSuccess)
JSON.parse(res.body)
end
video = resolve("https://youtu.be/aqz-KE-bpKQ")
puts "#{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.

def download(video, path)
uri = URI(video["downloadUrl"])
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request(Net::HTTP::Get.new(uri)) do |res|
File.open(path, "wb") { |f| res.read_body { |chunk| f.write(chunk) } }
end
end
end
download(video, "#{video["id"]}.mp4")

Handle errors and rate limits

Net::HTTP returns response objects rather than raising, so branch on the class: Net::HTTPTooManyRequests (429) carries res["Retry-After"]; Net::HTTPServerError means retry with backoff. Wrap calls with retry and an attempt counter in a begin/rescue block.

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.
  • Run downloads in Sidekiq or another background job, never in a Puma request thread.

FAQ

Is there a Rails-specific setup?

See the Rails integration for a service object and ActiveJob example.

Can I use Faraday or HTTParty instead?

Yes. Any HTTP client that can send an Authorization header and stream a response body works.

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