# Upload your first Blob object

Blob stores files such as images, PDFs, or text files. You save a file under a name and read it back later by that name.

In this tutorial, you build one API route that uploads `hello.txt` and one that reads it back. The read returns `Hello from ViteHub`.

> [!NOTE] Before you start
> Use the app from [Installation](https://vitehub.dev/docs/getting-started/installation) and run commands from the app root. You need no account or API key. Locally, Blob writes files to `.vitehub/data/blob`.

::tutorial-step{title="Enable Blob"}
## Enable Blob

Install the Blob package:

```bash pm [commands/install-blob]
pnpm add @vite-hub/blob
```

Add `blob: true` to your ViteHub options in `nuxt.config.ts` or `vite.config.ts`, keeping any other enabled features and plugins.

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: { blob: true },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  server: { port: 3000, strictPort: true },
  plugins: [vitehub({ blob: true }), nitro()],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  server: { port: 3000, strictPort: true },
  plugins: [vitehub({ blob: true })],
})
```

::

::tutorial-step{title="Accept a text upload"}
## Accept a text upload

Create `server/api/files.post.ts`. This route handles `POST /api/files` and saves the text of the request as `hello.txt`:

```ts [server/api/files.post.ts]
import { defineEventHandler, HTTPError, readRawBody } from 'h3'
import { blob } from 'vite-hub/blob'

export default defineEventHandler(async (event) => {
  const text = await readRawBody(event)
  if (!text) {
    throw new HTTPError({ status: 400, message: 'Send a text file in the request body.' })
  }

  const [error, object] = await blob.put('hello.txt', text, {
    contentType: 'text/plain',
  })
  if (error) throw error
  return {
    pathname: object.pathname,
    contentType: object.contentType,
    size: object.size,
  }
})
```

- `blob.put()` saves the file, replacing `hello.txt` if it already exists.
- `blob.put()` returns `[error, object]`. Check `error` for storage failures. Invalid arguments and configuration errors can throw.
- `object` describes the saved file. The route returns its name, type, and size.

::

::tutorial-step{title="Retrieve the file"}
## Retrieve the file

Create `server/api/files.get.ts` to read `hello.txt` and return its text:

```ts [server/api/files.get.ts]
import { defineEventHandler, HTTPError, setResponseHeader } from 'h3'
import { blob } from 'vite-hub/blob'

export default defineEventHandler(async (event) => {
  const [error, file] = await blob.get('hello.txt')
  if (error) throw error
  if (file === null) {
    throw new HTTPError({ status: 404, message: 'Upload hello.txt first.' })
  }

  setResponseHeader(event, 'content-type', 'text/plain; charset=utf-8')
  return file.text()
})
```

- `blob.get()` returns `[error, file]`, where `file` is a standard `Blob` or `null` when no file has that name.
- `file.text()` reads the content as text.
- If the file is missing, the route returns `404`.

::

::tutorial-step{title="Run and check the result"}
## Run and check the result

Start or restart the dev server after the config change:

```bash [commands/start]
pnpm dev
```

In a second terminal, create a test file:

```bash [commands/file]
printf 'Hello from ViteHub' > hello.txt
```

Upload it:

```bash [commands/request]
curl -X POST http://localhost:3000/api/files \
  -H 'content-type: text/plain' \
  --data-binary @hello.txt
```

The response shows the name, type, and size of the saved file:

```json [output/response.json]
{ "pathname": "hello.txt", "contentType": "text/plain", "size": 18 }
```

Read the file back:

```bash [commands/read]
curl http://localhost:3000/api/files
```

You get the file text back:

```text [output/file.txt]
Hello from ViteHub
```

The file stays after a restart. To check it, restart `pnpm dev` and run only the read request again.

::


## Before you go live

These routes let anyone upload. Before real users can upload, add access checks and upload limits.

## Next steps

::card-group
:::card{title="Upload files" icon="i-lucide-upload" to="/docs/blob/server-api#upload-files"}
Accept form uploads and check file type and size.
:::

:::card{title="Hosts" icon="i-lucide-cloud-cog" to="/docs/blob/hosts"}
Store files in R2 or S3 so all app instances share them.
:::
::

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/blob.md)
- [Get started](https://vitehub.dev/raw/docs/blob/get-started.md) (this page)
- [Configure](https://vitehub.dev/raw/docs/blob/configure.md)
- [Server API](https://vitehub.dev/raw/docs/blob/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/blob/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/blob/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/blob/limits-and-errors.md)
- [Media and images](https://vitehub.dev/raw/docs/blob/media.md)

Next: [Configure](https://vitehub.dev/raw/docs/blob/configure.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
