Skip to content
Reference
22 min read

@vercel/blob

Learn how to use the Vercel Blob SDK to access your blob store from your apps in Edge Functions.

Vercel Blob is available in Beta on Hobby and Pro plans

Those with the owner, member, developer role can access this feature

To start using Vercel Blob SDK, follow the steps in our Blob quickstart docs.

A read-write token is required to interact with the Blob SDK. When you create a Blob store in your Vercel Dashboard, an Environment Variable with the value of the token is created for you. You have the following options when deploying your application:

  • If you deploy your application in the same Vercel project where your Blob store is located, you do not need to specify the token parameter, as it's default value is equal to the store's token Environment Variable
  • If you deploy your application in a different Vercel project or scope, you can create an Environment Variable there and assign the token value from your Blob store settings to this variable. You will then set the token parameter to this Environment Variable
  • If you deploy your application outside of Vercel, you can copy the token value from the store settings and pass it as the token parameter when you call a Blob SDK method

To use the methods of the Blob SDK, you will need to call them inside an Edge Function, a Serverless Function or even a browser. In the examples below, we will be using Edge Functions.

This example creates an Edge Function that accepts a file from a multipart/form-data form and uploads it to the Blob store. The function returns a unique URL for the blob.

Next.js (/app)
Next.js (/pages)
Other frameworks
app/upload/route.ts
import { put } from '@vercel/blob';
 
export const runtime = 'edge';
 
export async function PUT(request: Request) {
  const form = await request.formData();
  const file = form.get('file') as File;
  const blob = await put(file.name, file, { access: 'public' });
 
  return Response.json(blob);
}

The put method uploads a blob object to the Blob store.

put(pathname, body, options);

It accepts the following parameters:

  • pathname: (Required) A string specifying the base value of the return URL
  • body: (Required) A blob object as ReadableStream, String, ArrayBuffer or Blob based on these supported body types
  • options: (Required) A JSON object with the following required and optional parameters:
Parameter
Required
Values
access
Yes
public - Support for private is planned
contentType
No
A string indicating the media type. By default, it's extracted from the pathname's extension.
token
No
A string specifying the token to use when making requests. It defaults to process.env.BLOB_READ_WRITE_TOKEN when deployed on Vercel as explained in Read-write token. You can also pass a client token created with the generateClientTokenFromReadWriteToken method
addRandomSuffix
No
A boolean specifying whether to add a random suffix to the pathname. It defaults to true.
cacheControlMaxAge
No
A number in seconds to configure the edge and browser cache. Defaults to one year. See the caching documentation for more details.

put() returns a JSON object with the following data for the created blob object:

{
  pathname: `string`,
  contentType: `string`,
  contentDisposition: `string`,
  url: `string`
}

An example blob is:

{
  pathname: 'profilesv1/user-12345.txt',
  contentType: 'text/plain',
  contentDisposition: 'attachment; filename="user-12345.txt"',
  url: 'https://ce0rcu23vrrdzqap.public.blob.vercel-storage.com/profilesv1/user-12345-NoOVGDVcqSPc7VYCUAGnTzLTG2qEM2.txt'
}

An example blob uploaded with addRandomSuffix: false is:

{
  pathname: 'profilesv1/user-12345.txt',
  contentType: 'text/plain',
  contentDisposition: 'attachment; filename="user-12345.txt"',
  //                                               no automatic random suffix added 👇
  url: 'https://ce0rcu23vrrdzqap.public.blob.vercel-storage.com/profilesv1/user-12345.txt'
}

This example creates an Edge Function that deletes a blob object from the Blob store.

Next.js (/app)
Next.js (/pages)
Other frameworks
app/delete/route.ts
import { del } from '@vercel/blob';
 
export const runtime = 'edge';
 
export async function DELETE(request: Request) {
  const { searchParams } = new URL(request.url);
  const urlToDelete = searchParams.get('url') as string;
  await del(urlToDelete);
 
  return new Response();
}

