# How to use yt-dlp as an API

yt-dlp has no HTTP API. Your real options: embed it as a Python library, wrap it in your own service, or use a managed media-import API. Honest trade-offs for each.

yt-dlp is a command-line tool and a Python library — it has no official HTTP API. If your app, backend, or automation needs to turn a video URL into a file programmatically, you have four real options, each with different costs. This guide covers all four honestly, plus what to check in a hosted API before you ship it.

<HostedAnswer>
yt-dlp has no HTTP API. In Python, import it as a library; from any other language, shell out to the CLI or wrap it in a small service of your own (all three below).
</HostedAnswer>

## Quick answer

- **Python project?** Import yt-dlp directly as a library — no API needed.
- **Other language, low volume?** Shell out to the CLI and parse `--dump-json` output.
- **Production service, real volume?** Either build and babysit your own wrapper service, or use a [managed media-import API](https://www.importly.io/?utm_source=ytdlp.org&utm_medium=referral&utm_campaign=doc-cta&utm_content=api-guide-quick-answer&utm_term=%2Fguides%2Fyt-dlp-api) and skip the maintenance.

## Option 1: Embed yt-dlp as a Python library

If your code is Python, you do not need an API at all. yt-dlp is on PyPI and is designed to be imported:

```python
import yt_dlp

opts = {
    "format": "bestvideo*+bestaudio/best",
    "outtmpl": "%(uploader)s/%(title)s.%(ext)s",
}

with yt_dlp.YoutubeDL(opts) as ydl:
    info = ydl.extract_info(url, download=True)
    print(info["title"], info["duration"])
```

Use `download=False` to fetch metadata only. The [yt-dlp Python guide](/guides/yt-dlp-python) covers the options dict, the downloaded file path, progress hooks and error handling. This is the cleanest integration — full access to formats, progress hooks, and postprocessors. The catch: your process now owns yt-dlp's failure modes (extractor breakage, IP blocks, updates), and downloads are long-running and CPU/network heavy, so you will end up adding a job queue anyway.

## Option 2: Shell out to the CLI from any language

From Node, Go, Rust, PHP, or anything else, spawn the binary. The flag that makes this workable is `-J` / `--dump-json`, which prints machine-readable metadata instead of downloading:

```bash
# Metadata only, as JSON
yt-dlp -J "URL"

# Download and print the final filepath for your program to pick up
yt-dlp -o "%(id)s.%(ext)s" --print after_move:filepath "URL"
```

Watch out for: non-zero exit codes on partial playlist failures, output that mixes progress and warnings on stderr, and the yt-dlp binary needing regular updates on whatever machine runs this. Fine for internal tools and low volume; brittle as a product dependency.

## Option 3: Wrap it in your own HTTP service

The classic build: a small FastAPI/Express app that accepts a URL, runs yt-dlp in a worker, and returns a file or a storage link. A minimal sketch:

```python
from fastapi import FastAPI
import yt_dlp

app = FastAPI()

@app.post("/import")
def import_media(url: str):
    with yt_dlp.YoutubeDL({"outtmpl": "/data/%(id)s.%(ext)s"}) as ydl:
        info = ydl.extract_info(url, download=True)
    return {"id": info["id"], "title": info["title"]}
```

That sketch works in a demo and falls over in production. Before trusting it, you need: a job queue (downloads take minutes), disk cleanup, concurrency limits, retry logic, a strategy for [datacenter IP blocks](/guides/run-yt-dlp-server), cookie management for gated content, and a cron job updating yt-dlp before extractor breakage takes you down. Budget for ongoing maintenance, not a weekend project.

## Option 4: Use a managed media-import API

If the download capability is part of a product — a SaaS feature, an AI pipeline, a no-code automation — the honest comparison is build-vs-buy. A managed API like [Importly](https://www.importly.io/?utm_source=ytdlp.org&utm_medium=referral&utm_campaign=doc-cta&utm_content=api-guide-inline&utm_term=%2Fguides%2Fyt-dlp-api) does the same job as the wrapper service above — public link in, usable media out — but the extractor updates, server-side blocks, rate limits, and retries are someone else's pager. You get webhook or direct-to-S3 delivery instead of managing disks. The trade-off is a per-import cost and an external dependency, which is why self-hosting still makes sense for personal tools and unusual requirements.

<AppCodeTabs />

## What to check in a hosted download API before you ship

Most hosted download APIs work in a demo. The ones that fail tend to fail a few weeks after launch, when the site changes something or your traffic moves to production servers. Check these before you build on one:

- **Test from your production host, not your laptop.** Run a dozen real links from the same cloud region your app runs in. Datacenter IPs get blocked first, so a laptop test tells you little.
- **Errors you can act on.** A private, removed or login-only video should come back as a specific error code you can show the user, not a generic 500 you retry forever.
- **Failed jobs cost nothing.** If the provider charges for failures, a bad week on their side becomes your bill.
- **Webhooks for failures too.** You want to hear when a job fails, not find out from a customer.
- **A cost cap per request.** Users paste three-hour streams. You should be able to reject those before anything downloads.
- **A file, not a direct YouTube link.** Many download APIs hand back a `googlevideo.com` URL. It expires within hours and is often tied to the IP that requested it, so your server gets a 403 when it fetches it later or from another machine. [Why those links 403 and how to check](https://www.importly.io/blog/youtube-download-api-link-403?utm_source=ytdlp.org&utm_medium=referral&utm_campaign=doc-cta&utm_content=api-guide-link-403&utm_term=%2Fguides%2Fyt-dlp-api).
- **Delivery to your storage.** A short-lived link is fine for a preview; for a product you usually want the file in your own bucket.
- **Signs it's maintained.** A public changelog with recent fixes for the sites you care about, and a provider that answers email. A marketplace listing with no changelog is the pattern that dies quietly.

For reference, [Importly](https://www.importly.io/?utm_source=ytdlp.org&utm_medium=referral&utm_campaign=doc-cta&utm_content=api-guide-checklist&utm_term=%2Fguides%2Fyt-dlp-api) (from the people behind this site) returns error codes like `VIDEO_PRIVATE` and `AUTH_REQUIRED`, doesn't charge for failed imports, sends a webhook on success or failure, takes a `maxCostDollars` cap, can copy files into your S3, R2 or B2 bucket, and keeps a public changelog. It's free to try, no card required. Hold any provider, including that one, to the same checks.

<ImportDemo eyebrow="For builders" title="Using yt-dlp inside an app or script?" subtitle="Paste a link to see what a managed media-import API returns — no install, no signup." />

## Which option fits your case

- personal scripts and internal tools → library or CLI, keep it simple
- Python app, modest volume, tolerant of breakage → library + a job queue
- product feature, paying users, or AI pipeline at volume → managed API, or a wrapper service with real ops investment
- anything running on cloud IPs at scale → read the server guide first; IP blocking changes the math

<Faq />

## Related guides

- [Use yt-dlp in Python (YoutubeDL API)](/guides/yt-dlp-python)
- [Run yt-dlp on a server](/guides/run-yt-dlp-server)
- [Automate yt-dlp: cron, n8n, Zapier](/guides/yt-dlp-automation)
- [Batch download videos](/guides/batch-download)
- [yt-dlp alternatives compared](/guides/yt-dlp-alternatives)
- [Run yt-dlp in GitHub Actions](/guides/yt-dlp-github-actions)
- [yt-dlp vs cobalt](/guides/yt-dlp-vs-cobalt)