#!/usr/bin/env bash # C1K REST API with curl (API-09). Set these first; never paste a key into a script. # export C1K_API_KEY='c1k_rest_...' # from the Integrations page # export C1K_ORG_ID='org_...' # shown next to the API base URL # Needs the scopes links:read, links:write, analytics:read and events:read. set -euo pipefail : "${C1K_API_KEY:?set C1K_API_KEY}" "${C1K_ORG_ID:?set C1K_ORG_ID}" BASE="https://c1k.me/api/v1/orgs/$C1K_ORG_ID" # --fail-with-body stops the script on an error and still prints the error JSON. C1K=(curl -sS --fail-with-body -H "Authorization: Bearer $C1K_API_KEY") # Who am I, and what may this key do? "${C1K[@]}" "$BASE/me"; echo # Create a link. The Idempotency-Key makes a retry after a lost response safe: # the same key with the same body returns the first result instead of a duplicate. CREATED=$("${C1K[@]}" -H 'Content-Type: application/json' \ -H "Idempotency-Key: example-$(date -u +%Y%m%d%H%M%S)-$RANDOM" \ -d '{"destination_url":"https://venture.example/launch","title":"Launch page","utm":{"source":"newsletter","medium":"email"}}' \ "$BASE/links") echo "$CREATED" LINK_ID=$(printf '%s' "$CREATED" | sed -n 's/.*"id":"\(lnk_[0-9A-Za-z]*\)".*/\1/p') # Edit it. If-Match carries the current version, here 1 (also sent as the ETag header). "${C1K[@]}" -X PATCH -H 'Content-Type: application/json' -H 'If-Match: "1"' \ -d '{"destination_url":"https://venture.example/launch-v2"}' \ "$BASE/links/$LINK_ID"; echo # List links, newest first; pass meta.next_cursor as ?cursor= for the next page. "${C1K[@]}" "$BASE/links?limit=25"; echo # Organization analytics for the last seven UTC days, or an explicit range (to is exclusive). "${C1K[@]}" "$BASE/analytics"; echo "${C1K[@]}" "$BASE/analytics?from=$(date -u -d '6 days ago' +%F 2>/dev/null || date -u -v-6d +%F)&to=$(date -u -d tomorrow +%F 2>/dev/null || date -u -v+1d +%F)"; echo # Poll the change feed. Keep meta.next_cursor and pass it back next time. # A 410 cursor_expired means: take a fresh snapshot, then poll without a cursor. "${C1K[@]}" "$BASE/events"; echo