---
title: Wait for the result
description: "Poll an analysis until it finishes, fails or is cancelled."
url: "https://mediaseer.ai/docs/guides/wait/"
---

# Wait for the result

Ask for the analysis with [getVodAnalysis](https://mediaseer.ai/docs/api/operations/getvodanalysis/) until it is `FINISHED`, `FAILED` or `CANCELLED`. See [lifecycle](https://mediaseer.ai/docs/lifecycle/) for the states.

An accepted start also returns this status resource in the `Location` header. Resolve the URI reference against that request’s URL and authenticate the status request as usual. The response body still contains the analysis ID; the web app, CLI and local MCP server continue polling by that ID.

Set `MEDIASEER_API_URL`, `MEDIASEER_API_KEY` and `ANALYSIS_ID` as described in the [quickstart](https://mediaseer.ai/docs/guides/quickstart/#prepare). The script exits with `0` for `FINISHED`, or `1` for failure, cancellation or timeout.

The examples keep credentials out of curl’s command line and ignore your curl configuration.

```bash title="wait.sh"
#!/usr/bin/env bash
#
# Asks for the analysis ANALYSIS_ID until it is final, at the CLI's interval and up to its timeout. Exits
# 0 when it is FINISHED, 1 when it is FAILED or CANCELLED or the timeout has passed.
set -euo pipefail

base="${MEDIASEER_API_URL:?the API origin, without /api/v1}"
base="${base%/}/api/v1"
: "${MEDIASEER_API_KEY:?your API key}"
id="${ANALYSIS_ID:?the id of an analysis}"

api() { curl -q -sS -H @- "$@" <<<"X-Api-Key: $MEDIASEER_API_KEY"; }

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

POLL_SECONDS=10
TIMEOUT_SECONDS=1800

deadline=$((SECONDS + TIMEOUT_SECONDS))
previous=""
while true; do
  # No request outlasts the timeout: each one gets the time that is left.
  left=$((deadline - SECONDS))
  if ((left <= 0)); then
    echo "still ${previous:-unknown} at the timeout; the analysis goes on without us" >&2
    exit 1
  fi
  status="$(api --max-time "$left" -o "$tmp/analysis.json" -w '%{http_code}' "$base/vod-analysis/$id")" || {
    echo "get failed or timed out: curl exit $?" >&2
    exit 1
  }
  if [[ "$status" != 200 ]]; then
    echo "get answered $status $(jq -r '.code // empty' "$tmp/analysis.json" 2>/dev/null || true)" >&2
    exit 1
  fi
  state="$(jq -r .state "$tmp/analysis.json")"
  if [[ "$state" != "$previous" ]]; then
    echo "state: $state"
    previous="$state"
  fi
  case "$state" in
    FINISHED) exit 0 ;;
    FAILED | CANCELLED)
      jq -r '.error.message // empty' "$tmp/analysis.json" >&2
      exit 1
      ;;
  esac
  if ((SECONDS + POLL_SECONDS > deadline)); then
    echo "still $state at the timeout; the analysis goes on without us" >&2
    exit 1
  fi
  sleep "$POLL_SECONDS"
done
```

## How often to ask

The script polls every 10 seconds and waits up to 30 minutes. An analysis keeps running after you stop waiting.

Mediaseer does not send webhook notifications to your code; poll for changes.
