Show examples in:
Javascript HTTP

Content Management API > Record

Update a record

Updating record is very similar to creating a new one from scratch, so make sure to read that guide and all the examples for more details.

When updating you can pass just the fields you want to change.

Updating and version locking

DatoCMS optionally supports optimistic locking. When updating an existing record, you can specify its current version within the appropriate meta property. DatoCMS compares this version with the current version stored to ensure that a client doesn't overwrite a record that has since been updated. If the version changed in-between, DatoCMS would reject the update with a 422 STALE_ITEM_VERSION error.

For more information, take a look at the Javascript examples.

Working with block records

Block records live inside of Modular Content, Structured Text and Single Block fields, which require different data structures as their value: while Modular Content is simply an array of blocks, Single Block accept only one block. Structured Text accepts a Structured text document, which in turn might contain a number of block nodes interspersed with textual content.

We’ll get to some detailed examples for both fields below, but the general idea that applies to both of them is that to preserve the atomicity property of ACID transactions, DatoCMS prohibits you from creating/editing/deleting blocks directly; you always need to add/edit/remove blocks by performing a create/update operation on the record that embeds them, so:

  • If you want to add a block to a particular field, you need to add to the existing field value the new “fragment” related to the new block;
  • If you want to edit a block that’s already present in the field, you need to pass the whole field value and replace only the “fragment” related to such block, while keeping the rest unchanged;
  • If you want to remove a block that’s already present in the field, you need to pass the whole field value and only remove the "fragment” related to such block, while keeping the rest unchanged.

Depending on the endpoints you're going to use, the “fragment” representing the block can be simply its ID, or a full JSON API object:

EndpointHTTP request blocks representationHTTP response blocks representation
GET /itemsN/AID
GET /items?nested=trueN/AJSON
resource object
GET /item/
N/AID
GET /item/
?nested=true
N/AJSON
resource object
POST /itemsJSON
resource object
ID
PUT /items/
ID or JSON
resource object
ID

During an update operation, to reference a block:

  • when you want to make some changes to the content of a specific block, you pass the complete JSON
    resource object (complete with its existing id);
  • when you want to add a totally new block, you pass the complete JSON
    resource object (with no ID of course, as it’s a new object);
  • when you don’t want to make any change to an existing block, you can pass just the block ID (the update operation will be faster this way, as it won’t have to process it).

Working with localized content

Before diving in, the key concepts to remember are the following:

  • Localization is handled on a per-field basis. That is, in the same model, some fields can have a different value specified for each locale, and some others just a single (non-localized) value.
  • On localized fields, records express a value for multiple locales using an object, whose keys represent the environment’s locales.
  • The locales specified in a record must be coherent across each localized field. That is, if you have two localized fields, they must share the same locale keys. You cannot ie. specify locales en and it for the title field, and only en for the content field.
  • When a model contains one or more localized fields, it can enforce the presence of a value for all locales, or not.
  • Every user/API token is linked to a role, which in turn specifies which locales can be managed by the user/API token itself on a per-model level.

Above we said that when you perform an update operation to a record, you can pass just the fields you want to change, and not all of them. When dealing with localized content, the only exception to this rule is when you have a model that does not enforce a value for every locale, and you want to add or remove a locale on a specific record. Only in this case, you are required to pass all localized fields in the payload to enforce a locales coherence between all the localized fields.

Also, when you want to update the value for a specific localized field, you always need to pass an object containing the value for each locale you want to preserve, not just the one you might want to update, otherwise you’ll get an error.

If your API token can only manage a subset of locales, during an update operation you are required not to include in the payload any other locales. In this case, the content for the locales you cannot control and that you won’t pass will be kept intact by the system.

In the following table, you’ll find a recap showing the result of an update operation, depending on the API token permissions, the locales already present in a record, and the locales specified in the payload of the update operation itself:

Role can manage content inRecord contains localized fields with content inPayload contains all localized fields with content inResult for the update operation
EnglishEnglishEnglishEnglish is updated.
English, ItalianEnglishEnglish, ItalianEnglish is updated.
Italian is added.
English, ItalianEnglish, ItalianEnglishEnglish is updated.
Italian is removed.
English, ItalianEnglish, ItalianEnglish, ItalianEnglish is updated.
Italian is updated.
English, Italian, FrenchEnglish, ItalianEnglish, FrenchEnglish is updated.
Italian is removed.
French is added.
EnglishEnglish, ItalianEnglishEnglish is updated.
Italian is preserved.
English, ItalianEnglish, FrenchEnglish, ItalianEnglish is updated.
French is preserved.
Italian is added.
English, ItalianEnglish, FrenchItalianEnglish is removed.
French is preserved.
Italian is added.

Body parameters

type string Required

Must be exactly "item".

meta.created_at string Optional

Date of creation

meta.first_published_at null, string Optional

Date of first publication

meta.current_version string Optional

The ID of the current record version (for optimistic locking, see the example)

Example: "4234"
meta.stage string, null Optional

The new stage to move the record to

relationships.item_type.data Optional

The record's model

relationships.creator.data Optional

The entity (account/collaborator/access token/sso user) who created the record

Returns

Returns a resource object of type item.

Examples

1
PUT https://site-api.datocms.com/items/:item_id HTTP/1.1
2
Authorization: Bearer YOUR-API-TOKEN
3
Accept: application/json
4
X-Api-Version: 3
5
Content-Type: application/vnd.api+json
6
7
{
8
"data": {
9
"type": "item",
10
"id": "hWl-mnkWRYmMCSTq4z_piQ"
11
}
12
}
Terminal window
1
curl -g 'https://site-api.datocms.com/items/:item_id' \
2
-X PUT \
3
-H "Authorization: Bearer YOUR-API-TOKEN" \
4
-H "Accept: application/json" \
5
-H "X-Api-Version: 3" \
6
-H "Content-Type: application/vnd.api+json" \
7
--data-binary '{"data":{"type":"item","id":"hWl-mnkWRYmMCSTq4z_piQ"}}'
1
await fetch("https://site-api.datocms.com/items/:item_id", {
2
method: "PUT",
3
headers: {
4
Authorization: "Bearer YOUR-API-TOKEN",
5
Accept: "application/json",
6
"X-Api-Version": "3",
7
"Content-Type": "application/vnd.api+json",
8
},
9
body: JSON.stringify({
10
data: { type: "item", id: "hWl-mnkWRYmMCSTq4z_piQ" },
11
}),
12
});
1
HTTP/1.1 200 OK
2
Content-Type: application/json
3
Cache-Control: cache-control: max-age=0, private, must-revalidate
4
X-RateLimit-Limit: 30
5
X-RateLimit-Remaining: 28
6
7
{
8
"data": {
9
"type": "item",
10
"id": "hWl-mnkWRYmMCSTq4z_piQ",
11
"relationships": {
12
"item_type": {
13
"data": {
14
"type": "item_type",
15
"id": "DxMaW10UQiCmZcuuA-IkkA"
16
}
17
}
18
},
19
"attributes": {
20
"title": "My first blog post!",
21
"content": "Lorem ipsum dolor sit amet...",
22
"category": "24",
23
"image": {
24
"alt": "Alt text",
25
"title": "Image title",
26
"custom_data": {},
27
"focal_point": null,
28
"upload_id": "20042921"
29
}
30
},
31
"meta": {
32
"created_at": "2020-04-21T07:57:11.124Z",
33
"updated_at": "2020-04-21T07:57:11.124Z",
34
"published_at": "2020-04-21T07:57:11.124Z",
35
"first_published_at": "2020-04-21T07:57:11.124Z",
36
"publication_scheduled_at": "2020-04-21T07:57:11.124Z",
37
"unpublishing_scheduled_at": "2020-04-21T07:57:11.124Z",
38
"status": "published",
39
"is_current_version_valid": true,
40
"is_published_version_valid": true,
41
"current_version": "4234",
42
"stage": null
43
}
44
}
45
}