Updating media via API

Hi,

Im trying update the media via API according to this article described by Dan:

But even if I specify the oldMediaId the media will be uploaded as a new item with a new ID:

curl -X POST http://x.x.x.x/api/library -H ‘accept: application/json’ -H ‘Content-Type: multipart/form-data’ -H ‘Authorization: Bearer xxxxxxxxxxxxxxxxxxxx’ -F ‘files=@fab-1.jpg;type=image/jpeg’ -F updateInLayouts=1 -F deleteOldRevisions=1 -F oldMediaId=141

{“files”:[{“name”:“fab-1.jpg”,“size”:334847,“type”:“image/jpeg”,“url”:“/api/library?file=fab-1.jpg&download=1”,“mediaId”:161,“storedas”:“161.jpg”,“duration”:10,“retired”:0,“fileSize”:334847,“md5”:“ac0d09df767a61c94889d5de5cbe3309”,“delete_url”:“/api/library?file=fab-1.jpg”,“delete_type”:“DELETE”}]}

Am I doing something wrong? Thanks
Zoltan

The post you linked to explains this.

Media items cannot be changed without getting a new number. Every time you upload a new media item, or a new version of a media item, you will get a new ID. If you opt to have the CMS update layouts, then it will check through all layouts the user has permission to and update the media items in those widgets to the new version.

Hi Alex,

Thanks, sorry it was not clear for me, so as soon as I upload the new media item with the oldMediaId option, it will update the layouts as well?

Ok, maybe let me go back two steps and describe the need. There is a content (set of JPGs) which is regularly changing hence we would like to automate this with the API.
But if the oldMediaId attribute is changing all the time, it means when we are uploading the new content we need to store the new MediaIDs so that we can reference it next time. Is my understanding correct then?

Thanks
Zoltan

If you pass in both the oldMediaId and updateInLayouts=1 then it will update the image in the layouts where the user your API integration is running as has permission to edit.

You’re correct you would either need to store the old ID, or it’s more usual to query the library for a media item with a specific name or tag, get its ID, and then use that in your call to replace the image.