isurl.cc API 簡介
isurl.cc 提供完整的 RESTful API,讓開發者可以將短網址功能整合到自己的應用程式、網站或自動化流程中。無論是批量生成短網址、取得點擊統計、還是管理 QR Code,都能透過 API 輕鬆實現。
取得 API 金鑰
- 登入 isurl.cc 帳號
- 前往「設定」→「API」
- 點擊「生成 API 金鑰」
- 複製並安全保管您的 API Key
重要:請勿將 API Key 公開在前端程式碼或公開的 Git 儲存庫中。
API 基本使用
縮短網址
最基本的 API 呼叫——將長網址轉換為短網址:
POST https://isurl.cc/api/url/add
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
{
"url": "https://example.com/very-long-url-path",
"custom": "mylink",
"password": "",
"expiry": ""
}
回應範例:
{
"error": 0,
"short": "https://isurl.cc/mylink"
}
Python 範例
import requests
API_KEY = "your_api_key_here"
API_URL = "https://isurl.cc/api/url/add"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"url": "https://example.com/long-url"
}
response = requests.post(API_URL, json=data, headers=headers)
result = response.json()
if result["error"] == 0:
print(f"短網址: {result['short']}")
else:
print(f"錯誤: {result['message']}")
JavaScript (Node.js) 範例
const fetch = require('node-fetch');
const API_KEY = 'your_api_key_here';
async function shortenUrl(longUrl) {
const response = await fetch('https://isurl.cc/api/url/add', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ url: longUrl })
});
const data = await response.json();
return data.short;
}
shortenUrl('https://example.com/long-url')
.then(short => console.log(`短網址: ${short}`));
PHP 範例
$apiKey = "your_api_key_here";
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://isurl.cc/api/url/add",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . $apiKey,
"Content-Type: application/json"
],
CURLOPT_POSTFIELDS => json_encode([
"url" => "https://example.com/long-url"
])
]);
$response = curl_exec($ch);
$result = json_decode($response, true);
echo "短網址: " . $result['short'];
進階 API 功能
取得連結統計
GET https://isurl.cc/api/url/:id/stats
Authorization: Bearer YOUR_API_KEY
批量縮短網址
透過迴圈呼叫 API,可以批量處理大量網址。建議設定適當的延遲(每秒不超過 10 次請求)以避免觸發頻率限制。
管理 QR Code
POST https://isurl.cc/api/qr/add
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
{
"type": "link",
"data": "https://example.com"
}
API 最佳實踐
- 錯誤處理:始終檢查回應中的 error 欄位,處理各種錯誤情況
- 頻率控制:遵守 API 使用限制,避免被暫時封鎖
- 安全存放金鑰:使用環境變數或密鑰管理服務存放 API Key
- 快取結果:對同一 URL 的縮短結果進行快取,減少不必要的 API 呼叫
- 使用 HTTPS:所有 API 請求必須使用 HTTPS
常見問題
Q:API 有使用次數限制嗎?
A:免費帳號有每日使用上限,如需更高額度可升級方案。
Q:API 支援批量操作嗎?
A:目前需逐一呼叫,建議透過非同步方式提升效率。
Q:短網址會過期嗎?
A:預設永不過期,但您可以透過 API 設定過期時間。
結論
isurl.cc API 讓開發者能輕鬆將短網址功能整合到任何應用中。完整的 API 文件提供更多端點和參數說明。立即申請 API Key,開始打造您的自動化短網址流程!