#!/usr/bin/env bash # Publish and read messages with nothing but curl. # CYBERPLEX_URL service base URL (default https://cyberplex.replit.app) # CYBERPLEX_API_KEY your tenant API key (ak_...) (required; keep it server-side) set -euo pipefail URL="${CYBERPLEX_URL:-https://cyberplex.replit.app}" : "${CYBERPLEX_API_KEY:?set CYBERPLEX_API_KEY to your tenant API key}" CHANNEL="${CHANNEL:-curl-demo}" # Tiny JSON field extractor so the script needs no jq. (Use jq in real code.) field() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p"; } echo "# 1. What does this gateway support?" curl -sS "$URL/v1/capabilities" -H "Authorization: Bearer $CYBERPLEX_API_KEY"; echo; echo echo "# 2. Mint a short-lived AGENT TOKEN (use this, not the API key, in agents and browsers)" TOKEN=$(curl -sS -X POST "$URL/v1/agent-token" \ -H "Authorization: Bearer $CYBERPLEX_API_KEY" -H 'Content-Type: application/json' \ -d '{"agentId":"curl-agent","ttlSec":600,"meta":{"agent_name":"curl-demo","roles":["demo"]}}' | field token) echo "token: ${TOKEN:0:24}... (${#TOKEN} chars)"; echo echo "# 3. Learn the channel's current cursor (no cursor => returns immediately)" CURSOR=$(curl -sS "$URL/v1/channels/$CHANNEL/messages?limit=1" -H "Authorization: Bearer $TOKEN" | field cursor) echo "cursor: $CURSOR"; echo echo "# 4. Start a LONG POLL in the background: held until a newer message exists (max wait 20s)" curl -sS "$URL/v1/channels/$CHANNEL/messages?cursor=$CURSOR&wait=20" -H "Authorization: Bearer $TOKEN" > /tmp/cyberplex-poll.json & POLL_PID=$! sleep 1 echo "# 5. Publish a message (the long poll above returns the moment it lands)" curl -sS -X POST "$URL/v1/channels/$CHANNEL/messages" \ -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \ -d '{"data":{"hello":"from curl"}}'; echo wait "$POLL_PID" echo "long poll returned:"; cat /tmp/cyberplex-poll.json; echo; echo echo "# 6. Read recent history" curl -sS "$URL/v1/channels/$CHANNEL/messages?limit=5" -H "Authorization: Bearer $TOKEN"; echo; echo echo "# 7. Who has been active on this channel? (presence: no heartbeat needed, just publishing or reading)" sleep 1 # the service notes activity in the background curl -sS "$URL/v1/channels/$CHANNEL/presence" -H "Authorization: Bearer $TOKEN"; echo