# Uploading images from backend using wix-media-backend

**URL:** <https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [April 2, 2019, 1:03pm UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665 "2019-04-02T13:03:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemisphere81](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/hemisphere81/32/10458_2.png) [@hemisphere81](https://forum.wixstudio.com/u/hemisphere81)\
**Post date:** [April 2, 2019, 1:03pm UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665/1 "2019-04-02T13:03:21Z")

</div>

I’ve created a site API to upload images to the Media Manager. I used this documentation:  
[https://support.wix.com/en/article/wix-code-exposing-a-site-api-with-http-functions](https://support.wix.com/en/article/wix-code-exposing-a-site-api-with-http-functions)  
[https://www.wix.com/code/reference/draft/wix-media-backend.html](https://www.wix.com/code/reference/draft/wix-media-backend.html)  
It is kind of strange to see ‘draft’ in the second URL. Brings me to my first question: **is this API ready to be used and did someone has already managed to use the upload function?**

Anyhow, in my http-functions.js I have

```javascript
 
import {uploadImage} from 'backend/media';
...
 export function post_image(request) {

 let options = {
 "headers": {
 "Content-Type": "application/json"
    }
  };

 return request.body.text()
    .then((body) => {
 let record = JSON.parse(body);
 let image = CryptoJS.enc.Base64.parse(record.image);
 ...
 return uploadImage(image, record.filename, mime)
.then((fileInfo) =>
...
 .catch((error) => {
      options.body = {
        "error": error.message
       };
        return serverError(options);
  }); 

```

The image comes in as a base64 String. Is this the right way to convert it to a Buffer variable?

In media.js there is:

```javascript
export function uploadImage(buffer, filename, mime) {
 
 return wixMedia.upload(
 "/Galleries/",
    buffer,
    filename,
    {
 "mediaOptions": {
 "mimeType": mime,
 "mediaType": "image"
      },
 "metadataOptions": {
 "isPrivate": false,
 "isVisitorUpload": false,
 "context": {
 "someKey1": "someValue1",
 "someKey2": "someValue2"
        }
      }
    }
  );
}

```

Result: upload() throws an error, resulting in returning a ServerError response  
“error”:“Cannot read property ‘mediaType’ of undefined”  
The folder ‘Galleries’ defined in the function, does get created… So I guess that either the upload isn’t working yet because of the draft notion, or that I just didn’t converted my image to the right Buffer type.

Does anyone can help me with this?

---

<div class="post-metadata">

**Author:** ![chris-derrell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/chris-derrell/32/1774_2.png) [@chris-derrell](https://forum.wixstudio.com/u/chris-derrell)\
**Post date:** [October 21, 2019, 9:15pm UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665/2 "2019-10-21T21:15:24Z")

</div>

I think they just recently moved it out of draft. I tried to use it while it was in the draft only as well last month, and it didn’t work for me at the time.

---

<div class="post-metadata">

**Author:** ![javierantonf](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/javierantonf/32/14459_2.png) [@javierantonf](https://forum.wixstudio.com/u/javierantonf)\
**Post date:** [February 3, 2020, 10:04am UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665/3 "2020-02-03T10:04:01Z")

</div>

The trick is to use Node.js’s Buffer class to create the buffer object from a Base64 string. Like here:

```javascript
var buffer = new Buffer.from(unescape(body.base64Content), 'base64');

```

---

<div class="post-metadata">

**Author:** ![hemisphere81](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/hemisphere81/32/10458_2.png) [@hemisphere81](https://forum.wixstudio.com/u/hemisphere81)\
**Post date:** [April 5, 2020, 3:06pm UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665/4 "2020-04-05T15:06:41Z")

</div>

Well at least it works now  
The https functions gets a POST request with a Dropbox shared URL, this URL is used for uploading it to the Media manager.

---

<div class="post-metadata">

**Author:** ![nikosp420](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/nikosp420/32/18323_2.png) [@nikosp420](https://forum.wixstudio.com/u/nikosp420)\
**Post date:** [April 23, 2020, 4:56pm UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665/5 "2020-04-23T16:56:46Z")

</div>

how you accomplished that? got same problem

---

<div class="post-metadata">

**Author:** ![hemisphere81](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/hemisphere81/32/10458_2.png) [@hemisphere81](https://forum.wixstudio.com/u/hemisphere81)\
**Post date:** [May 2, 2020, 9:30am UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665/6 "2020-05-02T09:30:38Z")

</div>

I made an external API in integromat. From Wix I send a trigger and this external flow goes to a dropbox folder (transferred into that trigger). This flow goes and shares every file in that folder and this generates a unique link (you can set the lifespan of that link - i’ve set it to 2mins in order to let the upload happen correctly); this external API calls a webhook in Wix and transfers each Dropbox URL to the WixBackend and there I upload the given dropboxURL into the media manager.

```javascript
export function uploadImageFromUrl(url,shootname, filename, mime) {
 return rp.get({ url, encoding: null })
    .then( (image) => {
 return mediaManager.upload(
 "/Galleries/" + shootname,
        image,
        filename,
        {
 "mediaOptions": {
 "mimeType": mime,
 "mediaType": "image"
          },
 "metadataOptions": {
 "isPrivate": false,
 "isVisitorUpload": false,
 "context": {
 "shoot": shootname,
 "filename": filename
            }
          }
        }
      );
    } );
 }

```

To be complete, I know there is a Dropbox integration to upload from the user interface but I need these pictures to be uploaded into a DataCollection the same time. Thats why I do it like this. After uploading Wix returns me Wix-URL and this goes into the database.

---

<div class="post-metadata">

**Author:** ![singhalvinayiet](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/singhalvinayiet/32/12885_2.png) [@singhalvinayiet](https://forum.wixstudio.com/u/singhalvinayiet)\
**Post date:** [May 20, 2020, 5:17am UTC](https://forum.wixstudio.com/t/uploading-images-from-backend-using-wix-media-backend/23665/7 "2020-05-20T05:17:52Z")

</div>

I want to upload images in media folder using third party API.

Third party API which is return image as json format.

Please help me ASAP because it’s very urgent and I am beginner in WIX server.