The del method deletes a blob object from the Blob store.

del(url, options);

It accepts the following parameters:

  • url: (Required) A string or Array of strings specifying the unique URL(s) of the blob object(s) to delete
  • options: (Optional) A JSON object with the following optional parameter:
Parameter
Required
Values
token
No
A string specifying the read-write token to use when making requests. It defaults to process.env.BLOB_READ_WRITE_TOKEN when deployed on Vercel as explained in Read-write token

del() returns a void response. A delete action is always successful if the blob url exists. A delete action won't throw if the blob url doesn't exists.

This example creates an Edge Function that returns a blob object's metadata.

Next.js (/app)
Next.js (/pages)
Other frameworks
app/get-blob/route.ts
import { head } from '@vercel/blob';
 
export const runtime = 'edge';
 
export async function GET(request: Request) {
  const { searchParams } = new URL(request.url);
  const blobUrl = searchParams.get('url');
  const blobDetails = await head(blobUrl);
 
  return Response.json(blobDetails);
}

The head method returns a blob object's metadata.

head(url, options);

It accepts the following parameters:

  • url: (Required) A string specifying the unique URL of the blob object to read
  • options: (Optional) A JSON object with the following optional parameter:
Parameter
Required
Values
token
No
A string specifying the read-write token to use when making requests. It defaults to process.env.BLOB_READ_WRITE_TOKEN when deployed on Vercel as explained in Read-write token

head() returns one of the following:

  • a JSON object with the requested blob object's metadata
  • throws a BlobNotFoundError if the blob object was not found
{
  size: `number`;
  uploadedAt: `Date`;
  pathname: `string`;
  contentType: `string`;
  contentDisposition: `string`;
  url: `string`;
  cacheControl: `string`;
}

This example creates an Edge Function that returns a list of blob objects in a Blob store.

Next.js (/app)
Next.js (/pages)
Other frameworks
app/get-blobs/route.ts
import { list } from '@vercel/blob';
 
export const runtime = 'edge';
 
export async function GET(request: Request) {
  const { blobs } = await list();
  return Response.json(blobs);
}

The list method returns a list of blob objects in a Blob store.

list(options);

It accepts the following parameters:

  • options: (Optional) A JSON object with the following optional parameters:
Parameter
Required
Values
token
No
A string specifying the read-write token to use when making requests. It defaults to process.env.BLOB_READ_WRITE_TOKEN when deployed on Vercel as explained in Read-write token
limit
No
A number specifying the maximum number of blob objects to return. It defaults to 1000
prefix
No
A string used to filter for blob objects contained in a specific folder assuming that the folder name was used in the pathname when the blob object was uploaded
cursor
No
A string obtained from a previous response for pagination of retults
mode
No
A string specifying the response format. Can either be expanded (default) or folded. In folded mode all blobs that are located inside a folder will be folded into a single folder string entry

list() returns a JSON object in the following format:

blobs: {
  size: `number`;
  uploadedAt: `Date`;
  pathname: `string`;
  url: `string`;
}[]
cursor?: `string`;
hasMore: `boolean`;
folders?: `string[]`

For a long list of blob objects (the default list limit is 1000), you can use the cursor and hasMore parameters to paginate through the results as shown in the example below:

let hasMore = true;
let cursor;
 
while (hasMore) {
  const listResult = await list({
    cursor,
  });
  hasMore = listResult.hasMore;
  cursor = listResult.cursor;
}

To retrieve the folders from your blob store, alter the mode parameter to modify the response format of the list operation. The default value of mode is expanded, which returns all blobs in a single array of objects.

Alternatively, you can set mode to folded to roll up all blobs located inside a folder into a single entry. These entries will be included in the response as folders. Blobs that are not located in a folder will still be returned in the blobs property.

By using the folded mode, you can efficiently retrieve folders and subsequently list the blobs inside them by using the returned folders as a prefix for further requests. Omitting the prefix parameter entirely, will return all folders in the root of your store. Be aware that the blobs pathnames and the folder names will always be fully quantified and never relative to the prefix you passed.

