API Documentation
Integrate KingCDN's powerful file distribution into your applications with our simple REST API.
Introduction
KingCDN provides a simple REST API for uploading and retrieving files through our global content delivery network. All API endpoints return JSON responses.
Base URL: https://king-cdn.zone.id
(replace with your actual domain)
Upload API
Upload files to the CDN with a simple POST request.
POST
/upload
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The file to upload (max 2GB) |
Example Request
const formData = new FormData();
formData.append('file', fileInput.files[0]);
const response = await fetch('https://king-cdn.zone.id/upload', {
method: 'POST',
body: formData
});
const data = await response.json();
Response
Successful upload returns a JSON object with file details:
Success Response (200 OK)
{
"uploaded_by": "King David",
"file_id": "9850f8cf99fdd99d",
"file_url": "https://king-cdn.zone.id/file/9850f8cf99fdd99d"
}
Error Response
{
"error": "Upload failed"
}
Retrieve API
Retrieve files from the CDN using the file ID returned from the upload operation.
GET
/file/:id
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
id | String | Yes | The file ID returned from upload |
Example Request
// Direct URL access
// https://king-cdn.zone.id/file/9850f8cf99fdd99d
// Or programmatically
const fileId = '9850f8cf99fdd99d';
const fileUrl = `https://king-cdn.zone.id/file/${fileId}`;
Response
Successful request returns the file with appropriate Content-Type header.
Error responses return JSON with error details:
Error Response (404 Not Found)
{
"error": "File not found"
}
Example Implementation
Here's a complete example of how to integrate KingCDN into your application:
// Upload a file to KingCDN
async function uploadFile(file) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch('https://king-cdn.zone.id/upload', {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.error) {
console.error('Upload failed:', data.error);
return null;
}
console.log('File uploaded successfully:', data.file_url);
return data.file_url;
} catch (error) {
console.error('Upload error:', error);
return null;
}
}
// Usage
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', async (e) => {
if (e.target.files.length > 0) {
const fileUrl = await uploadFile(e.target.files[0]);
if (fileUrl) {
// Do something with the file URL
console.log('File available at:', fileUrl);
}
}
});