# Using Upload API

**URL:** <https://forum.builder.io/t/using-upload-api/3482>\
**Category:** Technical Questions\
**Tags:** upload-api, images, files\
**Created:** [April 13, 2023, 10:52am UTC](https://forum.builder.io/t/using-upload-api/3482 "2023-04-13T10:52:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [April 13, 2023, 10:52am UTC](https://forum.builder.io/t/using-upload-api/3482/1 "2023-04-13T10:52:21Z")

</div>

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

A sample node js fetch code:

```auto
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

```auto
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));

```

> **[Upload API - Builder.io](https://www.builder.io/c/docs/upload-api#upload-api)**
>
> Drag and drop with your components, right within your existing site or app. Build and optimize digital experiences for any tech stack, visually.

---

<div class="post-metadata">

**Author:** ![hafiz](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@hafiz](https://forum.builder.io/u/hafiz)\
**Post date:** [July 13, 2023, 1:17am UTC](https://forum.builder.io/t/using-upload-api/3482/2 "2023-07-13T01:17:29Z")

</div>

@manish-sharma Hey man,  
I’m facing Authorization issue

{  
“status”: 401,  
“message”: “Authorization required”,  
“detail”: “For help please contact [support@builder.io](mailto:support@builder.io)”  
}

Here is my code

```auto

  <TextField type="file" onChange={handleImageChange} />

  const handleImageChange = async (event: any) => {
    const file = event.target.files[0];
    try {
      const formData = new FormData();
      formData.append("file", file);
      const response = await uploadFileImageHandler(formData);
      console.log("Uploaded image URL:", response);
    } catch (error) {
      console.error("Error uploading image:", error);
    }
  };

import { fileUploadEndpoints } from "./constant";

export const uploadFileImageHandler = async (value: any) => {
  try {
    const response = await fetch(`${fileUploadEndpoints.fileUpload}`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${process.env.NEXT_PUBLIC_BUILDERIO_PUBLIC_API_KEY}`,
        "Content-Type": "image/png",
      },
      body: value,
    });
    const data = await response.json();
    return data;
  } catch (error) {
    return error;
  }
};

export const fileUploadEndpoints = Object.freeze({
  fileUpload: "https://builder.io/api/v1/upload",
});

```

---

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [July 13, 2023, 3:20am UTC](https://forum.builder.io/t/using-upload-api/3482/3 "2023-07-13T03:20:23Z")

</div>

Hello @hafiz,

I see you are using a public API key for the Bearer token which isn’t going to work, you will need to generate a private key and use that. You can refer to the below forum post for help on generating private key

> [@Create Private Pages that are accessible only after authorization](https://forum.builder.io/t/create-private-pages-that-are-accessible-only-after-authorization/58/2):
>
> Howdy @yashu, welcome to the forum! You can do this using a recently added model setting called public readable. You can go over to [builder.io/models](http://builder.io/models) and choose your model and hit show more options under model settings and then find the public readable option and turn that off and save your model, e.g. perhaps make a new private-page model of page type with this setting switched off Note that this model option is currently only live on [beta.builder.io](http://beta.builder.io) but will be in the production [b…](http://builder.io)

---

<div class="post-metadata">

**Author:** ![sohaibVanguard-x](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@sohaibVanguard-x](https://forum.builder.io/u/sohaibVanguard-x)\
**Post date:** [August 2, 2023, 6:44am UTC](https://forum.builder.io/t/using-upload-api/3482/4 "2023-08-02T06:44:17Z")

</div>

Hi @manish-sharma,  
i am trying to upload video using [builder.io](http://builder.io) api  
my code is

```auto
<TextField type="file" onChange={handleImageChange} />

 const handleImageChange = async (event: any) => {
    setIsLoading(true)
    const file = event.target.files[0]
    const fileName = file.name
    const fileExtension = fileName.split('.').pop()
    try {
      const response: any = await uploadFileVideoHandler(file, fileExtension)
      if (response && response?.url!) {
        setFeaturedVideoUrl(response?.url)
      }
      setIsLoading(false)
    } catch (err) {
      setIsLoading(false)
      showErrorSnackBar('Please try again to upload image')
    }
  }

export const uploadFileVideoHandler = async (
  value: any,
  fileExtension: string,
) => {
  try {
    const myHeaders = new Headers()
    myHeaders.append(
      'Authorization',
      `Bearer ${process.env.NEXT_PUBLIC_BUILDERIO_PRIVATE_API_KEY}`,
    )
    myHeaders.append('Content-Type', `video/${fileExtension}`)
    const requestOptions = {
      method: 'POST',
      headers: myHeaders,
      body: value,
    }
    const response = await fetch(
      `${fileUploadEndpoints.fileUpload}`,
      requestOptions,
    )
    const result = await response.json()
    return result
  } catch (error) {
    return error
  }
}

```

i am getting 500 error

---

<div class="post-metadata">

**Author:** ![OmPrakash](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/omprakash/32/2751_2.png) [@OmPrakash](https://forum.builder.io/u/OmPrakash)\
**Post date:** [August 2, 2023, 9:57am UTC](https://forum.builder.io/t/using-upload-api/3482/5 "2023-08-02T09:57:18Z")

</div>

Verifying that the server expects the file to be uploaded with the correct content type. In this case, the content type is set as `video/${fileExtension}` , which may not be suitable for all servers. Some servers may expect `multipart/form-data` or other specific content types for file uploads.

If possible, could you try to test the server-side file upload functionality using tools like Postman or cURL to isolate the issue further?