const {
  folders: [firstFolder],
  blobs: rootBlobs,
} = await vercelBlob.list({
  mode: 'folded',
});
 
const { folders, blobs } = await vercelBlob.list({
  mode: 'folded',
  prefix: firstFolder,
});

This example creates an Edge Function that copies an existing blob to a new path in the store.

Next.js (/app)
Next.js (/pages)
Other frameworks
app/copy-blob/route.ts
import { copy } from '@vercel/blob';
 
export const runtime = 'edge';
 
export async function PUT(request: Request) {
  const form = await request.formData();
 
  const fromUrl = form.get('fromUrl') as string;
  const toPathname = form.get('toPathname') as string;
 
  const blob = await copy(fromUrl, toPathname, { access: 'public' });
 
  return Response.json(blob);
}

The copy method copies an existing blob object to a new path inside the blob store.

The contentType and cacheControlMaxAge will not be copied from the source blob. If the values should be carried over to the copy, they need to be defined again in the options object.

Contrary to put(), addRandomSuffix is false by default. This means no automatic random id suffix is added to your blob url, unless you pass addRandomSuffix: true. This also means copy() overwrites files per default, if the operation targets a pathname that already exists.

copy(fromUrl, toPathname, options);

It accepts the following parameters:

  • fromUrl: (Required) A blob URL identifying an already existing blob
  • toPathname: (Required) A string specifying the new path inside the blob store. This will be the base value of the return URL
  • options: (Required) A JSON object with the following required and optional parameters:
Parameter
Required
Values
access
Yes
public - Support for private is planned
contentType
No
A string indicating the media type. By default, it's extracted from the toPathname's extension.
token
No
A string specifying the token to use when making requests. It defaults to process.env.BLOB_READ_WRITE_TOKEN when deployed on Vercel as explained in Read-write token
addRandomSuffix
No
A boolean specifying whether to add a random suffix to the pathname. It defaults to false.
cacheControlMaxAge
No
A number in seconds to configure the edge and browser cache. Defaults to one year. See the caching documentation for more details.

copy() returns a JSON object with the following data for the copied blob object:

{
  pathname: `string`,
  contentType: `string`,
  contentDisposition: `string`,
  url: `string`
}

An example blob is:

{
  pathname: 'profilesv1/user-12345-copy.txt',
  contentType: 'text/plain',
  contentDisposition: 'attachment; filename="user-12345-copy.txt"',
  url: 'https://ce0rcu23vrrdzqap.public.blob.vercel-storage.com/profilesv1/user-12345-copy.txt'
}

As seen in the quickstart docs, you can upload files directly from clients (like browsers) to the Blob store.

All client uploads related methods are available under @vercel/blob/client.

The upload method is dedicated to client uploads. It fetches a client token on your server using the handleUploadUrl before uploading the blob. Read the client uploads documentation to know more.

upload(pathname, body, options);

It accepts the following parameters:

  • pathname: (Required) A string specifying the base value of the return URL
  • body: (Required) A blob object as ReadableStream, String, ArrayBuffer or Blob based on these supported body types
  • options: (Required) A JSON object with the following required and optional parameters:
Parameter
Required
Values
access
Yes
public - Support for private is planned
contentType
No
A string indicating the media type. By default, it's extracted from the pathname's extension.
handleUploadUrl
Yes*
A string specifying the route to call for generating client tokens for client uploads.
clientPayload
No
A string to be sent to your handleUpload server code. Example use-case: attaching the post id an image relates to. So you can use it to update your database.

upload() returns a JSON object with the following data for the created blob object:

{
  pathname: `string`;
  contentType: `string`;
  contentDisposition: `string`;
  url: `string`;
}

An example url is:

url: "https://ce0rcu23vrrdzqap.public.blob.vercel-storage.com/profilesv1/user-12345-NoOVGDVcqSPc7VYCUAGnTzLTG2qEM2.txt"

A server-side route helper to manage client uploads, it has two responsibilities:

  1. Generate tokens for client uploads
  2. Listen for completed client uploads, so you can update your database with the URL of the uploaded file for example
handleUpload(options);

It accepts the following parameters:

  • options: (Required) A JSON object with the following parameters:
Parameter
Required
Values
token
No
A string specifying the read-write token to use when making requests. It defaults to process.env.BLOB_READ_WRITE_TOKEN when deployed on Vercel as explained in Read-write token
request
Yes
An IncomingMessage or Request object to be used to determine the action to take
onBeforeGenerateToken
Yes
A function to be called right before generating client tokens for client uploads. See below for usage
onUploadCompleted
Yes
A function to be called by Vercel Blob when the client upload finishes. This is useful to update your database with the blob url that was uploaded
body
Yes
The request body

handleUpload() returns:

Promise<
  | { type: 'blob.generate-client-token'; clientToken: string }
  | { type: 'blob.upload-completed'; response: 'ok' }
>

Here's an example Next.js App Router route handler that uses handleUpload():

app/api/post/upload/route.ts
import { handleUpload, type HandleUploadBody } from '@vercel/blob/client';
import { NextResponse } from 'next/server';
 
// Use-case: uploading images for blog posts
export async function POST(request: Request): Promise<NextResponse> {
  const body = (await request.json()) as HandleUploadBody;
 
  try {
    const jsonResponse = await handleUpload({
      body,
      request,
      onBeforeGenerateToken: async (
        pathname: string,
        clientPayload?: string,
      ) => {
        // Generate a client token for the browser to upload the file
        // ⚠️ Authenticate and authorize users before generating the token.
        // Otherwise, you're allowing anonymous uploads.
 
        // ⚠️ When using the clientPayload feature, make sure to valide it
        // otherwise this could introduce security issues for your app
        // like allowing users to modify other users' posts
 
        return {
          allowedContentTypes: ['image/jpeg', 'image/png', 'image/gif'], // optional, default to all content types
          // maximumSizeInBytes: number, optional, default to 500MB
          // validUntil: number, optional, timestamp in ms, by default now + 30s (30,000)
          // addRandomSuffix: boolean, optional, allows to disable or enable random suffixes (defaults to `true`)
          // cacheControlMaxAge: number, optional, a duration in seconds to configure the edge and browser caches.
          tokenPayload: JSON.stringify({
            // optional, sent to your server on upload completion
            // you could pass a user id from auth, or a value from clientPayload
          }),
        };
      },
      onUploadCompleted: async ({ blob, tokenPayload }) => {
        // Get notified of client upload completion
        // ⚠️ This will not work on `localhost` websites,
        // Use ngrok or similar to get the full upload flow
 
        console.log('blob upload completed', blob, tokenPayload);
 
        try {
          // Run any logic after the file upload completed,
          // If you've already validated the user and authorization prior, you can
          // safely update your database
        } catch (error) {
          throw new Error('Could not update post');
        }
      },
    });
 
    return NextResponse.json(jsonResponse);
  } catch (error) {
    return NextResponse.json(
      { error: (error as Error).message },
      { status: 400 }, // The webhook will retry 5 times waiting for a 200
    );
  }
}

When you make a request to the SDK using any of the above methods, they will return an error if the request fails due to any of the following reasons:

  • Missing required parameters
  • An invalid token or a token that does have access to the Blob object
  • Suspended Blob store
  • Blob file or Blob store not found
  • Unforeseen or unknown errors

To catch these errors, wrap your requests with a try/catch statement as shown below:

import { put, BlobAccessError } from '@vercel/blob';
 
try {
  await put(...);
} catch (error) {
  if (error instanceof BlobAccessError) {
    // handle a recognized error
  } else {
    // throw the error again if it's unknown
    throw error;
  }
}
Last updated on November 28, 2023