TypeScript SDKGuides

Downloading Files

Learn how to download files from the Shelby network in a browser environment

Overview

This guide demonstrates how to retrieve and download files stored on the Shelby network from a browser environment. Files on Shelby are associated with the account address of the uploader and can be accessed through both direct URLs and the SDK API.

Prerequisites

Before downloading files, ensure you have:

  • The Shelby SDK installed and configured
  • Access to the account address of the file uploader
  • A valid API key for the Shelby network

Download Methods

Method 1: Direct URL Access

Files stored on Shelby can be accessed directly via HTTP using a predictable URL pattern:

https://api.shelbynet.shelby.xyz/shelby/v1/blobs/<uploader-address>/<file-name>.<file-extension>

URL Structure:

  • <uploader-address>: The Aptos account address that uploaded the file
  • <file-name>: The original name of the uploaded file
  • <file-extension>: The file extension (e.g., .txt, .pdf, .jpg)

Example:

https://api.shelbynet.shelby.xyz/shelby/v1/blobs/0x123.../document.pdf

Method 2: SDK API Access

The Shelby SDK provides programmatic access to retrieve file metadata and download files.

Initialize the Client

First, configure the Shelby client for your browser environment:

import { ShelbyClient } from "@shelby-protocol/sdk/browser";
import { Network } from "@aptos-labs/ts-sdk";

const shelbyClient = new ShelbyClient({
  network: Network.SHELBYNET,
  apiKey: process.env.SHELBY_API_KEY,
});

Retrieve Account Files

Get a list of all files associated with a specific account:

const blobs = await shelbyClient.coordination.getAccountBlobs({
  account: accountAddress,
});

This returns an array containing metadata for all files uploaded by the specified account, including:

  • File names
  • Upload timestamps
  • File sizes
  • Expiration dates
  • Blob merkle roots

Download Specific Files

Once you have the file metadata, you can download specific files using the blob information:

// Example: Download the first file from the account
if (blobs.length > 0) {
  const firstBlob = blobs[0];

  // Construct download URL
  const downloadUrl = `https://api.shelbynet.shelby.xyz/shelby/v1/blobs/${accountAddress}/${firstBlob.name}`;

  // Fetch the file
  const response = await fetch(downloadUrl);
  const fileData = await response.blob();

  // Create download link for user
  const downloadLink = document.createElement("a");
  downloadLink.href = URL.createObjectURL(fileData);
  downloadLink.download = firstBlob.name;
  downloadLink.click();
}

Error Handling

When downloading files, handle common error scenarios:

try {
  const blobs = await shelbyClient.coordination.getAccountBlobs({
    account: accountAddress,
  });

  if (blobs.length === 0) {
    console.log("No files found for this account");
    return;
  }

  // Process files...
} catch (error) {
  if (error.message.includes("404")) {
    console.error("Account or file not found");
  } else if (error.message.includes("403")) {
    console.error("Access denied - check API key");
  } else {
    console.error("Download failed:", error.message);
  }
}