crawlfetch

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

Java 11 added java.net.http.HttpClient, which is all you need to call CrawlFetch. This guide resolves a URL, parses the JSON with Jackson and streams the video to a file with BodyHandlers.ofFile.

What you need

  • A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
  • Java 17 or newer (11+ works without records).
  • Jackson Databind for JSON (com.fasterxml.jackson.core:jackson-databind).
  • The key stored in an environment variable named CRAWLFETCH_KEY, never in source control.

Resolve a video URL in Java

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 com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
public class Resolve {
static final HttpClient HTTP = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10)).build();
static final ObjectMapper JSON = new ObjectMapper();
static JsonNode resolve(String url, String format, String res) throws Exception {
String qs = "url=" + URLEncoder.encode(url, StandardCharsets.UTF_8)
+ "&format=" + format + "&res=" + res;
HttpRequest req = HttpRequest.newBuilder(URI.create("https://api.crawlfetch.com/v1/resolve?" + qs))
.header("Authorization", "Bearer " + System.getenv("CRAWLFETCH_KEY"))
.timeout(Duration.ofSeconds(15))
.build();
HttpResponse<String> resp = HTTP.send(req, HttpResponse.BodyHandlers.ofString());
if (resp.statusCode() != 200) {
throw new IllegalStateException("resolve failed: " + resp.statusCode());
}
return JSON.readTree(resp.body());
}
}

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.

JsonNode video = Resolve.resolve("https://youtu.be/aqz-KE-bpKQ", "mp4", "1080p");
HttpRequest dl = HttpRequest.newBuilder(URI.create(video.get("downloadUrl").asText())).build();
Path out = Path.of(video.get("id").asText() + ".mp4");
Resolve.HTTP.send(dl, HttpResponse.BodyHandlers.ofFile(out)); // streamed to disk

Handle errors and rate limits

Check statusCode() explicitly — HttpClient does not throw on 4xx/5xx. Read resp.headers().firstValue("retry-after") for 429. For retries, Resilience4j's Retry or Spring Retry give you exponential backoff without hand-written loops.

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.
  • Use sendAsync with a bounded executor when resolving many videos in parallel.

FAQ

Do I need a YouTube library for Java?

No. The API returns plain JSON, so the JDK HTTP client and any JSON library are enough.

What about Spring Boot?

See the Spring Boot integration for a RestClient bean and controller.

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