# Unable to upload a new file using the response from the Get Customization API

**URL:** https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676
**Category:** General
**Tags:** rest-api, get-customization-api, filekey, upload-files, file-not-found-error
**Created:** [November 18, 2022, 2:18am UTC](https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676 "2022-11-18T02:18:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![TylerR](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@TylerR](https://forum.kintone.dev/u/TylerR)
#### Post date: [November 18, 2022, 2:18am UTC](https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676/1 "2022-11-18T02:18:36Z")

</div>

I am using the [Get Customization](https://kintone.dev/en/docs/kintone/rest-api/apps/get-customization/) REST API (Endpoint: `/k/v1/app/customize`).  
This is returning correctly.  
However, when I try to use the returned `fileKey`s, I get an error saying, `The specified file (id: XXXXXXXX) not found.`.

I am trying to use the [Get Customization API](https://kintone.dev/en/docs/kintone/rest-api/apps/get-customization/)'s response to upload a new file.

The file that I upload via the [Upload File API](https://kintone.dev/en/docs/kintone/rest-api/files/upload-file/) works without problem.

## Goal

My goal is to add a customization file, however with out the other file keys for the current customization is overwrites the current files that are attached to the app.

## Script

```javascript
#save() {
  // Upload file
  this.#upload().then(data => {
    // Attach file to app
    this.#attachFile(data.fileKey).then(resp => {
      console.log(resp)
    }).catch(err => {
      console.error(err)
    })
  }).catch(err => {
    console.error(err)
  })
}

#getCustomizations() {
  return new Promise((resolve, reject) => {
    kintone.api(
      kintone.api.url('/k/v1/app/customize'),
      'GET',
      { app: this.#appID },
      resp => resolve(resp),
      err => reject(err)
    )
  })
}

#createBlob() {
  // Create file
  const CONTENT = "const TEST_VAR = true;"
  return new Blob([CONTENT], {
    type: "text/javascript"
  })
}

#upload() {
  return new Promise((resolve, reject) => {

    const FORM_DATA = new FormData()

    FORM_DATA.append(' __REQUEST_TOKEN__', this.#requestToken);
    FORM_DATA.append("file", this.#createBlob(), "CustomizationConfigLib_Configs.js")

    var url = `https://${location.host}/k/v1/file.json`;
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    xhr.onload = function () {
      if (xhr.status === 200) {
        resolve(JSON.parse(xhr.responseText));
      } else {
        reject(JSON.parse(xhr.responseText));
      }
    };
    xhr.send(FORM_DATA);
  })
}

    async #attachFile(fileKey) {
  const BODY = await this.#getCustomizations()
  BODY.app = this.#appID
  BODY.desktop.js.push({
    type: "FILE",
    file: { fileKey }
  })

  console.log(BODY)

  return new Promise((resolve, reject) => {
    kintone.api(
      kintone.api.url('/k/v1/preview/app/customize'),
      'PUT',
      BODY,
      resp => resolve({ resp, revision: BODY.revision }),
      err => reject(err)
    );
  })
}

```

## Problem

Everything works, except I get the error for the `fileKey` not found.

I have checked and know that the `fileKey` in question is referencing the already attached customization file.

---

<div class="post-metadata">

### Author: ![Genji](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/genji/32/646_2.png) [@Genji](https://forum.kintone.dev/u/Genji)
#### Post date: [November 18, 2022, 9:15am UTC](https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676/2 "2022-11-18T09:15:20Z")

</div>

Hello @TylerR ,

Welcome to the Kintone Developer Forum & thank you for posting your question!  
Thank you for posting your script & error message!

Few questions to better help you out:

Could you kindly elaborate on what is your goal?  
Specifically, what do you mean by this sentence:

> [@TylerR](#):
>
> My goal is to add a customization file, however with out the other file keys for the current customization is overwrites the current files that are attached to the app.

Thank you ~

---

<div class="post-metadata">

### Author: ![TylerR](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@TylerR](https://forum.kintone.dev/u/TylerR)
#### Post date: [November 18, 2022, 12:10pm UTC](https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676/3 "2022-11-18T12:10:15Z")

</div>

I am playing around with the different APIs. Just testing them out and learning what tools Kintone has.

My **goal** is to **add a JS file dynamically to the desktop customization** for an App.

---

<div class="post-metadata">

### Author: ![Chris](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/chris/32/44_2.png) [@Chris](https://forum.kintone.dev/u/Chris)
#### Post date: [November 22, 2022, 6:50pm UTC](https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676/4 "2022-11-22T18:50:50Z")

</div>

Hello @TylerR

If you obtain a `filekey` by a method other than the [Upload File API](https://kintone.dev/en/docs/kintone/rest-api/files/upload-file/) and upload files, an error you saw will occur.

**You must use the Upload File API to obtain `filekey`s.**

## To attach files in Kintone

I'm sure you already know this, but as a note for everyone else, to attach files in Kintone, you must use the Upload File API to upload on your Kintone environment first.

And then, you must attach the file to a record, or else the file will be deleted after 3 days if the [Add Record API](https://kintone.dev/en/docs/kintone/rest-api/records/add-record/) or [Update Record API](https://kintone.dev/en/docs/kintone/rest-api/records/update-record/) is not used.

I hope this helps.

---

<div class="post-metadata">

### Author: ![TylerR](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@TylerR](https://forum.kintone.dev/u/TylerR)
#### Post date: [November 23, 2022, 12:26pm UTC](https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676/5 "2022-11-23T12:26:33Z")

</div>

Ok, so I would first have to download the current customizations, then upload them all to gain the fileKeys for and then I could upload without loosing the currently attached files.

Is this correct?

---

<div class="post-metadata">

### Author: ![Chris](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/chris/32/44_2.png) [@Chris](https://forum.kintone.dev/u/Chris)
#### Post date: [November 29, 2022, 1:56am UTC](https://forum.kintone.dev/t/unable-to-upload-a-new-file-using-the-response-from-the-get-customization-api/676/6 "2022-11-29T01:56:47Z")

</div>

Hello @TylerR

Theoretically, yes.  
Use the Download File API to download the file and upload them.
