Contentful - How to change the field value from the React/Node.js environment?

477 Views Asked by At

Is there any way to change the value of some fields in a Content Type on a specific Entry from an React application. I want to simulate the like/dislike button. So when the user clicks the button, I want to change the specific value in the CMS on a specific Entry.

Is there a way, for example, to make a POST request to a Contentful API, in the React. Or to be precise since I am making API calls from Node.js environment.

1

There are 1 best solutions below

0
On BEST ANSWER

You can leverage the Contentful management API for that. Here's an example showing how to up a title field.

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getEntry('<entry_id>'))
.then((entry) => {
  entry.fields.title['en-US'] = 'New entry title'
  return entry.update()
})
.then((entry) => console.log(`Entry ${entry.sys.id} updated.`))
.catch(console.error)

To write data to Contentful you have to create a token with WRITE permission. Usually, people create a personal access token for that in the API keys section.

⚠️ But make sure to not leak your personal access token. This token has the same access rights as the logged-in user creating it. This means, that you shouldn't use a personal access token with WRITE permissions in a React application. People could find it and take over your space.

If your React application is talking to a Node.js backend that you control, that's safe.