Metadata-Version: 2.5
Name: diffusr
Version: 0.1.0
Summary: The diffusr API client: make videos, images and music from code. Free price checks, idempotent starts, wait_for, signed callbacks.
Project-URL: Homepage, https://diffusr.ai
Project-URL: Documentation, https://diffusr.ai/docs
License: MIT
License-File: LICENSE
Keywords: ai,api,diffusr,image,video
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# diffusr

The diffusr API client for Python 3.9+. Make videos, images and music from
code. Standard library only. Bodies and answers are the API's own snake_case
JSON (plain dicts).

```sh
pip install diffusr
```

## Quickstart

Use a **test key** (`dfsr_test_…`) while you build: prices are quoted exactly,
nothing is charged, no model runs, and the result is a placeholder file a few
seconds later. Swap in a live key (`dfsr_live_…`) when you are ready to spend.
Mint keys on the Developers page in the app.

```python
from diffusr import Diffusr

diffusr = Diffusr()  # reads DIFFUSR_KEY

# 1. Ask the price. Free: nothing is made, nothing is spent.
quote = diffusr.make_video(
    prompt="A can of sparkling water on a wet street, slow push-in, rain",
    duration_sec=5,
    dry_run=True,
)
print(quote["price"])  # {'credits': 63, 'usd': '0.6300', ...}

# 2. Make it. An Idempotency-Key is added for you and reused on retries,
#    so a dropped connection never pays twice.
job = diffusr.make_video(
    prompt="A can of sparkling water on a wet street, slow push-in, rain",
    duration_sec=5,
)

# 3. Wait for it (polls with backoff; raises WAIT_TIMEOUT if it takes too long).
result = diffusr.wait_for(job["id"], timeout=600)
if result["status"] == "SUCCEEDED":
    print(result["output"]["url"])
else:
    print(result["status"], result.get("error"))
print(result["charge"])  # quoted_credits, charged_credits, final
```

### Callbacks instead of polling

Pass `callback_url` (https, public) and we POST one signed event when the job
ends. Verify it with your signing secret (`diffusr.get_callback_secret()["secret"]`):

```python
from diffusr import Diffusr

# Flask: use the RAW body, not the parsed one.
@app.post("/diffusr")
def hook():
    raw = request.get_data(as_text=True)
    if not Diffusr.verify_callback(raw, request.headers, os.environ["DIFFUSR_WHSEC"]):
        return "", 400
    event = json.loads(raw)  # {id, type: "result.finished", data: <result>}
    return "", 200  # any 2xx; drop repeats of event["id"]
```

`verify_callback` implements [Standard Webhooks](https://www.standardwebhooks.com):
constant-time compare, two signatures after a secret rotation, a 5-minute
timestamp window, `whsec_` prefix handled.

## Methods

| Method | REST |
|---|---|
| `make_video`, `make_image`, `edit_image`, `recast`, `make_music`, `creator_video`, `run_template` `(**body)` | `POST /{tool}` |
| `get_result(id)`, `list_results(limit=)`, `cancel(id)` (stops queued work; a step already generating may finish and be charged: check `result["charge"]`) | `GET /results/{id}`, `GET /results`, `POST /results/{id}/cancel` |
| `wait_for(id, timeout=, interval=, on_poll=)` | polls `GET /results/{id}` |
| `upload_file(path_or_bytes, content_type=)`, `upload_from_url(url)` | `POST /uploads` + signed `PUT` |
| `list_models(kind=)`, `list_templates()` | `GET /models`, `GET /templates` |
| `list_callbacks(limit=, cursor=)`, `retry_callback(event_id)`, `get_callback_secret()`, `rotate_callback_secret()` | `/callbacks…` |
| `Diffusr.verify_callback(raw_body, headers, secret)` | |

`run_template` takes one flat body: `template="<id from list_templates()>"` plus the fields that
template takes (`mode`, `prompt`, `pictures`, `image_urls`, `product_image_url`, `script`, `place`,
`people`, `setup`, `resolution`):

```python
diffusr.run_template(template="tpl-ugc-talking-head", script="Hi!", people=[{"name": "Mia", "image_url": "https://…"}], dry_run=True)
```

`dry_run=True` on any make call answers with the price only. Pass
`idempotency_key="…"` to use your own key.

## Retries and errors

- Every request times out after 60 s (`Diffusr(timeout=...)`) with a `TIMEOUT` error.
- `GET`s (and so `wait_for`) retry on 429, 500, 502, 503, 504, network errors
  and timeouts with backoff, honouring `Retry-After` (waits over 30 s are not
  slept: the error is raised).
- Make calls retry on 429, 503, network errors and timeouts only, **with the
  same Idempotency-Key**.
- Every failure is a `DiffusrError`:

```python
from diffusr import DiffusrError

try:
    diffusr.make_video(prompt="…")
except DiffusrError as e:
    print(e.status, e.code, e.message, e.request_id, e.retry_after)
    # e.body has the extras: issues, field, budget…
```

Codes are the API's (`RATE_LIMITED`, `BUDGET_EXCEEDED`, `INSUFFICIENT_CREDITS`…).
The SDK adds `NETWORK_ERROR`, `TIMEOUT`, `WAIT_TIMEOUT`, `UPLOAD_FAILED`,
`CONTENT_TYPE_REQUIRED` and `MISSING_API_KEY`. Handle codes you don't know.

## Docs

Full API reference, prices and agent guides: https://diffusr.ai/docs

MIT licensed.
