Update CMS Item Fields via API

Hi,

I am trying to do a bulk change in one collection via API. This is the piece of code:

function updateItem(item) { return webflow .updateItem({ collectionId: guidesId, itemId: item.id, fields: { name: item.name, slug: item.slug, _archived: item.archived, _draft: item.draft, cta: '2' } }) .catch(err => console.error(err)); }

If I run it like this, I get the following error:

'Field \'slug\': Unique value is already in database: \'my-awesome-slug\''

However, if I dont submit slug, I get this:
'Field \'slug\': Field is required'

I don’t understand why I have to resubmit fields that are already filled. The only field I care about is “cta-1”. Can someone help me?

I will answer this myself:
Only updating the fields that need to change is documented here:
https://developers.webflow.com/?shell#patch-collection-item

Javascript SDK does not currently support it, so a custom request like this is necessary:

function updateItem({
  webflow, collectionId, itemId, map
}) {
  // currently not implemented in webflow-api package
  const url = `https://api.webflow.com/collections/${collectionId}/items/${itemId}`;
  const options = {
    body: JSON.stringify({ fields: map }),
    method: 'PATCH',
    headers: {
      Authorization: `Bearer ${webflow.token}`,
      'Accept-Version': '1.0.0',
      'Content-Type': 'application/json'
    }
  };
  return fetch(url, options);
}
1 Like