Keep integration keys on your server.Run privileged requests in trusted server code. Do not bundle a long-lived API key in a public frontend. Browser examples illustrate request structure only.
Uploading Files via API
cURL
bash
curl -X POST https://media.example.com/api/v1/files \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "[email protected]" \
-F "visibility=public" \
-F "alias=gallery/cover"TypeScript / JavaScript
typescript
const form = new FormData();
form.append("file", fileInput.files[0]);
form.append("visibility", "public");
form.append("alias", "profile/avatar");
const res = await fetch("https://media.example.com/api/v1/files", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
},
body: form,
});
const result = await res.json();
console.log("Media URL:", result.data.url);Python
python
import requests
with open("sample.mp4", "rb") as f:
res = requests.post(
"https://media.example.com/api/v1/files",
headers={"Authorization": "Bearer mk_live_xxxx"},
files={"file": f},
data={"visibility": "public"}
)
data = res.json()
print("Permanent URL:", data["data"]["url"])Rust
rust
use reqwest::multipart::{Form, Part};
let form = Form::new()
.part("file", Part::bytes(file_bytes).file_name("photo.jpg"));
let client = reqwest::Client::new();
let res = client
.post("https://media.example.com/api/v1/files")
.bearer_auth("mk_live_xxxx")
.multipart(form)
.send()
.await?
.json::<serde_json::Value>()
.await?;
println!("URL: {}", res["data"]["url"]);Next.js (App Router)
typescript
// lib/media.ts
const API_BASE = process.env.MEDIA_API_URL!;
const API_KEY = process.env.MEDIA_API_KEY!;
export async function getMediaFiles(limit = 20) {
const res = await fetch(`${API_BASE}/api/v1/files?limit=${limit}`, {
headers: { 'X-API-Key': API_KEY },
next: { revalidate: 60 },
});
const { data } = await res.json();
return data.items;
}
export async function uploadMedia(formData: FormData) {
const res = await fetch(`${API_BASE}/api/v1/files`, {
method: 'POST',
headers: { 'X-API-Key': API_KEY },
body: formData,
});
return res.json();
}Python SDK Pattern
python
import requests
class OwnMediaClient:
def __init__(self, base_url: str, api_key: str):
self.base = base_url.rstrip("/")
self.session = requests.Session()
self.session.headers["X-API-Key"] = api_key
def list_files(self, limit=50, media_type=None):
params = {"limit": limit}
if media_type:
params["type"] = media_type
r = self.session.get(f"{self.base}/api/v1/files", params=params)
r.raise_for_status()
return r.json()["data"]
def upload(self, filepath: str, tags=None, alias=None):
data = {}
if tags:
data["tags"] = ",".join(tags)
if alias:
data["alias"] = alias
with open(filepath, "rb") as f:
files = {"file": f}
r = self.session.post(f"{self.base}/api/v1/files", files=files, data=data)
r.raise_for_status()
return r.json()["data"]
def delete(self, file_id: str, permanent=False):
endpoint = f"/api/v1/files/{file_id}"
if permanent:
endpoint += "/permanent"
r = self.session.delete(f"{self.base}{endpoint}")
r.raise_for_status()
return r.json()
# Usage
client = OwnMediaClient("https://api.example.com", "mk_live_...")
files = client.list_files(limit=10, media_type="image")
uploaded = client.upload("photo.jpg", tags=["portfolio"], alias="my-photo")