#!/usr/bin/env bash
# First step for any 401 or 403: ask the API which workspace, mode and scopes your key has. Needs: curl, jq.
set -euo pipefail
BASE="${RELATIONSAI_API_BASE:-https://voice-api.relationsai.com/v1}"

out=$(mktemp)
status=$(curl -sS -o "$out" -w '%{http_code}' "$BASE/workspace" -H "Authorization: Bearer $RELATIONSAI_API_KEY")
if [ "$status" = "200" ]; then
  jq -r '"OK: \(.name), \(.api_key.environment) key \(.api_key.id), scopes: \(.api_key.scopes | join(" "))"' "$out"
else
  jq -r '"HTTP '"$status"' \(.error.code): \(.error.message)\n  request_id: \(.error.request_id)\n  fix: \(.error.doc_url)"' "$out" >&2
  exit 1
fi
