Show examples in:
Javascript HTTP

Content Management API > Record

Create a new record

The payload to pass to create a new record always depends on the specific settings of its model and the fields it declares.

When creating a new record, you don't need to specify a value for every field of it's model, as the ones you don't specify will be set to the field's default value (if available), or null (which is a valid value for every type of field).

Different values for different field types

If you do want to provide an explicit value for a field, then each type of field requires a different type of value. Most of the field types require scalar values — ie. a Single-line string requires a string, an Integer field requires a number, etc:

1
{
2
// If the model has a `title` single-line string field...
3
"name": "Alfie",
4
// If the model has an `age` integer field...
5
"age": 4
6
// And so on :)
7
}

Some other fields, like Geo-location or Color fields, require objects instead of simple data types:

1
{
2
// If the model has a `location` field of type 'Geo-location'...
3
"location": { "latitude": 45.0703393, "longitude": 7.686864 },
4
// If the model has an `accent_color` field of type 'Color'...
5
"accent_color": { "red": 239, "green": 208, "blue": 156, "alpha": 255 }
6
}

We'll analyze in detail every different type of field that DatoCMS offers in the following examples.

Localized fields

Fields can be localized. If that's the case, records need to express a value for multiple locales using an object, whose keys represent the locale itself.

If your model has the "All locales required?" option turned on, then you MUST express a field value for all environment's locales:

1
{
2
// If the model has a `title` single-line string field...
3
"name": {
4
"en": "Alfie",
5
"it": "Asso"
6
},
7
// If the model has an `color` field...
8
"age": {
9
"en": { "red": 255, "green": 0, "blue": 0, "alpha": 255 },
10
"it": { "red": 0, "green": 255, "blue": 0, "alpha": 255 }
11
}
12
// And so on :)
13
}

Otherwise, you can just define a subset of them (as long as all localized fields express the exact same subset!):

1
{
2
// Even if the environment has many locales, since the model has the "All locales required?" option
3
// turned off, we can define only the Italian locale:
4
"name": {
5
"it": "Asso"
6
},
7
"age": {
8
"it": { "red": 0, "green": 255, "blue": 0, "alpha": 255 }
9
}
10
}

Field type values

Single-line string

The field accepts String values.

Multi-line text

The field accepts simple String values.

Boolean

The field accepts simple Boolean values.

Integer

The field accepts simple Integer values.

Float

The field accepts simple Float values.

Date

The field accepts String values in ISO 8601 date format (ie. "2015-12-29").

Date time

The field accepts String values in ISO 8601 date-time format (ie. "2020-04-17T16:34:31.981+01:00").

If you're on legacy timezone management, remember that when sending an ISO8601 datetime you should keep in mind that the system will ignore any provided timezone, and will use the project's timezone instead.

Color

The field accepts an object with the following properties:

PropertyRequiredType
redInteger between 0 and 255
greenInteger between 0 and 255
blueInteger between 0 and 255
alphaInteger between 0 and 255
JSON

The field accepts String values that are valid JSON.

Location

The field accepts an object with the following properties:

PropertyRequiredType
latitudeFloat between -90.0 to 90
longitudeFloat between -180.0 to 180
SEO

The field accepts an object with the following properties:

PropertyRequiredTypeDescription
titleStringTitle meta tag (max. 320 characters)
descriptionStringDescription meta tag (max. 320 characters)
imageUpload IDAsset to be used for social shares
twitter_card"summary", "summary_large_image"Type of Twitter card to use
no_indexBooleanWhether the noindex meta tag should be returned
Slug

The field accepts String values that satisfy the following regular expression: /^[a-z0-9_]+(?:\-[a-z0-9]+)*$/.

External video

The field accepts an object with the following properties:

PropertyRequiredTypeDescriptionExample
provider"youtube", "vimeo", "facebook"External video provider"youtube"
provider_uidStringUnique identifier of the video within the provider"vUdGBEb1i9g"
urlURLURL of the video"https://www.youtube.com/watch?v=qJhobECFQYk"
widthIntegerVideo width459
heightIntegerVideo height344
thumbnail_urlURLURL for the video thumb"https://i.ytimg.com/vi/vUdGBEb1i9g/hqdefault.jpg"
titleStringTitle of the video"Next.js Conf Booth Welcoming!"
Single-asset

