curl -X POST "https://yourdomain.com/v1/files" \
-H "X-API-Key: tgbox_your_key_here" \
-F "file=@./invoice.pdf"
function uploadFile(file, apiKey, onProgress) {
return new Promise((resolve, reject) => {
const form = new FormData();
form.append("file", file);
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://yourdomain.com/v1/files");
if (apiKey) xhr.setRequestHeader("X-API-Key", apiKey);
xhr.upload.onprogress = (e) => {
if (e.lengthComputable)
onProgress(Math.round((e.loaded / e.total) * 100));
};
xhr.onload = () => {
const data = JSON.parse(xhr.responseText || "{}");
xhr.status < 300 ? resolve(data) : reject(data);
};
xhr.send(form);
});
}
import requests
from pathlib import Path
def upload_file(path, api_key=None):
file_path = Path(path)
headers = {"X-API-Key": api_key} if api_key else {}
with file_path.open("rb") as fh:
files = {"file": (file_path.name, fh, "application/octet-stream")}
resp = requests.post(
"https://yourdomain.com/v1/files",
headers=headers,
files=files,
timeout=(5, 120),
)
if resp.status_code == 429:
retry = resp.headers.get("Retry-After", "30")
raise RuntimeError(f"Rate limited; retry after {retry}s")
resp.raise_for_status()
return resp.json()
<?php
use GuzzleHttp\Client;
$client = new Client([
"base_uri" => "https://yourdomain.com",
"timeout" => 120,
]);
$path = __DIR__ . "/invoice.pdf";
$handle = fopen($path, "rb");
try {
$response = $client->post("/v1/files", [
"headers" => ["X-API-Key" => "tgbox_your_key_here"],
"multipart" => [[
"name" => "file",
"contents" => $handle,
"filename" => basename($path),
]],
]);
print_r(json_decode((string) $response->getBody(), true));
} finally {
if (is_resource($handle)) fclose($handle);
}