TypeScript SDKExamples
Media Examples
Examples for uploading and managing media
Complete examples for uploading and managing media files with the Fanbeam SDK.
Initialize Upload
import { media } from 'fanbeam';
const response = await media.initializeUpload({
body: {
filename: 'image.jpg',
contentType: 'image/jpeg',
size: 1024000
}
});
const { id, presignedUrl } = response.data;Upload File
import { media } from 'fanbeam';
// Initialize upload
const initResponse = await media.initializeUpload({
body: {
filename: 'image.jpg',
contentType: 'image/jpeg',
size: file.size
}
});
// Upload file using presigned URL
const file = new File([fileData], 'image.jpg', { type: 'image/jpeg' });
await fetch(initResponse.data.presignedUrl, {
method: 'PUT',
body: file,
headers: {
'Content-Type': 'image/jpeg'
}
});
// Finalize upload
const finalizeResponse = await media.finalizeUpload({
params: { id: initResponse.data.id }
});
console.log('Media ID:', finalizeResponse.data.id);Get Media Metadata
import { media } from 'fanbeam';
const response = await media.get({
params: { id: 'media-id' }
});
console.log('Media:', response.data);Delete Media
import { media } from 'fanbeam';
await media.remove({
params: { id: 'media-id' }
});Complete Upload Example
import { media } from 'fanbeam';
import fs from 'fs';
async function uploadMedia(filePath: string) {
// Read file
const fileData = fs.readFileSync(filePath);
const stats = fs.statSync(filePath);
// Initialize upload
const initResponse = await media.initializeUpload({
body: {
filename: path.basename(filePath),
contentType: 'image/jpeg',
size: stats.size
}
});
// Upload file
await fetch(initResponse.data.presignedUrl, {
method: 'PUT',
body: fileData,
headers: {
'Content-Type': 'image/jpeg'
}
});
// Finalize
const finalizeResponse = await media.finalizeUpload({
params: { id: initResponse.data.id }
});
return finalizeResponse.data.id;
}