The field accepts an object with the following properties:

PropertyRequiredTypeDescriptionExample
upload_idUpload IDID of an asset"3429022"
titleStringTitle for the asset, if you want to override the asset's default value (see Upload default_field_metadata)"From my trip to Italy"
altStringAlternate text for the asset, if you want to override the asset's default value (see Upload default_field_metadata)"Florence skyline"
focal_point{ x: Float, y: Float }, nullFocal point for the asset, if you want to override the asset's default value (see Upload default_field_metadata). Values must be expressed as Float between 0 and 1. Focal point can only be specified for image assets.{ "x": 0.34, "y": 0.45 }
custom_dataRecord<String, String>An object containing custom keys that you can use on your frontend projects{ "watermark_image": "true" }
Asset gallery

This field accepts an Array of objects with the following properties:

PropertyRequiredTypeDescriptionExample
upload_idUpload IDID of an asset"3429022"
titleStringTitle for the asset, if you want to override the asset's default value (see Upload default_field_metadata)"https://www.youtube.com/watch?v=vUdGBEb1i9g"
altStringAlternate text for the asset, if you want to override the asset's default value (see Upload default_field_metadata)"vUdGBEb1i9g"
focal_point{ x: Float, y: Float }, nullFocal point for the asset, if you want to override the asset's default value (see Upload default_field_metadata). Values must be expressed as Float between 0 and 1. Focal point can only be specified for image assets.{ "x": 0.34, "y": 0.45 }
custom_dataRecord<String, String>An object containing custom keys that you can use on your frontend projects{ "watermark_image": "true" }
Single link

This field accepts a String representing the ID of the linked record.

Multiple links

This field accepts an Array<String> representing the IDs of the linked records.

Modular content

When a record is being read, this field returns an Array<String> representing the IDs of the inner block records.

When a record is being created, this field must contain an Array of objects representing the inner block records:

Example:

1
[
2
{
3
"type": "item",
4
"attributes": {
5
// ... put your fields values here
6
},
7
"relationships": {
8
"item_type": {
9
"data": {
10
// the block model
11
"id": "435822",
12
"type": "item_type",
13
}
14
}
15
},
16
},
17
// ...
18
]

When a record is being updated, make sure to include the ID of the already existing block records, or if the block is not subject to any change, just pass its ID:

1
[
2
// we don't need to change anything to these two blocks, just pass their IDs:
3
"9862635",
4
"9862636",
5
// we want to change some field values for this block!
6
{
7
"id": "9862637",
8
"type": "item",
9
"attributes": {
10
// ... put the block fields to be changed here
11
},
12
"relationships": {
13
"item_type": {
14
"data": {
15
// the block model
16
"id": "435822",
17
"type": "item_type",
18
}
19
}
20
},
21
},
22
]
Single block

When a record is being read, this field returns a <String> representing the ID of the inner block record.

When a record is being created, this field must contain an object representing the inner block record:

Example:

1
{
2
"type": "item",
3
"attributes": {
4
// ... put your fields values here
5
},
6
"relationships": {
7
"item_type": {
8
"data": {
9
// the block model
10
"id": "435822",
11
"type": "item_type",
12
}
13
}
14
},
15
},
16
// ...

When a record is being updated, make sure to include the ID of the already existing block record, or if the block is not subject to any change, just pass its ID:

1
// we don't need to change anything to this block, just pass its ID:
2
"9862635"
1
// we want to change some field values for this block!
2
{
3
"id": "9862637",
4
"type": "item",
5
"attributes": {
6
// ... put the block fields to be changed here
7
},
8
"relationships": {
9
"item_type": {
10
"data": {
11
// the block model
12
"id": "435822",
13
"type": "item_type",
14
}
15
}
16
},
17
}
Structured text

This field accepts a Structured Text document.

When a record is being created, the item attribute of block nodes must be the entire block record instead of the ID:

Example:

