API Overview
RESTful API untuk layanan pemendek URL dengan dukungan custom short codes dan tracking statistik.
Features:
- URL shortening otomatis
- Custom short codes
- Optional URL titles
- Click tracking
- URL expiration
- Token-based authentication
Format:
- JSON requests dan responses
- HTTP status codes standar
- UTF-8 encoding
- Bearer token authentication
Autentikasi: Sebagian besar endpoint memerlukan token API.
Buat token Anda di dashboard.
Authentication
API menggunakan Bearer token authentication dengan Laravel Sanctum.
Cara Mendapatkan Token
- Daftar atau login ke akun Anda
- Kunjungi halaman API Tokens
- Buat token baru dengan nama yang deskriptif
- Salin token yang dibuat (hanya ditampilkan sekali!)
Menggunakan Token
Sertakan token dalam header Authorization:
Authorization: Bearer YOUR_API_TOKEN_HERE
Endpoint Types
Authenticated Endpoints
- Full features & custom codes
- URL management
- User-specific data
- No rate limits
Public Endpoints
- Basic features only
- No custom codes
- Anonymous URLs
- Rate limited
Base URL
Semua API requests harus menggunakan base URL berikut:
https://link.batubhayangkara.com/api
Create Short URL (Authenticated)
Endpoint
POST
/api/v1/shorten
Authentication
Required
Bearer Token
Content-Type
application/json
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
original_url |
string | Yes | URL yang ingin dipendekkan (max 2000 chars) |
title |
string | No | Judul optional untuk URL (max 255 chars) |
custom_short_code |
string | No | Custom short code (3-20 chars, a-z, 0-9, -) |
expires_at |
datetime | No | Waktu expirasi (ISO 8601 format) |
Request Example
{
"original_url": "https://example.com/very/long/url/path",
"title": "My Awesome Website",
"custom_short_code": "my-site",
"expires_at": "2024-12-31T23:59:59Z"
}
Success Response (201)
{
"success": true,
"data": {
"id": 123,
"original_url": "https://example.com/very/long/url/path",
"title": "My Awesome Website",
"short_url": "https://link.batubhayangkara.com/my-site",
"short_code": "my-site",
"is_custom": true,
"expires_at": "2024-12-31T23:59:59.000000Z",
"created_at": "2026-03-16T02:13:27.000000Z"
}
}
Public URL Shortening (No Auth)
Endpoint
POST
/api/shorten
Authentication
None
Rate Limit
10/minute
Parameters (Limited)
| Field | Type | Required | Description |
|---|---|---|---|
original_url |
string | Yes | URL yang ingin dipendekkan (max 2000 chars) |
Success Response (201)
{
"success": true,
"message": "URL shortened successfully. Sign up for custom codes and more features!",
"data": {
"original_url": "https://example.com/path",
"short_url": "https://link.batubhayangkara.com/aBc123",
"short_code": "aBc123",
"created_at": "2026-03-16T02:13:27.000000Z"
}
}
Get My URLs (Authenticated)
Endpoint
GET
/api/v1/my-urls
Query Parameters
?per_page=15 (max 100)
Success Response (200)
{
"success": true,
"data": [
{
"id": 123,
"original_url": "https://example.com/path",
"title": "My Website",
"short_code": "my-site",
"is_custom": true,
"clicks": 42,
"expires_at": null,
"created_at": "2026-03-16T02:13:27.000000Z"
}
],
"pagination": {
"current_page": 1,
"per_page": 15,
"total": 25,
"last_page": 2,
"has_more": true
}
}
Delete URL (Authenticated)
Endpoint
DELETE
/api/v1/urls/{id}
Path Parameters
id - URL ID (integer)
Success Response (200)
{
"success": true,
"message": "URL berhasil dihapus"
}
Error Response (404)
{
"success": false,
"message": "URL not found or not owned by user"
}
Get Statistics (Authenticated)
Endpoint
GET
/api/v1/stats
Authentication
Required
Success Response (200)
{
"success": true,
"data": {
"total_urls": 42,
"total_clicks": 1337,
"most_clicked": {
"short_code": "my-site",
"clicks": 250,
"url": "https://link.batubhayangkara.com/my-site"
},
"recent_urls": [
{
"short_code": "abc123",
"original_url": "https://example.com",
"clicks": 5,
"created_at": "2026-03-16T02:13:27.000000Z"
}
]
}
}
Error Codes Reference
| HTTP Code | Error Type | Description | Common Causes |
|---|---|---|---|
| 200 | Success | Request successful | All went well |
| 201 | Created | Resource created successfully | URL shortened successfully |
| 400 | Bad Request | Invalid request data | Missing required fields, invalid URL format |
| 401 | Unauthorized | Authentication required | Missing token, invalid token |
| 403 | Forbidden | Access denied | Token expired, insufficient permissions |
| 404 | Not Found | Resource not found | URL doesn't exist, not owned by user |
| 409 | Conflict | Resource already exists | Custom short code already taken |
| 422 | Validation Error | Input validation failed | Invalid data format, field constraints |
| 429 | Rate Limit | Too many requests | Exceeded rate limit, try again later |
| 500 | Server Error | Internal server error | Unexpected server issue |
Common Error Response Format
{
"success": false,
"message": "Error description in Bahasa Indonesia",
"errors": {
"original_url": ["URL tidak valid"],
"short_code": ["Kode sudah digunakan"]
}
}
Code Examples
cURL Examples
1. Get API Token (via Web Interface)
Visit https://link.batubhayangkara.com/api-tokens to generate your API token
2. Create Authenticated Short URL
curl -X POST "https://link.batubhayangkara.com/api/v1/shorten" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-d '{
"original_url": "https://example.com/very/long/path",
"title": "My Website",
"short_code": "my-site",
"expires_at": "2024-12-31T23:59:59Z"
}'
3. Create Public Short URL (No Auth)
curl -X POST "https://link.batubhayangkara.com/api/shorten" \
-H "Content-Type: application/json" \
-d '{
"original_url": "https://example.com/path"
}'
4. Get My URLs
curl -X GET "https://link.batubhayangkara.com/api/v1/my-urls?per_page=10" \
-H "Authorization: Bearer YOUR_API_TOKEN"
5. Delete URL
curl -X DELETE "https://link.batubhayangkara.com/api/v1/urls/123" \
-H "Authorization: Bearer YOUR_API_TOKEN"
6. Get Statistics
curl -X GET "https://link.batubhayangkara.com/api/v1/stats" \
-H "Authorization: Bearer YOUR_API_TOKEN"
JavaScript Examples
Using Fetch API
// Create authenticated short URL
const response = await fetch('https://link.batubhayangkara.com/api/v1/shorten', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_TOKEN'
},
body: JSON.stringify({
original_url: 'https://example.com/path',
title: 'My Website',
short_code: 'custom-code'
})
});
const result = await response.json();
console.log(result);
PHP Examples
Using Guzzle HTTP
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('https://link.batubhayangkara.com/api/v1/shorten', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_API_TOKEN'
],
'json' => [
'original_url' => 'https://example.com/path',
'title' => 'My Website',
'short_code' => 'custom-code'
]
]);
$data = json_decode($response->getBody(), true);
var_dump($data);
URL Statistics
Endpoint
GET
/api/stats/{shortCode}
Parameters
shortCode - Short code dari URL
Success Response (200)
{
"success": true,
"data": {
"short_code": "my-site",
"original_url": "https://example.com/very/long/url/path",
"title": "My Awesome Website",
"is_custom": true,
"clicks": 42,
"created_at": "2026-03-16T02:13:27.000000Z",
"expires_at": "2024-12-31T23:59:59.000000Z",
"is_expired": false
}
}
Error Codes
| HTTP Code | Error Type | Description |
|---|---|---|
| 400 | Bad Request | Invalid request format atau missing parameters |
| 404 | Not Found | Short URL tidak ditemukan |
| 422 | Validation Error | Validation gagal (URL invalid, custom code sudah ada, dll) |
| 500 | Server Error | Internal server error |
Error Response Example
{
"success": false,
"message": "Validation failed",
"errors": {
"original_url": ["The original url field is required."],
"custom_short_code": ["Custom code is already taken, please choose another"]
}
}
cURL Examples
1. Create Short URL (Auto-generated)
curl -X POST https://link.batubhayangkara.com/api/shorten \
-H "Content-Type: application/json" \
-d '{
"original_url": "https://example.com/very/long/url"
}'
2. Create Short URL (Custom Code)
curl -X POST https://link.batubhayangkara.com/api/shorten \
-H "Content-Type: application/json" \
-d '{
"original_url": "https://example.com/my-product",
"title": "Product Launch Page",
"custom_short_code": "product-2024"
}'
3. Get URL Statistics
curl -X GET https://link.batubhayangkara.com/api/stats/product-2024
4. Create Expiring URL
curl -X POST https://link.batubhayangkara.com/api/shorten \
-H "Content-Type: application/json" \
-d '{
"original_url": "https://example.com/limited-offer",
"title": "Flash Sale - Limited Time",
"expires_at": "2024-12-31T23:59:59Z"
}'
Need help? Contact support atau check the dashboard untuk testing secara manual.