crawlfetch

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

PHP "YouTube downloader script" tutorials usually scrape the watch page with regular expressions, which stops working within weeks. With CrawlFetch the PHP side is a single authenticated GET using Guzzle, and the file is streamed straight to disk.

What you need

  • A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
  • PHP 8.1 or newer.
  • Composer and the guzzlehttp/guzzle package.
  • The key stored in an environment variable named CRAWLFETCH_KEY, never in source control.
composer require guzzlehttp/guzzle

Resolve a video URL in PHP

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.

<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://api.crawlfetch.com',
'timeout' => 15,
'headers' => ['Authorization' => 'Bearer ' . getenv('CRAWLFETCH_KEY')],
]);
function resolveVideo(Client $client, string $url, string $format = 'mp4', string $res = '1080p'): array
{
$response = $client->get('/v1/resolve', [
'query' => ['url' => $url, 'format' => $format, 'res' => $res],
]);
return json_decode((string) $response->getBody(), true, flags: JSON_THROW_ON_ERROR);
}
$video = resolveVideo($client, 'https://youtu.be/aqz-KE-bpKQ');
echo $video['title'] . ' — ' . $video['bestQuality'] . PHP_EOL;

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.

<?php
// Streams to disk without loading the video into memory.
$http = new GuzzleHttp\Client(['timeout' => 0]);
$http->get($video['downloadUrl'], ['sink' => __DIR__ . "/{$video['id']}.mp4"]);

Handle errors and rate limits

Guzzle throws ClientException for 4xx and ServerException for 5xx. Catch GuzzleHttp\Exception\RequestException, read $e->getResponse()?->getStatusCode(), and for 429 use $e->getResponse()->getHeaderLine('Retry-After'). Guzzle's RetryMiddleware can retry 500s automatically.

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 a queue worker (Laravel queues, Symfony Messenger), not inside the web request.
  • Set 'timeout' => 0 only on the download client; keep a short timeout on the API client.

FAQ

Can I build a YouTube downloader website in PHP with this?

You can build the backend for one, but only download content you own or have permission to use, and keep the API key on the server.

Does it work on shared hosting?

Yes, if outbound HTTPS is allowed. There are no binaries to install, unlike yt-dlp-based setups.

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