Video download API for C++: YouTube, TikTok, Instagram and more
For native tools, the cpr library (a modern C++ wrapper around libcurl) keeps the CrawlFetch integration short. Pair it with nlohmann/json to read the response.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- C++17 compiler.
- cpr and nlohmann/json (both available via vcpkg or Conan).
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
vcpkg install cpr nlohmann-json
Resolve a video URL in C++
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.
#include <cpr/cpr.h>#include <nlohmann/json.hpp>#include <cstdlib>#include <iostream>int main() {const char* key = std::getenv("CRAWLFETCH_KEY");auto r = cpr::Get(cpr::Url{"https://api.crawlfetch.com/v1/resolve"},cpr::Parameters{{"url", "https://youtu.be/aqz-KE-bpKQ"}, {"format", "mp4"}, {"res", "1080p"}},cpr::Bearer{key ? key : ""},cpr::Timeout{15000});if (r.status_code != 200) {std::cerr << "resolve failed: " << r.status_code << "\n";return 1;}auto video = nlohmann::json::parse(r.text);std::cout << video["title"].get<std::string>() << "\n";}
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.
#include <fstream>std::ofstream out(video["id"].get<std::string>() + ".mp4", std::ios::binary);cpr::Download(out, cpr::Url{video["downloadUrl"].get<std::string>()});
Handle errors and rate limits
cpr::Response::status_code is 0 on network failure and the HTTP status otherwise; r.header["retry-after"] holds the 429 delay. cpr::Download streams straight to the ofstream, so memory stays flat.
| 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. - Link libcurl with TLS support (OpenSSL or Schannel on Windows).
FAQ
Can I use raw libcurl?
Yes — set CURLOPT_HTTPHEADER with the bearer token and a CURLOPT_WRITEFUNCTION that writes to a file.
Related
Last updated 2026-10-25