# Uploading

Canonical: https://docs.projectbabbage.com/docs/guides/storage/uploading

Uploading files to UHRP storage servers

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.

```bash
npm install @bsv/sdk
```

## 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.

```ts
import { WalletClient } from '@bsv/sdk';

const wallet = new WalletClient('auto');
```

## 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.

```ts
import { StorageUploader } from '@bsv/sdk';

const storageUploader = new StorageUploader({
  storageURL: 'https://nanostore.babbage.systems',
  wallet
});
```

- `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.

```ts
import { readFileSync } from 'fs';

const fileBuffer = readFileSync('path/to/file.png');
const fileData = Array.from(fileBuffer);

const fileObject = {
  data: fileData,       // The file’s raw bytes
  type: 'image/png'     // The file’s MIME type
};
```

## Step 4: Upload the File

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

```ts
async function uploadExample() {
  try {
    const retentionPeriod = 525600; // Example: ~1 year in minutes

    const result = await storageUploader.publishFile({
      file: fileObject,
      retentionPeriod
    });

    console.log('File published:', result.published);
    console.log('UHRP URL:', result.uhrpURL);
  } catch (error) {
    console.error('Error uploading file:', error);
  }
}

uploadExample();
```

### 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!
