HTTP interface for controlling Apple TVs via pyatv
Project description
pyatv-http
An HTTP interface for controlling Apple TVs, built on top of pyatv.
Features
POST /<name>/turnOnandPOST /<name>/turnOff— checks the Apple TV's current power state and only sends a command if it differs from the desired state.GET /<name>/powerState— reads the current power state without changing it.GET /devices— lists the devices available in the config file.GET /health— unauthenticated liveness check, for load balancers/uptime monitors.- Every other request requires a bearer token, configured as a list of accepted tokens in the config file.
- Config-driven: one TOML file lists the port to listen on, accepted API tokens, and the paired devices.
- Pairing stays out-of-band, via pyatv's own
atvremoteCLI; apyatv-http gen-confighelper turns a paired device's stored credentials into a config snippet.
Setup
1. Find your Apple TV
uvx --from pyatv atvremote scan
This lists every Apple TV on the network along with its identifiers, IP address, and which protocols it supports (AirPlay, Companion, etc).
2. Pair with atvremote
Power control uses the Companion protocol, and pyatv-http talks to the device over AirPlay for the initial handshake, so pair both:
uvx --from pyatv atvremote --id <device-identifier> pair --protocol airplay
uvx --from pyatv atvremote --id <device-identifier> pair --protocol companion
Follow the on-screen PIN prompt for each. Credentials are stored in pyatv's
storage file (default ~/.pyatv.conf).
3. Generate a config entry
uv run pyatv-http gen-config \
--identifier <device-identifier> \
--address <device-ip-or-hostname> \
--key living_room
<device-identifier> and <device-ip-or-hostname> come from the scan
output in step 1; --key is whatever short name you want to use in URLs
(--name sets the human-readable display name too, defaulting to --key).
This prints a [devices.living_room] TOML block built from the stored
pairing. Paste it into your config file (see below). If the identifier
doesn't match any paired device, the command lists the identifiers it does
have on file.
4. Write the config file
port = 8080
[auth]
tokens = ["a-long-random-token"]
[devices.living_room]
name = "Living Room"
identifier = "AA:BB:CC:DD:EE:FF"
address = "10.0.0.5"
[devices.living_room.protocols.airplay]
identifier = "AA:BB:CC:DD:EE:FF"
credentials = "..."
[devices.living_room.protocols.companion]
identifier = "11:22:33:44:55:66"
credentials = "..."
port— port the HTTP server listens on. Optional, defaults to8080.[auth].tokens— required, non-empty list of bearer tokens accepted on every request (see API below). Generate one with e.g.python3 -c "import secrets; print(secrets.token_urlsafe(32))".- The
[devices.<key>]table key (living_roomabove) is the URL path segment used in requests, e.g.POST /living_room/turnOn. identifier— the device's main pyatv identifier (fromatvremote scan).address— the Apple TV's IP address or hostname; used to connect directly instead of relying on mDNS discovery at request time.[devices.<key>.protocols.<protocol>]— one block per paired protocol, exactly as generated bygen-config. Supported protocol names:airplay,companion,dmap,mrp,raop.
5. Run the server
uv run pyatv-http serve --config config.toml
--config is optional; if omitted, it defaults to
$XDG_CONFIG_HOME/pyatv-http/config.toml, falling back to
~/.config/pyatv-http/config.toml when $XDG_CONFIG_HOME isn't set.
By default the server binds 0.0.0.0; pass --host to bind a specific
interface.
API
Every request must include one of the configured tokens as a bearer token:
TOKEN=a-long-random-token
curl -X POST http://localhost:8080/living_room/turnOn \
-H "Authorization: Bearer $TOKEN"
curl -X POST http://localhost:8080/living_room/turnOff \
-H "Authorization: Bearer $TOKEN"
curl http://localhost:8080/living_room/powerState \
-H "Authorization: Bearer $TOKEN"
curl http://localhost:8080/devices \
-H "Authorization: Bearer $TOKEN"
curl http://localhost:8080/health
turnOn/turnOff/powerState return a JSON body:
{"device": "living_room", "power_state": "on"}.
GET /devices returns the devices available in the config file:
[{"device": "living_room", "name": "Living Room"}]
GET /health (no token required) returns {"status": "ok"}.
| Status | Meaning |
|---|---|
| 200 | Command sent (or no-op, if the device was already in that state) |
| 401 | Missing or invalid bearer token |
| 404 | No device configured under that name |
| 504 | Device could not be found/reached on the network |
| 502 | pyatv raised an error while connecting or sending the command |
Interactive API docs
FastAPI auto-generates interactive documentation for the running server:
GET /docs— Swagger UI (use the "Authorize" button to set your bearer token, then try requests directly from the browser).GET /redoc— ReDoc view of the same schema.GET /openapi.json— the raw OpenAPI schema.
These three routes are not themselves behind the bearer-token check.
Notes
- Each request opens a fresh connection to the Apple TV, checks its current
power state, and only sends
turnOn/turnOffif it differs from the desired state — there's no persistent connection or background polling. - Pairing is entirely out-of-band via
atvremote; this project never performs the pairing handshake itself.
Development
uv sync
uv run pytest # or: mise run test
hk check --all
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyatv_http-0.1.2.tar.gz.
File metadata
- Download URL: pyatv_http-0.1.2.tar.gz
- Upload date:
- Size: 98.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eac3d9cbd963f743e4e7988d68ea74b1011392c703d5f00467b22356dd665bbb
|
|
| MD5 |
d16f98168893a17aa1e7d0bdb75c57ee
|
|
| BLAKE2b-256 |
a79eb364e54dd2eac91e33c15d514b98df126dc0deef3e67a071567bde0a4eb1
|
Provenance
The following attestation bundles were made for pyatv_http-0.1.2.tar.gz:
Publisher:
release.yml on hugoh/pyatv-http
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyatv_http-0.1.2.tar.gz -
Subject digest:
eac3d9cbd963f743e4e7988d68ea74b1011392c703d5f00467b22356dd665bbb - Sigstore transparency entry: 2215444754
- Sigstore integration time:
-
Permalink:
hugoh/pyatv-http@0719172abab14eb737468bf7d7d6985d72c9dc0c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hugoh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0719172abab14eb737468bf7d7d6985d72c9dc0c -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyatv_http-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pyatv_http-0.1.2-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9700225e5607c4daae3f6cc25fdeeefc5b2fb7b98491c68d2f241e9d2b6197b
|
|
| MD5 |
66dce57dd7a83df89d0a0fdc53458c02
|
|
| BLAKE2b-256 |
6a5e42bceae2c71117e50d0f862836144035b4ec9aa0932169f78cc2db948f34
|
Provenance
The following attestation bundles were made for pyatv_http-0.1.2-py3-none-any.whl:
Publisher:
release.yml on hugoh/pyatv-http
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyatv_http-0.1.2-py3-none-any.whl -
Subject digest:
d9700225e5607c4daae3f6cc25fdeeefc5b2fb7b98491c68d2f241e9d2b6197b - Sigstore transparency entry: 2215444778
- Sigstore integration time:
-
Permalink:
hugoh/pyatv-http@0719172abab14eb737468bf7d7d6985d72c9dc0c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/hugoh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0719172abab14eb737468bf7d7d6985d72c9dc0c -
Trigger Event:
push
-
Statement type: