ShortyyDocs

cURL & Shell Scripting Guide

Master interacting with Shortyy via command-line shell scripts, CI/CD runners, and curl commands.

cURL & Shell Scripting Guide

cURL is the easiest way to test endpoints and automate short link generation in GitHub Actions, GitLab CI, or Linux terminal sessions.


Bash Shortener Script (`shorten.sh`)

Save the following script to /usr/local/bin/shorten:

bash
#!/usr/bin/env bash
set -euo pipefail

if [ -z "${1:-}" ]; then
  echo "Usage: shorten <url> [expires_in]"
  exit 1
fi

DEST_URL="$1"
EXPIRES="${2:-2h}"
API_KEY="${SHORTYY_API_KEY:?Error: SHORTYY_API_KEY environment variable is not set}"

RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "https://api.shortyy.com/v1/links" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"url\": \"$DEST_URL\", \"expires_in\": \"$EXPIRES\"}")

HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')

if [ "$HTTP_CODE" -eq 201 ]; then
  SHORT_URL=$(echo "$BODY" | grep -o '"short_url":"[^"]*' | cut -d'"' -f4)
  echo "Success: $SHORT_URL"
else
  echo "Error ($HTTP_CODE): $BODY"
  exit 1
fi