Vercel Blob Integration
Vercel Blob is a serverless object storage solution for storing and serving files.
Guidelines
- •Use the
@vercel/blobpackage for all blob operations. - •The integration uses the
BLOB_READ_WRITE_TOKENenvironment variable (automatically configured in Vercel deployments). - •Use
put()for uploads,del()for deletions, andlist()for listing files. - •Set
access: 'public'for publicly accessible files. - •File URLs are permanent and globally distributed via Vercel's edge network.
Example Files
app/api/upload/route.ts
typescript
import { put } from '@vercel/blob'
import { type NextRequest, NextResponse } from 'next/server'
export async function POST(request: NextRequest) {
try {
const formData = await request.formData()
const file = formData.get('file') as File
if (!file) {
return NextResponse.json({ error: 'No file provided' }, { status: 400 })
}
// Upload to Vercel Blob
const blob = await put(file.name, file, {
access: 'public',
})
return NextResponse.json({
url: blob.url,
filename: file.name,
size: file.size,
type: file.type,
})
} catch (error) {
console.error('Upload error:', error)
return NextResponse.json({ error: 'Upload failed' }, { status: 500 })
}
}
app/api/delete/route.ts
typescript
import { del } from '@vercel/blob'
import { type NextRequest, NextResponse } from 'next/server'
export async function DELETE(request: NextRequest) {
try {
const { url } = await request.json()
if (!url) {
return NextResponse.json({ error: 'No URL provided' }, { status: 400 })
}
// Delete from Vercel Blob
await del(url)
return NextResponse.json({ success: true })
} catch (error) {
console.error('Delete error:', error)
return NextResponse.json({ error: 'Delete failed' }, { status: 500 })
}
}
app/api/list/route.ts
typescript
import { list } from '@vercel/blob'
import { NextResponse } from 'next/server'
export async function GET() {
try {
const { blobs } = await list()
const files = blobs.map((blob) => ({
...blob,
filename: blob.pathname.split('/').pop() || 'unknown',
}))
return NextResponse.json({ files })
} catch (error) {
console.error('Error listing files:', error)
return NextResponse.json({ error: 'Failed to list files' }, { status: 500 })
}
}