Skip to content

Instantly share code, notes, and snippets.

@taprile314
Last active December 3, 2025 15:51
Show Gist options
  • Select an option

  • Save taprile314/c55cde342b2645f83a9e3658e058209c to your computer and use it in GitHub Desktop.

Select an option

Save taprile314/c55cde342b2645f83a9e3658e058209c to your computer and use it in GitHub Desktop.
Vercel Serverless Function Fetch API - Upload file to Vercel Blob
import { put } from '@vercel/blob';
/**
* Serverless API handler for image upload to Vercel Blob Storage
* Using Vercel Web Standard fetch API
*/
export default {
/**
* @param {Request} request - Web Standard Request object containing multipart form data
*/
async POST(request) {
try {
// Parse form data from request
const formData = await request.formData() ?? new FormData();
const file = formData.get('file') || formData.get('image');
if (!file) {
return new Response(JSON.stringify({ error: 'No file provided' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
// Upload to Vercel Blob
const blob = await put(file.name, file, {
access: 'public',
});
return new Response(JSON.stringify({
success: true,
url: blob.url
}), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
} catch (error) {
console.error('Upload error:', error);
return new Response(JSON.stringify({ error: 'Upload failed' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment