Files
unshackle/docs/dev/rest-api/index.md
T

244 lines
11 KiB
Markdown

# Server Overview
`unshackle serve` starts an HTTP server. The server can give the download engine of the
command-line tool as a REST API, and your CDM devices to other machines. The
[server modes](#server-modes) select which of the two it gives.
This page is for the operator of the server. The other pages of this section tell you how
to set the [API keys](authentication.md), how to watch the server from the
[dashboard](dashboard.md), and how a [`dl --remote` client](../../guide/remote-sessions.md)
connects to it.
!!! info "Pages for client developers"
To write a client for the REST API, start with the [REST API Quick Start](quickstart.md).
[Where to go next](#where-to-go-next) lists the other client pages. To download a title,
you do not need the server: the [command-line tool](../../guide/cli-reference.md) is simpler.
## What the API is for
The server exposes the same capabilities as the `dl` command, but as discrete HTTP
calls you can script against:
- **Discovery**: show the services available on the server, use a service's search,
and enumerate the titles and tracks behind a title ID.
- **Downloading**: submit a download as a background *job*, then follow its status and
live progress (by polling, or by subscribing to its Server-Sent Events stream),
cancel it, retry it, or bump it to the front of the queue.
- **History and housekeeping**: read the log of finished jobs, inspect the server's
effective (redacted) configuration, examine external tool versions, and clear caches
or temp files.
- **Remote sessions**: a thin local client can authenticate against a
service *on the server*, then pull titles, tracks, and segment info back, and relay
DRM licensing. This is how one machine with the credentials and CDMs can serve
downloads to another. See the [remote session protocol](remote-sessions.md).
Because every download runs as a queued job in a separate job worker process, the API is
well suited to running unattended: submit work, come back later, and read the result.
## Running the server
Start the server with the `serve` command:
```shell title="Start the REST API on the default host and port"
unshackle serve
```
By default the server binds to `127.0.0.1:8786` and requires an API key on every
request. It also serves the local Widevine and PlayReady CDM endpoints (for
pywidevine / pyplayready clients) in addition to the REST API.
!!! warning "An API key is required by default"
Unless you pass `--no-key`, the server needs an API secret configured under the
`serve` section of your config. If none is set, `unshackle serve` refuses to
start. See [Authentication](authentication.md) for how to set `api_secret` and
how the `X-Secret-Key` header works.
### Common options
The `serve` command accepts the following flags:
| Option | Default | Effect |
|---|---|---|
| `-h`, `--host` | `127.0.0.1` | Address to bind to. Use `0.0.0.0` to accept connections from other machines. |
| `-p`, `--port` | `8786` | Port to bind to. |
| `--api-only` | off | Serve only the REST API, without the Widevine/PlayReady CDM HTTP endpoints. |
| `--cdm-only` | off | Serve only the Widevine/PlayReady CDM HTTP endpoints, plus `/api/health` and the dashboard. No REST API, no remote sessions, no Swagger UI. Cannot be combined with `--api-only` or `--remote-only`. |
| `--no-widevine` | off | Disable the Widevine CDM endpoints. Cannot be combined with `--api-only`. |
| `--no-playready` | off | Disable the PlayReady CDM endpoints. Cannot be combined with `--api-only`. |
| `--no-key` | off | Disable API-key authentication entirely; every request is allowed. |
| `--remote-only` | off | Expose only the remote-session subset of routes. Implies `--api-only`. |
| `--debug-api` | off | Include tracebacks and worker stderr in error responses. |
| `--debug` | off | Enable debug-level logging. |
| `--caddy` | off | Also launch [Caddy](https://caddyserver.com/) as a reverse proxy, using the `Caddyfile` in your user-config directory. |
```shell title="Serve the REST API only, on all interfaces"
unshackle serve --api-only --host 0.0.0.0 --port 8786
```
!!! danger "Binding to a public interface"
`--no-key` disables authentication completely. Anyone who can reach the port can
find titles, download them, and read your configuration. Only combine it with a host like
`127.0.0.1`, or keep the default key-based auth when binding to `0.0.0.0`. Put a
TLS-terminating reverse proxy (Caddy, nginx) in front for anything internet-facing.
### Server modes
The routes the server mounts depend on how you start the server:
=== "Default (CDM + REST)"
The server mounts both the pywidevine/pyplayready CDM endpoints and the full REST API.
This is the mode you get when you operate `unshackle serve` with no mode flags.
=== "`--api-only`"
The server mounts only the REST API. It does not mount the CDM HTTP endpoints.
Authentication uses the single configured API key (or none, with `--no-key`).
=== "`--remote-only`"
The server mounts only the remote-session subset of routes (health, services, search,
and everything under `/api/session/*`). This mode implies `--api-only`, and the
interactive Swagger UI is **not** mounted.
=== "`--cdm-only`"
The server mounts only the pywidevine and pyplayready CDM endpoints. Under `/api/` it
answers `/api/health` and, when `serve.dashboard.key` is set, the dashboard. It does not
start the remote session store, the service refresh loop or the Swagger UI. Use it for a
CDM backend that a proxy reaches on its own hostname.
## Base URL
All REST endpoints live under the `/api` prefix. With the default bind address the
base URL is:
```
http://127.0.0.1:8786/api
```
Every path in this documentation is relative to the host and port. For example, the
health check is `GET http://127.0.0.1:8786/api/health`.
!!! tip "Interactive API docs"
Unless you started the server with `--remote-only`, an interactive Swagger UI is
available at [`/api/docs/`](http://127.0.0.1:8786/api/docs/). The server generates
it from the same route table it serves, so it always matches the version you
operate. Note that in `--api-only` mode the docs page is itself behind the
API key, like every other route.
A quick smoke test:
```shell title="Confirm the server is up"
curl -H "X-Secret-Key: your-api-secret" http://127.0.0.1:8786/api/health
```
```json title="Response"
{
"status": "ok",
"version": "5.3.0",
"update_check": {
"update_available": false,
"current_version": "5.3.0",
"latest_version": "5.3.0"
}
}
```
## Endpoint map
Every REST path uses JSON request and response bodies and lives under `/api`. The
table below is a map of the surface. The [Endpoints](endpoints.md) page gives each
request and response in full.
### Discovery
| Method | Path | Purpose |
|---|---|---|
| `GET` | `/api/health` | Liveness and version check. |
| `GET` | `/api/services` | List the services available on this server. |
| `POST` | `/api/search` | Run a service's search for a query. |
| `POST` | `/api/list-titles` | List the titles behind a service title ID. |
| `POST` | `/api/list-tracks` | List the video, audio, and subtitle tracks for a title. |
### Downloads and jobs
| Method | Path | Purpose |
|---|---|---|
| `POST` | `/api/download` | Queue a download; returns a `job_id`. |
| `GET` | `/api/download/jobs` | List jobs, with filtering and sorting. |
| `POST` | `/api/download/jobs/clear-finished` | Remove all finished jobs. |
| `GET` | `/api/download/jobs/{job_id}` | Full detail for one job. |
| `GET` | `/api/download/jobs/{job_id}/events` | Stream one job's progress as Server-Sent Events. |
| `DELETE` | `/api/download/jobs/{job_id}` | Cancel a running job, or remove a finished one. |
| `POST` | `/api/download/jobs/{job_id}/retry` | Re-queue a finished job's parameters as a new job. |
| `POST` | `/api/download/jobs/{job_id}/priority` | Move a queued job to the front. |
| `POST` | `/api/download/jobs/{job_id}/input` | Answer the prompt that a running job waits on. |
### History and server info
| Method | Path | Purpose |
|---|---|---|
| `GET` | `/api/history` | Read the persisted log of finished jobs. |
| `DELETE` | `/api/history/{job_id}` | Delete a history entry. |
| `GET` | `/api/profiles` | Named credential profiles per service. |
| `GET` | `/api/config` | The server's effective, redacted configuration. |
| `GET` | `/api/env/check` | Presence and versions of external tools. |
### Maintenance
| Method | Path | Purpose |
|---|---|---|
| `POST` | `/api/maintenance/clear-cache` | Clear the cache directory. |
| `POST` | `/api/maintenance/clear-temp` | Clear the temp directory. |
| `POST` | `/api/maintenance/refresh-services` | `git pull` each configured service repository and re-import the services that changed. |
### Remote sessions
The `/api/session/*`, `/api/tunnel` and `/api/tunnel/check` routes carry the remote sessions of
`dl --remote`. The [remote session protocol](remote-sessions.md#routes) lists each route.
| Method | Path | Purpose |
|---|---|---|
| `GET` | `/api/remote/logins` | The logins of a [relayed](endpoints.md#remote-services-relay) list call that wait on you. |
| `POST` | `/api/remote/logins/{login_id}/input` | Answer the prompt of such a login. |
!!! note "Remote-only routes"
A server that runs with `--remote-only` exposes only the
[remote session routes](remote-sessions.md#routes) and the dashboard routes. Everything else (downloads, jobs,
history, config, maintenance) is only available in the full or `--api-only` modes.
## Authentication in one line
With the default configuration, send your API secret in the `X-Secret-Key` header on
every request:
```shell title="An authenticated request"
curl -X POST http://127.0.0.1:8786/api/search \
-H "X-Secret-Key: your-api-secret" \
-H "Content-Type: application/json" \
-d '{"service": "EXAMPLE", "query": "example"}'
```
A missing or unknown API key returns `401`. [Authentication](authentication.md) gives
the full model, including per-key service allowlists.
## Where to go next
<div class="grid cards" markdown>
- **[Quick Start](quickstart.md)**: start the server, set an API key, and operate your
first end-to-end download over the API.
- **[Authentication](authentication.md)**: the `X-Secret-Key` header, `--no-key`,
per-key service allowlists, and CORS.
- **[Endpoints](endpoints.md)**: every request body, query parameter, and response
shape, endpoint by endpoint.
- **[Job Lifecycle](job-lifecycle.md)**: how a download moves from `queued` to a
terminal state, live progress, cancellation, and retries.
- **[Remote Session Protocol](remote-sessions.md)**: the routes, login states,
licensing modes and message shapes of a remote session.
- **[Error Responses](errors.md)**: the standard error envelope, error codes, and
their HTTP statuses.
</div>