# Riffalong connector spec

Version 1, November 2026. From https://riffalong.com/connectors/

Riffalong can add songs from any music service that answers two HTTP requests: a search and a
download. This file is everything you need to build one. It's written to be handed to a coding
agent as it is: "Read this spec and add a Riffalong connector to this project, then check it with
the list at the end."

## How people connect it

In Riffalong, the person opens **Music sources**, picks **Custom service**, and pastes three things:

1. A **search URL**, for example `https://music.example.com/riffalong/search`
2. A **download URL**, for example `https://music.example.com/riffalong/download`
3. An **API key**, if your service wants one (optional)

Riffalong checks them with a search for `test`. Any `results` list, even an empty one, means it
works. The connection is called "Custom service on music.example.com" until the person renames it.
From then on, what they type in Add a song is sent to your search, and the song they pick is
downloaded from your download URL. Riffalong knows nothing else about your service.

## Search

```
GET <search URL>?q=<what the person typed>
```

Any query string the search URL already has is kept, and `q` is added to it. Reply `200` with JSON:

```json
{
  "results": [
    {
      "id": "42",
      "title": "Swing Low",
      "artist": "Wild Blue Country",
      "album": "Sessions",
      "duration": 197,
      "artwork": "https://music.example.com/covers/42.jpg",
      "format": "mp3"
    }
  ]
}
```

| Field      | Required | Meaning                                                        |
|------------|----------|----------------------------------------------------------------|
| `id`       | yes      | A string your download URL understands. Sent back as it is.    |
| `title`    | yes      | The song's title.                                              |
| `artist`   | no       | Who plays it.                                                  |
| `album`    | no       | The album.                                                     |
| `duration` | no       | Length in seconds (a number).                                  |
| `artwork`  | no       | A URL of a cover image.                                        |
| `format`   | no       | The file's extension: `mp3`, `m4a`, `flac`, `wav`…             |

A result without `id` or `title` is skipped. Riffalong shows the first 25 results in your order and
ignores fields it doesn't know.

## Download

```
GET <download URL>?id=<id>
```

- Reply `200` with the audio file as the body. Riffalong names it by `format`, else the file name in
  `Content-Disposition`, else `Content-Type`. MP3 and M4A are the safest choices.
- Reply `202` if the file isn't ready yet. Riffalong asks again after the number of seconds in
  `Retry-After` (3 when there's none), for up to 10 minutes.
- Any other status outside 2xx is an error.

## The API key

If the person gave a key, every request carries:

```
Authorization: Bearer <key>
```

- `401` means the key is wrong or missing, and so does a `403` with no body.
- A `403` whose body is `{"error": "…"}` shows your message to the person instead, for example
  `{"error": "This device is waiting to be allowed on the computer the service runs on."}`.

Riffalong keeps the key on the device and sends it only to the hosts the person typed in. A redirect
to another host is followed without the key.

## Addresses

Both URLs must be one of:

- **HTTPS**, for anything on the internet.
- **Plain HTTP on a local address**: `localhost`, a name ending in `.local`, a name with no dots, or
  an address in `10.0.0.0/8`, `127.0.0.0/8`, `172.16.0.0/12` or `192.168.0.0/16`. Anyone on the same
  network can read plain HTTP, so prefer HTTPS or iroh when you can.
- **iroh**: `iroh://<endpoint id>/<path>`, where the endpoint id is your service's iroh endpoint id
  as 64 hex characters. This works at home and away, encrypted both ways, with no ports to open.

Redirects follow the same rules.

### Serving over iroh

Riffalong finds your endpoint by its id alone, through iroh's default discovery and n0's public
relays, so publish your endpoint the default way. It speaks dumbpipe's protocol:

1. It connects with the ALPN `DUMBPIPEV0` and opens a bidirectional stream.
2. It writes the five bytes `hello`.
3. Then a plain HTTP/1.1 request follows on that stream, exactly as above (same paths, same key,
   same 202s and errors), and your reply goes back on it.

Each Riffalong device has its own iroh identity, and every request over iroh carries
`X-Device-Name` (for example "Sam's iPhone"). To approve devices one by one, answer an unknown device with
`403 {"error": "…"}` until someone at your end allows it.

## A small example

A connector for a folder of audio files, in Python with nothing to install. Run it with
`python3 connector.py ~/Music` (set `CONNECTOR_KEY` to require a key), then connect
`http://<this computer's address>:8080/search` and `http://<this computer's address>:8080/download`.

```python
import json, os, sys, urllib.parse
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

FOLDER = os.path.expanduser(sys.argv[1])
KEY = os.environ.get("CONNECTOR_KEY", "")
AUDIO = (".mp3", ".m4a", ".flac", ".wav")


class Connector(BaseHTTPRequestHandler):
    def do_GET(self):
        url = urllib.parse.urlparse(self.path)
        query = urllib.parse.parse_qs(url.query)
        if KEY and self.headers.get("Authorization") != f"Bearer {KEY}":
            return self.send_error(401)
        if url.path == "/search":
            words = query.get("q", [""])[0].lower()
            names = [n for n in sorted(os.listdir(FOLDER)) if n.lower().endswith(AUDIO) and words in n.lower()]
            results = [{"id": n, "title": os.path.splitext(n)[0], "format": os.path.splitext(n)[1][1:]} for n in names[:25]]
            return self.reply(200, "application/json", json.dumps({"results": results}).encode())
        if url.path == "/download":
            path = os.path.join(FOLDER, os.path.basename(query.get("id", [""])[0]))
            if not os.path.isfile(path):
                return self.send_error(404)
            with open(path, "rb") as file:
                return self.reply(200, "application/octet-stream", file.read())
        self.send_error(404)

    def reply(self, status, kind, body):
        self.send_response(status)
        self.send_header("Content-Type", kind)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)


ThreadingHTTPServer(("0.0.0.0", 8080), Connector).serve_forever()
```

## Check it

- [ ] `curl "<search URL>?q=test"` replies `200` with a `results` list (empty is fine).
- [ ] A search for a song you have returns it with an `id` and a `title`.
- [ ] `curl -o song "<download URL>?id=<that id>"` saves a file that plays, or replies `202` with
      `Retry-After` until it does, within 10 minutes.
- [ ] With a key: no `Authorization` header, or a wrong one, gets `401`.
- [ ] Not on a local network: both URLs are HTTPS or `iroh://`.
- [ ] In Riffalong, **Music sources → Custom service** connects, a search shows your results, and
      adding one brings the song in.

## Not in this version

- Sign-in with a username and password, OAuth, or signed requests.
- Paging, POST searches, or request bodies.
- Tabs. Only audio comes through a connector.
