Uploading

Uploading files to UHRP storage servers

Read as Markdown

The StorageUploader from the @bsv/sdk makes it simple to upload files to the Babbage storage server and retrieve a permanent, content-addressed URL known as a UHRP URL. This allows you to store data in a decentralized, verifiable way on the Babbage overlay network. This guide walks you through the steps to upload a file, specify how long it should be hosted, and retrieve the corresponding UHRP URL.


Prerequisites

Before you begin, endure you have the following:

  • Node.js and npm – Installed on your system (Node v14+ recommended).
  • BSV Wallet – A working Babbage/BSV wallet setup.
  • @bsv/sdk – The BSV TypeScript SDK, installed via npm: npm install @bsv/sdk.
  • File for upload - The file you would like to upload the storage server.
1npm install @bsv/sdk
Copied!

Step 1: Set Up Your Wallet

To interact with the Babbage network, you'll need a wallet instance. This is required to sign your upload and prove ownership.

1import { WalletClient } from '@bsv/sdk';
2
3const wallet = new WalletClient('auto');
Copied!

Step 2: Create a StorageUploader Instance

Now you’ll create a StorageUploader using the wallet and the URL of the storage server you want to use.

1import { StorageUploader } from '@bsv/sdk';
2
3const storageUploader = new StorageUploader({
4  storageURL: 'https://nanostore.babbage.systems',
5  wallet
6});
Copied!
  • storageURL: The endpoint of the storage server to upload to.
  • wallet: Required to authenticate and sign the upload.

Step 3: Prepare Your File

Convert the file into a byte array and specify its MIME type. This will be passed to the uploader.

1import { readFileSync } from 'fs';
2
3const fileBuffer = readFileSync('path/to/file.png');
4const fileData = Array.from(fileBuffer);
5
6const fileObject = {
7  data: fileData,       // The file’s raw bytes
8  type: 'image/png'     // The file’s MIME type
9};
Copied!

Step 4: Upload the File

Call publishFile to upload your file and specify how long it should be hosted (retention period in minutes).

1async function uploadExample() {
2  try {
3    const retentionPeriod = 525600; // Example: ~1 year in minutes
4
5    const result = await storageUploader.publishFile({
6      file: fileObject,
7      retentionPeriod
8    });
9
10    console.log('File published:', result.published);
11    console.log('UHRP URL:', result.uhrpURL);
12  } catch (error) {
13    console.error('Error uploading file:', error);
14  }
15}
16
17uploadExample();
Copied!

Result Fields:

  • result.published: A boolean indicating whether the file was successfully published.
  • result.uhrpURL: The permanent UHRP URL referencing the file.

What’s Next?

Nice work! You’ve successfully uploaded a file to the Babbage storage server. From here, you might:

  • Store the uhrpURL in your database or share it publicly.
  • Use StorageDownloader to retrieve the file later.
  • Build applications that allow users to upload and share files with embedded metadata, expiration, or monetization features.

For more advanced use cases, explore:

  • Integration with micropayments or identity systems.
  • Embedding UHRP URLs in tokens, NFTs, or blockchain-based apps.

Happy uploading!

Was this page helpful?