# Use Vercel Blob in a Vite app with ViteHub

Set `host: 'vercel'` and `blob: true` in `vitehub()`, and connect a Blob store to the Vercel project. ViteHub picks the `vercel` driver, and server code reads and writes files with `blob` from `vite-hub/blob`. You do not install `@vercel/blob`; the driver bundles it.

## What Vercel Blob is

[Vercel Blob](https://vercel.com/docs/vercel-blob) is Vercel's file storage, on all plans, for uploads, images, exports, and more. Each store lives in one region and is public or private, which you cannot change later:

- A public store gives each file a URL that anyone with the link can open.
- A private store needs a token for every read.

Your code calls the same `blob` API as on Cloudflare R2, Netlify Blobs, S3, or local disk.

## Set up Vercel Blob

### Enable Blob with the Vercel host

### Nuxt

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

### Nitro

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

export default defineConfig({
  plugins: [vitehub({ host: 'vercel', blob: true }), nitro()],
})
```

### Vite

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

export default defineConfig({
  plugins: [vitehub({ host: 'vercel', blob: true })],
})
```

On the `vercel` host, `blob: true` picks `driver: 'vercel'`. The store writes private objects unless you set `access: 'public'` or serve it with `serve: { access: 'public' }`. A public object has a public Vercel Blob URL that anyone can open. For a public store, set the access mode yourself:

### Nuxt

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

### Nitro

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

export default defineConfig({
  plugins: [
    vitehub({
      host: 'vercel',
      blob: { driver: 'vercel', access: 'public' },
    }),
    nitro(),
  ],
})
```

### Vite

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

export default defineConfig({
  plugins: [
    vitehub({
      host: 'vercel',
      blob: { driver: 'vercel', access: 'public' },
    }),
  ],
})
```

`access` must match your store. See [Vercel Blob options](https://vitehub.dev/docs/blob/configure#vercel-blob) for `allowOverwrite`, `downloadTimeoutMs`, and `token`.

### Create the store and connect it

On Vercel, ViteHub signs in with [OIDC](https://vercel.com/docs/vercel-blob#oidc-access-by-default). When you connect a store, Vercel adds `BLOB_STORE_ID`, and each deployment gets a short-lived `VERCEL_OIDC_TOKEN`. You need no long-lived secret.

ViteHub picks credentials at runtime in this order:

1. A `token` in the `vercel` store config.
2. OIDC, when `BLOB_STORE_ID` is set. The Vercel SDK reads the OIDC token itself.
3. `BLOB_READ_WRITE_TOKEN`, for code off Vercel, such as CI or another host. The SDK also uses it when `BLOB_STORE_ID` is set but there is no OIDC token.

When neither `BLOB_STORE_ID` nor `BLOB_READ_WRITE_TOKEN` is set at runtime, ViteHub throws `BLOB_C0009`.

`vite dev` uses the local `fs` store when it infers Vercel Blob from these variables or from the Vercel host, so development writes do not change the real store. To use the real store in `vite dev`, set `remote: true` on the store, or set `driver: 'vercel'`, then run `vercel env pull` once. The SDK refreshes the OIDC token with your Vercel CLI login. Read [Use the real store in development](https://vitehub.dev/docs/blob/configure#use-the-real-store-in-development).

Create the store in one of two ways:

- On the project's **Storage** page in the Vercel dashboard, or with `vercel blob create-store <name> --access private --yes` in the linked project folder. See [Server uploads with Vercel Blob](https://vercel.com/docs/vercel-blob/server-upload).
- With the ViteHub Vercel Provision Step. It creates a store named `vitehub-blob` in `iad1` with your `access` mode and connects it to the project's production, preview, and development environments, which adds `BLOB_STORE_ID`. If an older connection has only `BLOB_READ_WRITE_TOKEN`, the step warns and keeps it. Reconnect the store in the dashboard to use OIDC.

```bash [Terminal]
VERCEL_TOKEN=... VERCEL_PROJECT_ID=... pnpm vitehub provision run --host vercel --dry-run
VERCEL_TOKEN=... VERCEL_PROJECT_ID=... pnpm vitehub provision run --host vercel
```

Add `VERCEL_TEAM_ID` or `VERCEL_ORG_ID` if the token needs a team scope. See [Provisioning](https://vitehub.dev/docs/development/provisioning).

### Upload and read files

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

// Check who may upload before this call
export default defineEventHandler(async (event) => {
  const [error, files] = await blob.handleUpload(event, {
    formKey: 'files',
    ensure: { maxSize: '4MB', types: ['image'] },
    put: { prefix: 'avatars', addRandomSuffix: true },
  })
  if (error) throw error
  return files
})
```

```ts [server/api/avatars/[...path].get.ts]
import { createError, defineEventHandler, getRouterParam } from 'h3'
import { blob } from 'vite-hub/blob'

export default defineEventHandler(async (event) => {
  const [error, file] = await blob.get(getRouterParam(event, 'path')!)
  if (error) throw error
  if (!file) throw createError({ statusCode: 404 })
  return file
})
```

`blob.handleUpload()` checks each file's size and type, then saves it. Every async Blob method except `serve()` returns `[error, value]`. On a public store, the saved object has a `url` the browser can open. See [Blob server API](https://vitehub.dev/docs/blob/server-api) for `put()`, `list()`, `delete()`, and `head()`.

### Serve files from a private store

A private store has no public URL. Set `serve` with `authorize: true` to add a route that streams files through your app to signed-in users. It needs [Auth](https://vitehub.dev/docs/auth):

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'vercel',
    auth: true,
    blob: {
      driver: 'vercel',
      access: 'private',
      serve: { route: '/files', authorize: true },
    },
  },
})
```

### Nitro

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

export default defineConfig({
  plugins: [
    vitehub({
      host: 'vercel',
      auth: true,
      blob: {
        driver: 'vercel',
        access: 'private',
        serve: { route: '/files', authorize: true },
      },
    }),
    nitro(),
  ],
})
```

### Vite

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

export default defineConfig({
  plugins: [
    vitehub({
      host: 'vercel',
      auth: true,
      blob: {
        driver: 'vercel',
        access: 'private',
        serve: { route: '/files', authorize: true },
      },
    }),
  ],
})
```

`/files/avatars/ada.png` now returns `avatars/ada.png` from the store to a signed-in user, and `401` to a signed-out one. Export `authorize` from `server/blob.ts` to decide which user can read which file.

> **Warning**

Use `serve: { access: 'public' }` only when anyone may read every file of the store. `access` on the Vercel Blob store sets the provider access, and a public object can be read from its Vercel Blob URL without the serve route. So `access: 'public'` on a store that `serve: { authorize: true }` serves fails with `BLOB_C0022`.

See [Serve blob-backed assets](https://vitehub.dev/docs/blob/configure#serve-blob-backed-assets) and [Protect served objects](https://vitehub.dev/docs/blob/configure#protect-served-objects).

## How to use Vercel Blob locally

With the `vercel` host, the dev server also uses Vercel Blob and needs the token. Link the app to the project and copy its variables to `.env.local`:

```bash [Terminal]
vercel link
vercel env pull
pnpm dev
```

Nitro loads `.env` and `.env.local` at dev server start. The store connection must include the **Development** environment, or `vercel env pull` writes no token. The dev server uses the real store, so use a separate dev store for test data.

While the dev server runs, read and write files from another terminal:

```bash [Terminal]
pnpm vitehub blob list --prefix avatars/
pnpm vitehub blob put avatars/ada.png ./ada.png
pnpm vitehub blob get avatars/ada.png --output ./copy.png
```

See [Blob hosts](https://vitehub.dev/docs/blob/hosts#read-and-write-blobs-during-development) for all commands.

## Deploy to Vercel

Vercel deploys Vite and Nitro apps with [zero configuration](https://vercel.com/docs/frameworks/full-stack/vite-with-nitro). Connect the Git repository to a Vercel project, or run `vercel deploy` from the app root. If your configured token matches `BLOB_READ_WRITE_TOKEN`, ViteHub masks it at build time and reads it from that variable at runtime.

## Vercel Blob limits

Some limits come from Vercel and some from the ViteHub driver. Read [Vercel Blob pricing and limits](https://vercel.com/docs/vercel-blob/usage-and-pricing) and [Vercel Functions limits](https://vercel.com/docs/functions/limitations#request-body-size).

- A Vercel Function accepts a request body of at most 4.5 MB. A form upload with `blob.handleUpload()` sends the file through your function, so keep `ensure.maxSize` under that. A larger request fails with `413`, also in `vite dev` for Vercel. For larger files, use [direct uploads](https://vitehub.dev/docs/blob/server-api#direct-uploads). See [Blob limits](https://vitehub.dev/docs/blob/limits-and-errors#limits).
- The [multipart upload](https://vitehub.dev/docs/blob/server-api#multipart-uploads) route also sends each part through your function, and Vercel Blob needs parts of at least 5 MiB (except the last). So on Vercel, a file over 4.5 MB cannot use it either.
- The `vercel` driver signs a `PUT` with a Vercel client token, as `@vercel/blob/client` does, so `handleUpload({ direct: true })` sends large files straight to Vercel Blob. It needs `BLOB_READ_WRITE_TOKEN`: a store that only uses Vercel OIDC cannot sign. It does not sign a `GET`, so serve private files with `blob.serve()`.
- The driver rejects `customMetadata` with `BLOB_R0011`. [Media records](https://vitehub.dev/docs/blob/media) still work, because they are JSON objects in the same store.
- ViteHub sets `allowOverwrite: true` by default, so a write to an existing pathname replaces the file. The Vercel SDK rejects it by default; set `allowOverwrite: false` to keep that.
- After an overwrite or delete, the Vercel cache can serve the old file for up to 60 seconds.
- A file can be up to 5 TB. Vercel caches files up to 512 MB.
- Operations per minute: Hobby allows 1,200 simple and 900 advanced, Pro 7,200 simple and 4,500 advanced. Each deleted file is one operation.

## Move the same code to another host

Routes that import `blob` stay the same. Change the host, and ViteHub picks the new host's default store:

| Host | Default Blob store |
| --- | --- |
| `cloudflare` | A Cloudflare R2 bucket named after the app, with binding `BLOB`. |
| `netlify` | Netlify Blobs. |
| `node` | Files in `.vitehub/data/blob` on the local disk. |

Run `vitehub provision run --host cloudflare` to create the R2 bucket. See [Blob hosts](https://vitehub.dev/docs/blob/hosts) for R2, S3, and MinIO setup, and [Blob configuration](https://vitehub.dev/docs/blob/configure#provider-options) for each driver's options.

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

- [Overview](https://vitehub.dev/raw/docs/guides.md)
- [Agents](https://vitehub.dev/raw/docs/guides/deploy-agent-cloudflare-vercel.md)
- [Chat bots](https://vitehub.dev/raw/docs/guides/slack-ai-bot.md)
- Storage
  - [Upstash Redis](https://vitehub.dev/raw/docs/guides/upstash-redis.md)
  - [Cloudflare R2](https://vitehub.dev/raw/docs/guides/cloudflare-r2.md)
  - [Vercel Blob](https://vitehub.dev/raw/docs/guides/vercel-blob.md) (this page)
- [Databases](https://vitehub.dev/raw/docs/guides/turso.md)
- [Server work](https://vitehub.dev/raw/docs/guides/resend.md)
- [Better Auth on Cloudflare](https://vitehub.dev/raw/docs/guides/better-auth-cloudflare.md)
- [Concepts](https://vitehub.dev/raw/docs/guides/rate-limiting-algorithms.md)

Next: [Resend](https://vitehub.dev/raw/docs/guides/resend.md)

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