|
| 1 | +# audd |
| 2 | + |
| 3 | +[](https://github.com/AudDMusic/audd-python/actions/workflows/ci.yml) |
| 4 | +[](https://github.com/AudDMusic/audd-python/actions/workflows/contract.yml) |
| 5 | +[](https://pypi.org/project/audd/) |
| 6 | +[](https://pypi.org/project/audd/) |
| 7 | + |
| 8 | +Official Python SDK for the [AudD](https://audd.io) music recognition API. |
| 9 | + |
| 10 | +## Quickstart |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install audd |
| 14 | +``` |
| 15 | + |
| 16 | +```python |
| 17 | +from audd import AudD |
| 18 | + |
| 19 | +audd = AudD(api_token="test") # use your token from https://dashboard.audd.io |
| 20 | +result = audd.recognize("https://audd.tech/example.mp3") |
| 21 | +if result: |
| 22 | + print(f"{result.artist} — {result.title}") |
| 23 | +``` |
| 24 | + |
| 25 | +## Capabilities |
| 26 | + |
| 27 | +| What | How | |
| 28 | +|---|---| |
| 29 | +| Recognize a short clip (≤25s) | `audd.recognize(source)` | |
| 30 | +| Recognize a long file (hours, days) | `audd.recognize_enterprise(source, limit=...)` | |
| 31 | +| Manage stream recognition (set callback, longpoll for events) | `audd.streams.*` | |
| 32 | + |
| 33 | +`source` accepts a URL, a file path, a file-like object, or raw bytes — auto-detected. |
| 34 | + |
| 35 | +## Async |
| 36 | + |
| 37 | +Use `AsyncAudD` instead — same surface: |
| 38 | + |
| 39 | +```python |
| 40 | +from audd import AsyncAudD |
| 41 | + |
| 42 | +async def main(): |
| 43 | + audd = AsyncAudD(api_token="test") |
| 44 | + try: |
| 45 | + result = await audd.recognize("https://audd.tech/example.mp3") |
| 46 | + print(result) |
| 47 | + finally: |
| 48 | + await audd.aclose() |
| 49 | +``` |
| 50 | + |
| 51 | +## Errors |
| 52 | + |
| 53 | +Every server error becomes a typed exception: |
| 54 | + |
| 55 | +```python |
| 56 | +from audd import AudD, AudDAuthenticationError, AudDSubscriptionError |
| 57 | + |
| 58 | +try: |
| 59 | + AudD(api_token="bad").recognize("https://x.mp3") |
| 60 | +except AudDAuthenticationError as e: |
| 61 | + print(f"check your token: {e.error_code} {e.message}") |
| 62 | +except AudDSubscriptionError: |
| 63 | + print("this endpoint isn't enabled on your token") |
| 64 | +``` |
| 65 | + |
| 66 | +The full hierarchy is documented in [`src/audd/errors.py`](src/audd/errors.py). Every `AudDAPIError` carries `error_code`, `message`, `http_status`, `request_id`, `requested_params`, `request_method`, `branded_message`, and `raw_response`. |
| 67 | + |
| 68 | +## Forward compatibility |
| 69 | + |
| 70 | +Models accept and round-trip unknown server fields via `model_extra`: |
| 71 | + |
| 72 | +```python |
| 73 | +result = audd.recognize("https://example.mp3", return_=["apple_music"]) |
| 74 | +print(result.apple_music.url) # typed |
| 75 | +print(result.model_extra) # any other unknown fields |
| 76 | +``` |
| 77 | + |
| 78 | +If AudD adds a new metadata block tomorrow (e.g., `tidal`), you can read it as `result.model_extra["tidal"]` *today* — no SDK release needed. The next SDK release adds the typed `tidal` field, and both paths keep working. |
| 79 | + |
| 80 | +## Streams |
| 81 | + |
| 82 | +Manage real-time stream recognition (set callback, longpoll for events): |
| 83 | + |
| 84 | +```python |
| 85 | +audd.streams.set_callback_url("https://your.server/cb") |
| 86 | +audd.streams.add("https://your.stream.url", radio_id=42) |
| 87 | +for event in audd.streams.list(): |
| 88 | + print(event) |
| 89 | +``` |
| 90 | + |
| 91 | +### Receiving events without exposing your token |
| 92 | + |
| 93 | +For browser widgets and other contexts where shipping the api_token would leak it, |
| 94 | +derive a `category` server-side and share that with the consumer: |
| 95 | + |
| 96 | +```python |
| 97 | +from audd import LongpollConsumer |
| 98 | + |
| 99 | +# `category` is derived server-side via AudD(...).streams.derive_longpoll_category(radio_id), |
| 100 | +# then shared with the browser/widget. The consumer carries no api_token. |
| 101 | +consumer = LongpollConsumer(category="abc123def") |
| 102 | +for event in consumer.iterate(timeout=30): |
| 103 | + print(event) |
| 104 | +``` |
| 105 | + |
| 106 | +## Configuration |
| 107 | + |
| 108 | +```python |
| 109 | +import httpx |
| 110 | +from audd import AudD |
| 111 | + |
| 112 | +audd = AudD( |
| 113 | + api_token="...", |
| 114 | + max_retries=3, # retry budget per call |
| 115 | + backoff_factor=0.5, # initial backoff seconds (jittered) |
| 116 | + httpx_client=httpx.Client(proxies="http://corp-proxy:8080"), |
| 117 | +) |
| 118 | +``` |
| 119 | + |
| 120 | +Default timeouts: 30s connect / 60s read for standard endpoints, 30s connect / **1 hour** read for the enterprise endpoint. Pass `timeout=` per call to override. |
| 121 | + |
| 122 | +**Concurrency:** `AudD` and `AsyncAudD` are safe for concurrent use — share one instance across threads or asyncio tasks. `set_api_token(...)` rotates the token safely; in-flight requests continue with the old token, subsequent requests use the new one. |
| 123 | + |
| 124 | +## Custom catalog (advanced — not for music recognition) |
| 125 | + |
| 126 | +> ⚠ **The custom-catalog endpoint is NOT how you submit audio for music recognition.** |
| 127 | +> For recognition, use `recognize()` or `recognize_enterprise()`. The custom-catalog |
| 128 | +> endpoint adds songs to your private fingerprint database for *your* account. |
| 129 | +> Requires special access — contact api@audd.io if you need it. |
| 130 | +
|
| 131 | +```python |
| 132 | +audd.custom_catalog.add(audio_id=42, source="https://my.song.mp3") |
| 133 | +``` |
| 134 | + |
| 135 | +## Advanced |
| 136 | + |
| 137 | +For endpoints not yet wrapped by typed methods on this SDK, use the raw-request escape hatch: |
| 138 | + |
| 139 | +```python |
| 140 | +raw = audd.advanced.raw_request("someNewMethod", {"q": "x"}) |
| 141 | +``` |
| 142 | + |
| 143 | +## Spec contract |
| 144 | + |
| 145 | +This SDK builds against the [`audd-openapi`](https://github.com/AudDMusic/audd-openapi) spec. The contract tests in `tests/contract/` validate the parser against the canonical fixture set on every push, on a daily cron, and on every spec update. |
| 146 | + |
| 147 | +## License |
| 148 | + |
| 149 | +MIT — see [LICENSE](./LICENSE). |
| 150 | + |
| 151 | +## Support |
| 152 | + |
| 153 | +- Documentation: https://docs.audd.io |
| 154 | +- Tokens: https://dashboard.audd.io |
| 155 | +- Email: api@audd.io |
0 commit comments