Upload files via API

Upload a file via the API directly.

Step by Step.

  • Create a formdata object.
  • Append the file as a buffer or from a multipart upload.
  • Pass your API key to the api-key header
  • Response contains the URL to the file that you can store in your database.

Express.js example.

// POST route for file upload
app.post('/upload_file', async (req, res) => {
 const file = req.file; // Assuming the file is sent as part of the request

 // Create a new FormData object and append the file to it
 const formData = new FormData();
 formData.append('file', file);

 try {
   // Make a POST request to the Uploadfast API endpoint
   const response = await fetch('https://uploadfast-server.fly.dev/upload', {
    method: 'POST',
    headers: {
      'api-key': process.env.UPLOADFAST_KEY
    },
    body: formData
    });

  // Your uploaded file
  const data = await response.json()
  // Get data about the file
  const {url, file_name} = data[0]

  // Persist URI to database..

  return new Response('Files uploaded successfully', {status: 200});

  } catch (error) {
    console.error(error);
    return new Response('File upload failed', {
      status: 500
    })
  }
});