# Podcast cleanup in a SaaS app: local example

The Node server from the guide at https://diffio.ai/docs/developers/guides/saas-podcast-cleanup/, with a
development-only sign-in for two users (alice and bob), a page with a file input, status and player, and a script that
sends a locally signed webhook. Node 22.13 or later (it uses `node:sqlite`).

## Get the files

```bash
mkdir saas-podcast-cleanup && cd saas-podcast-cleanup
BASE=https://diffio.ai/docs/examples/saas-podcast-cleanup
for f in package.json server.mjs upload.js send-webhook.mjs; do curl -fsSLO "$BASE/$f"; done
curl -fsSL "$BASE/index.html.txt" -o index.html
npm install
```

## Run it

```bash
export DIFFIO_API_KEY="diffio_..."      # server only
# Only with a key from Diffio's development site (dev.diffio.ai); production keys need nothing here.
# export DIFFIO_API_BASE_URL="https://diffio-api-dev.web.app/v2"
export DIFFIO_WEBHOOK_SECRET="whsec_$(node -e 'console.log(require("crypto").randomBytes(24).toString("base64"))')"
node server.mjs                          # http://localhost:3000 (development sign-in on)
```

For local checks any `whsec_` secret works, because `send-webhook.mjs` signs with the same one.

`/dev/login/:user`, `/dev/logout` and `/dev/fake-job` stand in for your real sign-in and exist only while
`NODE_ENV` is not `production`. In your app, `req.session.userId` comes from your own sign-in.

## Check it locally (no upload, no public URL)

A fake job is a job row with no Diffio project behind it, so webhooks can be replayed against it.

```bash
B=http://localhost:3000
curl -s -o /dev/null -w "%{http_code}\n" $B/api/uploads/p1                       # 401: no session
curl -s -c alice.txt -X POST $B/dev/login/alice
curl -s -c bob.txt -X POST $B/dev/login/bob
for j in p1:g1 p2:g2 p3:g3; do
  curl -s -b alice.txt -H 'content-type: application/json' \
    -d "{\"project_id\":\"${j%%:*}\",\"generation_id\":\"${j##*:}\"}" $B/dev/fake-job; echo
done
curl -s -o /dev/null -w "%{http_code}\n" -b bob.txt $B/api/uploads/p1            # 404: alice's job, not bob's
```

| Command | Expected |
| --- | --- |
| `node send-webhook.mjs generation.completed p1 g1 alice evt_a`, then `curl -b alice.txt $B/api/uploads/p1` | `204`; `"status":"complete"` and `"media_error":"project_not_found"` (the fake project does not exist on Diffio; `invalid_api_key` with a wrong key) |
| `node send-webhook.mjs generation.failed p2 g2 alice evt_b`, then the same with `generation.completed ... evt_b` | `204` both times; the second is a duplicate event id, so p2 stays `"failed"` with `no_speech_detected` |
| `node send-webhook.mjs generation.completed p3 g-other alice evt_c` | `204`; p3 stays `"processing"`: another generation of the same project changes nothing |
| `node send-webhook.mjs generation.completed p3 g3 bob evt_d` | `204`; p3 stays `"processing"`: the event names another user |
| `DIFFIO_WEBHOOK_SECRET=whsec_AAAA node send-webhook.mjs generation.completed p3 g3 alice evt_e` | `400`: the signature does not match |

The server log prints `recorded` or `duplicate` for each verified delivery.

## A real upload

Open http://localhost:3000, choose Alice, then pick an audio or video file. The page uploads it straight to Diffio's
media host and polls `GET /api/uploads/<project_id>`. The job turns `complete` when Diffio's `generation.completed`
webhook reaches your server, so real deliveries need a public HTTPS URL:

1. Expose port 3000 with a tunnel (for example `cloudflared tunnel --url http://localhost:3000`).
2. `node server.mjs portal` prints a webhook portal link: add `https://<tunnel>/webhooks/diffio` for
   `generation.completed` and `generation.failed`, then set `DIFFIO_WEBHOOK_SECRET` to that endpoint's secret and
   restart the server.
3. Pick a file. When the webhook arrives, the page shows the job and plays the restored MP3.

Picking the same file again in the same tab reuses its upload key, so it gets the same project and, once its file has
arrived, `upload: null` and no second upload.
