Recipes

Three complete jobs through the API, tested: a highlight reel from a folder of clips, vertical clips from a long interview, and a video made on a schedule.

Updated

Three complete jobs through the API: a highlight reel from a folder of clips, vertical clips from one long interview, and a video made on a schedule. Each is plain Python with httpx, and each runs as written in our test suite against a local stand-in for the service, so the calls here are the calls that work.

The agent decides how to make each piece. You say what you want in words and what must come back as deliverables; each finished file arrives in the turn's outcome.deliveries[]. See Make an ad through the session API for every field.

Setup

A workspace key with the read, write and run scopes (Credentials).

import os
import httpx

wm = httpx.Client(
    base_url="https://api.wavemaker.io",
    headers={"Authorization": f"Bearer {os.environ['WAVEMAKER_KEY']}"},
    timeout=60,
)

Three helpers the recipes share: upload a file, start a session, and wait for a turn to end.

import hashlib
import pathlib


def upload(project: str, path: pathlib.Path) -> str:
    """Put a local file in the project; returns its sha256 for attachments."""
    data = path.read_bytes()
    sha = hashlib.sha256(data).hexdigest()
    uri = f"wm://file/{project}/uploads/{path.name}"
    mime = "video/mp4" if path.suffix == ".mp4" else "application/octet-stream"
    asked = wm.post("/v1/tools/write", json={
        "project": project, "request_id": f"upload-{sha[:16]}",
        "args": {"uri": uri, "upload": {"size": len(data), "sha256": sha, "mime": mime}},
    }).json()["data"]["upload"]
    httpx.put(asked["url"], content=data, headers=asked["headers"]).raise_for_status()
    done = wm.post("/v1/tools/write", json={
        "project": project, "request_id": f"commit-{sha[:16]}",
        "args": {"uri": uri, "commit": {"upload": asked["id"], "sha256": sha}},
    }).json()
    return done["data"]["sha256"]


def start(title: str, request_id: str) -> dict:
    """A new session; keep its id, project and controller token."""
    response = wm.post("/v1/sessions", json={"request_id": request_id, "title": title})
    return response.json()["data"]


def finish(session: dict, turn: dict) -> dict:
    """Long-poll the session's events until this turn ends; returns the ended turn."""
    after = 0
    while True:
        page = wm.get(f"/v1/sessions/{session['id']}/events",
                      params={"after": after, "wait_s": 25, "turn_id": turn["id"]}).json()["data"]
        for event in page["items"]:
            after = event["seq"]
            if event["type"] in ("turn.completed", "turn.failed", "turn.interrupted"):
                read = wm.get(f"/v1/sessions/{session['id']}/requests/{turn['request_id']}")
                return read.json()["data"]  # its deliveries carry download links
        after = page["next_after"]

Uploading asks for a link, sends the bytes to it, then commits; any size up to 5 GB works the same way. Repeating a call with the same request_id returns its first answer, so a retry after a dropped connection is safe.

1. A highlight reel from a folder of clips

Upload each clip, then post one turn with the clips attached (up to 40) and one deliverable. A note on each attachment says what it is for.

session = start("Spring tournament reel", "reel-session-1")
clips = sorted(pathlib.Path(CLIPS).glob("*.mp4"))
attachments = [
    {"sha256": upload(session["project"], clip), "role": "reference", "note": clip.stem}
    for clip in clips[:40]
]
turn = wm.post(f"/v1/sessions/{session['id']}/turns", json={
    "request_id": "reel-turn-1",
    "controller": session["controller"]["token"],
    "text": "A 60-second highlight reel of the tournament: the best goals and the trophy "
            "at the end, cut to the energy of the play.",
    "attachments": attachments,
    "deliverables": [{"id": "reel", "kind": "video", "aspect": "16:9", "duration_s": 60}],
}).json()["data"]
ended = finish(session, turn)
for delivery in ended["outcome"]["deliveries"]:
    print(delivery["deliverable_id"], delivery["url"], delivery["sha256"])

2. Five vertical clips from one long interview

One upload and a deliverable with a count. The agent reads the whole recording, picks the moments that stand on their own and delivers each clip as it finishes.

session = start("Founder interview clips", "clips-session-1")
interview = upload(session["project"], pathlib.Path(INTERVIEW))
turn = wm.post(f"/v1/sessions/{session['id']}/turns", json={
    "request_id": "clips-turn-1",
    "controller": session["controller"]["token"],
    "text": "Five vertical clips for Reels from this interview, each opening on its "
            "strongest line, with captions.",
    "attachments": [{"sha256": interview, "role": "reference", "note": "the full interview"}],
    "deliverables": [{"id": "clips", "kind": "video", "aspect": "9:16", "count": 5,
                      "duration_s": 45}],
}).json()["data"]
ended = finish(session, turn)
print(len(ended["outcome"]["deliveries"]), "clips")

3. A video every weekday, on a schedule

A routine runs a brief on a schedule as one turn per run, within caps you set: the most one run may spend, and the most a calendar month may spend (both in microcredits, 1,000,000 to a credit). A run that would pass a cap is skipped, not charged. Routines need a live key.

routine = wm.post("/v1/routines", json={
    "title": "Morning market recap",
    "brief": "A 30-second vertical recap of yesterday's market news for our feed.",
    "trigger": {"kind": "schedule", "tz": "America/New_York",
                "weekly": {"time": "07:00", "days": ["mon", "tue", "wed", "thu", "fri"]}},
    "deliverables": [{"id": "recap", "kind": "video", "aspect": "9:16", "duration_s": 30}],
    "cap_run_microcredits": 40_000_000,
    "cap_month_microcredits": 900_000_000,
    "state": "active",
}).json()["data"]
print(routine["id"], routine["next_run_at"])

To hear when each run ends, register a webhook for routine.run.completed. Its data names the routine, the run's status (success, partial, failed or skipped), what it cost and its deliveries.

endpoint = wm.post("/v1/webhooks/endpoints", json={
    "url": WEBHOOK_URL,
    "events": ["routine.run.completed"],
    "description": "Morning recaps",
}).json()["data"]
print(endpoint["id"])

Keep the endpoint's signing secret from this reply and check each delivery's signature (API reference). POST /v1/routines/{id}/run starts a run at once, and PATCH /v1/routines/{id} with {"state": "paused"} stops the schedule.

Related guidance