1
{
2
"schema": "dast",
3
"document": {
4
"type": "root",
5
"children": [
6
{
7
"type": "block",
8
"item": {
9
"type": "item",
10
"attributes": {
11
// ... put your fields values here
12
},
13
"relationships": {
14
"item_type": {
15
"data": {
16
// the block model
17
"id": "435822",
18
"type": "item_type",
19
}
20
}
21
},
22
}
23
}
24
]
25
},
26
}

When a record is being updated, make sure to include the ID of the already existing block records, or if the block is not subject to any change, just pass its ID:

1
{
2
"schema": "dast",
3
"document": {
4
"type": "root",
5
"children": [
6
{
7
"type": "block",
8
// this block is not subject to any change, just pass its ID
9
"item": "34823428"
10
},
11
{
12
"type": "block",
13
// we want to change some field values for this block!
14
"item": {
15
"id": "34823424",
16
"type": "item",
17
"attributes": {
18
// ... put the block fields to be changed here
19
},
20
"relationships": {
21
"item_type": {
22
"data": {
23
// the block model
24
"id": "435822",
25
"type": "item_type",
26
}
27
}
28
}
29
}
30
}
31
]
32
}
33
}

Body parameters

id string Optional

RFC 4122 UUID of record expressed in URL-safe base64 format

Example: "hWl-mnkWRYmMCSTq4z_piQ"
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

relationships.item_type.data Required

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
POST https://site-api.datocms.com/items 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
"attributes": {
11
"title": "My first blog post!",
12
"content": "Lorem ipsum dolor sit amet...",
13
"category": "24",
14
"image": {
15
"upload_id": "1235",
16
"alt": "Alt text",
17
"title": "Image title",
18
"custom_data": {}
19
}
20
},
21
"relationships": {
22
"item_type": {
23
"data": {
24
"type": "item_type",
25
"id": "DxMaW10UQiCmZcuuA-IkkA"
26
}
27
}
28
}
29
}
30
}
Terminal window
1
curl -g 'https://site-api.datocms.com/items' \
2
-X POST \
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","attributes":{"title":"My first blog post!","content":"Lorem ipsum dolor sit amet...","category":"24","image":{"upload_id":"1235","alt":"Alt text","title":"Image title","custom_data":{}}},"relationships":{"item_type":{"data":{"type":"item_type","id":"DxMaW10UQiCmZcuuA-IkkA"}}}}}'
1
await fetch("https://site-api.datocms.com/items", {
2
method: "POST",
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: {
11
type: "item",
12
attributes: {
13
title: "My first blog post!",
14
content: "Lorem ipsum dolor sit amet...",
15
category: "24",
16
image: {
17
upload_id: "1235",
18
alt: "Alt text",
19
title: "Image title",
20
custom_data: {},
21
},
22
},
23
relationships: {
24
item_type: {
25
data: { type: "item_type", id: "DxMaW10UQiCmZcuuA-IkkA" },
26
},
27
},
28
},
29
}),
30
});
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
"included": [
46
{
47
"type": "item_type",
48
"id": "DxMaW10UQiCmZcuuA-IkkA",
49
"relationships": {
50
"singleton_item": {
51
"data": null
52
},
53
"fields": {
54
"data": [
55
{
56
"type": "field",
57
"id": "Pkg-oztERp6o-Rj76nYKJg"
58
}
59
]
60
},
61
"fieldsets": {
62
"data": [
63
{
64
"type": "fieldset",
65
"id": "93Y1C2sySkG4Eg0atBRIwg"
66
}
67
]
68
},
69
"title_field": {
70
"data": null
71
},
72
"image_preview_field": {
73
"data": null
74
},
75
"excerpt_field": {
76
"data": null
77
},
78
"ordering_field": {
79
"data": null
80
},
81
"workflow": {
82
"data": null
83
}
84
},
85
"attributes": {
86
"name": "Blog post",
87
"api_key": "post",
88
"singleton": false,
89
"sortable": true,
90
"modular_block": false,
91
"tree": false,
92
"ordering_direction": null,
93
"ordering_meta": "created_at",
94
"draft_mode_active": false,
95
"all_locales_required": false,
96
"collection_appearance": "compact",
97
"hint": "Blog posts will be shown in our website under the Blog section",
98
"inverse_relationships_enabled": false
99
},
100
"meta": {
101
"has_singleton_item": false
102
}
103
}
104
]
105
}