Last active
December 3, 2025 15:51
-
-
Save taprile314/c55cde342b2645f83a9e3658e058209c to your computer and use it in GitHub Desktop.
Vercel Serverless Function Fetch API - Upload file to Vercel Blob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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