Updating a record with array of attached files

Hi, I have a collection where the records have an array of attached files. I found that if I send a put to the content api “https://site-api.datocms.com/items/167588323”, it will replace the whole array I send with the old one, is there a way to just add the new attached files to the already existing array instead or am I doing this the right and only way? Thanks.

{
   "data":{
      "id":"167588323",
      "type":"item",
      "attributes":{
         "body":"test",
         "title":"test",
         "distributorid":999998,
         "author":"test",
         "audience":[
            "132964517",
            "60954060"
         ],
         "attachedfiles":[
            {
               "upload_id":"66508869",
               "FileName":"bg.png",
               "MimeType":"image/png",
               "Url":"https://www.datocms-assets.com/65985/1691077851-bg.png",
               "Title":null,
               "Format":"png",
               "created_at":"0001-01-01T00:00:00",
               "updated_at":"0001-01-01T00:00:00",
               "Content":null,
               "file_length":0,
               "DistributorNumber":0
            },
            {
               "upload_id":"66508870",
               "FileName":"logobar.png",
               "MimeType":"image/png",
               "Url":"https://www.datocms-assets.com/65985/1691077868-logobar.png",
               "Title":null,
               "Format":"png",
               "created_at":"0001-01-01T00:00:00",
               "updated_at":"0001-01-01T00:00:00",
               "Content":null,
               "file_length":0,
               "DistributorNumber":0
            }
         ]
      },
      "relationships":{
         "item_type":{
            "data":{
               "id":"632150",
               "type":"item_type"
            }
         }
      }
   }
}

Hello @jterry

As you said, the only way to do so is to send all of the assets you want to be on the field after you update it. This is done intentionally as we can’t know if you omitted purposely an asset to remove it from the field, or if you just want to add one. That is, the “remove all assets and keep only the one i’m sending” and “keep all assets and add just this one” would be the same, and non differentiable, thats why we went with this approach, of having to set all assets when updating the field.

Hope this clears things up!
Let me know if you have any other doubts :slight_smile:

Unless I’m misunderstanding you, it seems like you have an existing record that links to a set of Dato assets.

If that’s the case then maybe this will help.

When I do this I normally fetch the record bringing down an array of existing asset IDs, then I have a method that uploads each new asset which returns it’s ID.

I then append these ids to the array.

Then its a simple case of updating the record with the new array.

Hope that helps.