ThirdwebStorage
Upload and download files from decentralized storage systems.
// Create a default storage class with a client ID when used in client-side applicationsconst storage = new ThirdwebStorage({ clientId: "your-client-id" }); // Create a default storage class with a secret key when used in server-side applicationsconst storage = new ThirdwebStorage({ secretKey: "your-secret-key" }); You can get a clientId and secretKey from https://thirdweb.com/create-api-key // Upload any file or JSON objectconst uri = await storage.upload(data);const result = await storage.download(uri); // Or configure a custom uploader, downloader, and gateway URLsconst gatewayUrls = { // We define a mapping of schemes to gateway URLs "ipfs://": [ "https://ipfs.thirdwebcdn.com/ipfs/", "https://cloudflare-ipfs.com/ipfs/", "https://ipfs.io/ipfs/", ],};const downloader = new StorageDownloader();const uploader = new IpfsUploader();const clientId = "your-client-id";const storage = new ThirdwebStorage({ clientId, uploader, downloader, gatewayUrls });
class ThirdwebStorage<
function constructor<
Downloads arbitrary data from any URL scheme.
const uri = "ipfs://example";const data = await storage.download(uri);
function download( url: string,): Promise<Response>;
Downloads JSON data from any URL scheme. Resolves any URLs with schemes to retrievable gateway URLs.
const uri = "ipfs://example";const json = await storage.downloadJSON(uri);
function downloadJSON<TJSON = any>( url: string,): Promise<TJSON>;
Resolve any scheme on a URL to get a retrievable URL for the data
const uri = "ipfs://example";const url = storage.resolveScheme(uri);console.log(url);
function resolveScheme(url: string): string;
Upload arbitrary file or JSON data using the configured decentralized storage system. Automatically uploads any file data within JSON objects and replaces them with hashes.
// Upload file dataconst file = readFileSync("../file.jpg");const fileUri = await storage.upload(file); // Or upload a JSON objectconst json = { name: "JSON", image: file };const jsonUri = await storage.upload(json);
function upload(data: unknown, options?: T): Promise<string>;
Batch upload arbitrary file or JSON data using the configured decentralized storage system. Automatically uploads any file data within JSON objects and replaces them with hashes.
// Upload an array of file dataconst files = [ readFileSync("../file1.jpg"), readFileSync("../file2.jpg"),];const fileUris = await storage.uploadBatch(files); // Upload an array of JSON objectsconst objects = [ { name: "JSON 1", image: files[0] }, { name: "JSON 2", image: files[1] },];const jsonUris = await storage.uploadBatch(objects);
function uploadBatch( data: Array<unknown>, options?: T,): Promise<Array<string>>;