Using Upload API

The upload API lets you upload files, such as images and videos, to be used within Builder.

A sample node js fetch code:

const fetch = require("node-fetch");
const fs = require('fs');

function upload_image(file) {
  // read binary data
  var bitmap = fs.readFileSync(file);
  fetch("https://builder.io/api/v1/upload", {
    method: "POST",
    body: bitmap,
    headers: {
     "Authorization": "Bearer builder-private-key",
     "Content-Type": "image/jpeg"
    },
 }).then(res => {
      return res.json();
 }).then(resp => {
     console.log(resp);
 }).catch((e) => console.log(e));
}
upload_image('./path-to-media');

Sample Javascript Fetch

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer builder-private-key");
myHeaders.append("Content-Type", "image/jpeg");

var file = "<file contents here>"; // Binary data

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: file,
  redirect: 'follow'
};

fetch("https://builder.io/api/v1/upload", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
1 Like