Bytescale JavaScript SDK
The Bytescale JavaScript SDK allows you to quickly integrate your application with Bytescale.
Effortlessly upload, optimize, transform, and host your digital media assets.
Installation
npm install @bytescale/sdk
Additional step for Node.js:
npm install node-fetch
Full Documentation
Bytescale JavaScript SDK Full Documentation »
Quick Guide
Upload a File
import * as Upload from "@bytescale/sdk";
import fetch from "node-fetch";
const uploadManager = new Upload.UploadManager(
new Upload.Configuration({
fetchApi: fetch,
apiKey: "YOUR_UPLOAD_API_KEY"
})
);
uploadManager
.upload({
accountId: "YOUR_UPLOAD_ACCOUNT_ID",
data: "Example Data"
})
.then(
uploadedFile => console.log(uploadedFile),
error => console.error(error)
);
Download a File
import * as Upload from "@bytescale/sdk";
import fetch from "node-fetch";
const fileApi = new Upload.FileApi(
new Upload.Configuration({
fetchApi: fetch,
apiKey: "YOUR_UPLOAD_API_KEY"
})
);
fileApi
.downloadFile({
accountId: "YOUR_UPLOAD_ACCOUNT_ID",
filePath: "/uploads/2022/12/25/hello_world.txt"
})
.then(response => response.text())
.then(
fileContents => console.log(fileContents),
error => console.error(error)
);
Note: you can also download files using: https://upcdn.io/{accountId}/raw/{filePath}
Process a File
import * as Upload from "@bytescale/sdk";
import fetch from "node-fetch";
const fileApi = new Upload.FileApi(
new Upload.Configuration({
fetchApi: fetch,
apiKey: "YOUR_UPLOAD_API_KEY"
})
);
fileApi
.processFile({
accountId: "YOUR_UPLOAD_ACCOUNT_ID",
filePath: "/uploads/2022/12/25/image.jpg",
transformation: "thumbnail"
})
.then(response => response.stream())
.then(
imageByteStream =>
new Promise((resolve, reject) => {
const writer = fs.createWriteStream("image-thumbnail.jpg");
writer.on("close", resolve);
writer.on("error", reject);
imageByteStream.pipe(writer);
})
)
.then(
() => console.log("Thumbnail saved to 'image-thumbnail.jpg'"),
error => console.error(error)
);
Note: you can also process files using: https://upcdn.io/{accountId}/{transformation}/{filePath}
Get File Details
import * as Upload from "@bytescale/sdk";
import fetch from "node-fetch";
const fileApi = new Upload.FileApi(
new Upload.Configuration({
fetchApi: fetch,
apiKey: "YOUR_UPLOAD_API_KEY"
})
);
fileApi
.getFileDetails({
accountId: "YOUR_UPLOAD_ACCOUNT_ID",
filePath: "/uploads/2022/12/25/image.jpg"
})
.then(
fileDetails => console.log(fileDetails),
error => console.error(error)
);
List Folder
import * as Upload from "@bytescale/sdk";
import fetch from "node-fetch";
const folderApi = new Upload.FolderApi(
new Upload.Configuration({
fetchApi: fetch,
apiKey: "YOUR_UPLOAD_API_KEY"
})
);
folderApi
.listFolder({
accountId: "YOUR_UPLOAD_ACCOUNT_ID",
folderPath: "/",
recursive: false
})
.then(
result => console.log(`Items in folder: ${result.items.length}`),
error => console.error(error)
);
License
MIT