cURL 範例

使用 cURL 在命令列呼叫 Boring API 的各種範例。

基本設定

export BORING_API_KEY="boring_xxxxxxxxxxxxx"

最簡請求

curl -X POST https://boring.aiagent-me.com/v2/posts \
  -H "boring-api-key: $BORING_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "post": {
      "accountId": "your-account-id",
      "content": {
        "text": "Hello from cURL!",
        "mediaUrls": [],
        "platform": "facebook"
      },
      "target": {
        "targetType": "facebook"
      }
    }
  }'

使用 JSON 檔案

request.json

{
  "post": {
    "accountId": "your-account-id",
    "content": {
      "text": "Hello from cURL!",
      "mediaUrls": [],
      "platform": "facebook"
    },
    "target": {
      "targetType": "facebook"
    }
  }
}

呼叫:

curl -X POST https://boring.aiagent-me.com/v2/posts \
  -H "boring-api-key: $BORING_API_KEY" \
  -H "Content-Type: application/json" \
  -d @request.json

Facebook 範例

curl -X POST ... -d '{
  "post": {
    "accountId": "fb-account-id",
    "content": {
      "text": "Hello from Boring API! 🚀",
      "mediaUrls": [],
      "platform": "facebook"
    },
    "target": {"targetType": "facebook"}
  }
}'

Instagram 範例

curl -X POST ... -d '{
  "post": {
    "accountId": "ig-account-id",
    "content": {
      "text": "New carousel! ➡️",
      "mediaUrls": [
        "https://example.com/photo1.jpg",
        "https://example.com/photo2.jpg"
      ],
      "platform": "instagram"
    },
    "target": {"targetType": "instagram"}
  }
}'

Threads 範例

curl -X POST ... -d '{
  "post": {
    "accountId": "threads-account-id",
    "content": {
      "text": [
        "🧵 Part 1: Planning",
        "Part 2: Execution",
        "Part 3: Metrics"
      ],
      "mediaUrls": [],
      "platform": "threads"
    },
    "target": {"targetType": "threads"}
  }
}'

X(Twitter)範例

curl -X POST ... -d '{
  "post": {
    "accountId": "x-account-id",
    "content": {
      "text": "Launch day 🚀 #startup",
      "mediaUrls": [],
      "platform": "x"
    },
    "target": {"targetType": "x"}
  }
}'

YouTube 範例

curl -X POST ... -d '{
  "post": {
    "accountId": "yt-account-id",
    "content": {
      "text": "Product Tour 2025\n\nFull walkthrough with #hashtag",
      "mediaUrls": [
        "https://example.com/video.mp4",
        "https://example.com/thumbnail.jpg"
      ],
      "platform": "youtube"
    },
    "target": {"targetType": "youtube"}
  }
}'

使用 jq 處理回應

curl -s -X POST ... | jq

提取貼文 ID:

post_id=$(curl -s -X POST ... | jq -r '.data.post_id')

Bash 函式

publish() {
  local account_id=$1
  local platform=$2
  local text=$3
  local media_url=$4

  curl -s -X POST "$API_URL" \
    -H "boring-api-key: $BORING_API_KEY" \
    -H "Content-Type: application/json" \
    -d "{
      \"post\": {
        \"accountId\": \"$account_id\",
        \"content\": {
          \"text\": \"$text\",
          \"mediaUrls\": [\"$media_url\"],
          \"platform\": \"$platform\"
        },
        \"target\": {
          \"targetType\": \"$platform\"
        }
      }
    }"
}

批次發佈腳本

batch_publish.sh

#!/bin/bash
API_URL="https://boring.aiagent-me.com/v2/posts"
API_KEY="$BORING_API_KEY"

while IFS=',' read -r account platform text media; do
  json=$(cat <<EOF
{
  "post": {
    "accountId": "$account",
    "content": {
      "text": "$text",
      "mediaUrls": [${media:+\"$media\"}],
      "platform": "$platform"
    },
    "target": {"targetType": "$platform"}
  }
}
EOF
)

  response=$(curl -s -X POST "$API_URL" \
    -H "boring-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d "$json")

  if [ "$(echo "$response" | jq -r '.success')" = "true" ]; then
    echo "✓ $(echo "$response" | jq -r '.data.post_id')"
  else
    echo "✗ $(echo "$response" | jq -r '.message')"
  fi

  sleep 2
done < posts.csv

posts.csv 範例:

fb-account-id,facebook,Hello Facebook!,
ig-account-id,instagram,Photo day!,https://example.com/photo.jpg
threads-account-id,threads,Daily thoughts,

檢查媒體 URL

curl -I https://example.com/image.jpg

大量檢查:

./check_media.sh "https://example.com/1.jpg" "https://example.com/2.jpg"

錯誤處理腳本

response=$(curl -s -X POST "$API_URL" ...)

if [ "$(echo "$response" | jq -r '.success')" != "true" ]; then
  error=$(echo "$response" | jq -r '.error')
  message=$(echo "$response" | jq -r '.message')
  echo "Error: $error - $message"
fi

延伸閱讀