شیوه احراز هویت (Authentication)
تمام درخواستهای API نیازمند ارسال کلید اختصاصی شما هستند. کلید API خود را میتوانید پس از ورود در پنل کاربری f2n.ir دریافت نمایید. کلید را به یکی از دو روش زیر ارسال کنید:
# روش اول: استفاده از هدر اختصاصی
X-API-Key: f2n_live_your_secret_api_key
# روش دوم: هدر استاندارد Bearer
Authorization: Bearer f2n_live_your_secret_api_key
کوتاهسازی و تولید پیوند جدید
POST
https://f2n.ir/api/v1/links/shorten
اگر برای حساب خود پیشوند اختصاصی (مانند mb) ثبت کرده باشید، تمام پیوندها به شکل خودکار با الگوی f2n.ir/mb-slug صادر میگردند.
پارامتر
نوع
وضعیت
توضیحات
url
String
اجباری
نشانی اینترنتی مقصد (باید با http:// یا https:// آغاز شود)
custom_code
String
اختیاری
شناسه دلخواه (مانند rick که نتیجه آن mb-rick خواهد بود)
password
String
اختیاری
رمز عبور پیوند جهت محافظت از دسترسی به آدرس مقصد
title
String
اختیاری
برچسب پیوند جهت دستهبندی و شناسایی در پنل مدیریت
نمونه کدهای پیادهسازی
cURL
Python
Node.js / JS
PHP
Go
کپی کد
curl -X POST https://f2n.ir/api/v1/links/shorten \
-H "X-API-Key: f2n_live_your_secret_api_key" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/target-page",
"custom_code": "campaign",
"title": "کمپین نوروزی"
}'
کپی کد
import requests
API_KEY = "f2n_live_your_secret_api_key"
ENDPOINT = "https://f2n.ir/api/v1/links/shorten"
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json"
}
payload = {
"url": "https://example.com/target-page",
"custom_code": "campaign",
"title": "کمپین نوروزی"
}
response = requests.post(ENDPOINT, json=payload, headers=headers)
data = response.json()
if response.status_code == 200:
print("پیوند کوتاه با موفقیت ساخته شد:", data["data"]["short_url"])
else:
print("خطا:", data.get("detail"))
کپی کد
const response = await fetch('https://f2n.ir/api/v1/links/shorten', {
method: 'POST',
headers: {
'X-API-Key': 'f2n_live_your_secret_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com/target-page',
custom_code: 'campaign',
title: 'کمپین نوروزی'
})
});
const result = await response.json();
if (response.ok) {
console.log('Short URL:', result.data.short_url);
} else {
console.error('Error:', result.detail);
}
کپی کد
<?php
$apiKey = "f2n_live_your_secret_api_key";
$ch = curl_init("https://f2n.ir/api/v1/links/shorten");
$payload = json_encode([
"url" => "https://example.com/target-page",
"custom_code" => "campaign",
"title" => "کمپین نوروزی"
]);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
"X-API-Key: " . $apiKey,
"Content-Type: application/json"
]
]);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
echo "Short Link: " . $result["data"]["short_url"];
?>
کپی کد
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
url := "https://f2n.ir/api/v1/links/shorten"
apiKey := "f2n_live_your_secret_api_key"
payload := map[string]string{
"url": "https://example.com/target-page",
"custom_code": "campaign",
"title": "کمپین نوروزی",
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))
req.Header.Set("X-API-Key", apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Status:", resp.Status)
}
نمونه پاسخ موفق (200 OK)
{
"success": true,
"data": {
"code": "mb-campaign",
"short_url": "https://f2n.ir/mb-campaign",
"original_url": "https://example.com/target-page",
"title": "کمپین نوروزی",
"prefix": "mb"
}
}