Sorry, no results found for "".

Show examples in:
Javascript HTTP

Content Management API > Record

Create a new record
📚 New to DatoCMS records?

Before creating your first record, we strongly recommend reading the Introduction to Records guide. It covers fundamental concepts about field types, block manipulation, and localization that are essential for building a valid creation payload.

The payload required to create a new record is determined by the specific model it's based on and the fields it contains.

Suppose our project contains a "Dog" model (ID: 1234, API key: dog) with the following fields:

Field API keyField type
nameSingle-line string (string)
breedSingle-line string (string)
descriptionMultiple-paragraph text (text)
ageInteger (integer)

We can create a new dog record like this:

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Buddy",
breed: "Labrador",
description: "Very friendly and calm.\nI love it.",
age: 4,
});
console.log(record);
}
run();
const response = {
type: "item",
id: "4572128",
name: "Buddy",
breed: "Labrador",
description: "Very friendly and calm.\nI love it.",
age: 4,
meta: {
created_at: "2020-04-17T16:34:31.981+01:00",
updated_at: "2020-04-17T16:34:32.005+01:00",
published_at: "2020-04-17T16:34:32.004+01:00",
first_published_at: "2020-04-17T16:34:32.004+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
current_version: "8045084",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

When creating a record, you don't need to specify a value for every field. Any field you omit will be set to its configured default value, or null if no default is set.

While the Introduction to Records guide offers a complete reference for every field type, there are several key rules that are especially important when creating a new record.

Field value formatting

Every field in your payload must be formatted according to its type. This can range from a simple string or number to a structured object. For a comprehensive breakdown of the expected format for every field type, please refer to the Field Types Overview in our main records guide.

Suppose our project contains a "Dog" model (ID: 1234, API key: dog) with the following fields:

Field API keyField type
nameSingle-line string (string)
breedSingle-line string (string)
descriptionMultiple-paragraph text (text)
ageInteger (integer)
heightFloat (float)
date_of_birthDate-time (date_time)
availableBoolean (boolean)
locationGeo-location (lat_lon)
colorColor (color)
other_stuffJSON (json)

A couple of gotchas when writing our call to client.items.create():

  • Geo-location and Color fields require an object with all their properties specified;
  • The JSON field must be a JSON-serialized string, and not an object.

You can read all the details about the value that each specific type of field requires in the Field type values section.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Buddy",
breed: "Labrador",
description: "Very friendly and calm.\nI love it.",
age: 4,
height: 50.5,
date_of_birth: "2020-04-17T17:25:00",
available: true,
location: {
latitude: 45.0703393,
longitude: 7.686864,
},
color: {
alpha: 255,
blue: 156,
green: 208,
red: 239,
},
other_stuff: JSON.stringify({ foo: "bar", additionalData: "1234" }),
});
console.log(record);
}
run();
const response = {
type: "item",
id: "4572180",
name: "Buddy",
breed: "Labrador",
description: "Very friendly and calm.\nI love it.",
age: 4,
height: 50.5,
date_of_birth: "2020-04-17T17:25:00+01:00",
available: true,
location: { latitude: 45.0703393, longitude: 7.686864 },
color: { red: 239, green: 208, blue: 156, alpha: 255 },
other_stuff: '{\n "foo": "bar",\n "additionalData": "1234"\n}',
meta: {
created_at: "2020-04-17T16:58:27.488+01:00",
updated_at: "2020-04-17T16:58:27.498+01:00",
published_at: "2020-04-17T16:58:27.498+01:00",
first_published_at: "2020-04-17T16:58:27.498+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "8045322",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

Block Fields

When creating a record, any new blocks (for Modular Content, Single Block, or Structured Text fields) must be provided as full block objects. This object must include the item_type in its relationships to specify which Block Model to use. You cannot create a record by providing only block IDs. For a deeper dive into manipulating blocks, see the guide on Creating and Updating Blocks.

To make our dog's description more dynamic we are going to add a Modular content field. This will allow us to compose the description by combining multiple blocks of different kind. Each block model will have its set of fields and can be repeated as many times as needed.

This is our "Dog" model (ID: 1234, API key: dog):

Field API keyField type
nameSingle-line string (string)
descriptionModular content (rich_text)

The modular content field accepts three different types of blocks:

  • A "Prize" block model (ID: 1235, API key: prize_block):

    Field API keyField type
    nameSingle-line string (string)
    yearInteger (integer)
    pictureSingle asset (upload)
  • A "Description" block model (ID: 1236, API key: description_block):

    Field API keyField type
    descriptionMulti-paragraph text (text)
  • A "Carousel" block model (ID: 1237, API key: carousel_block):

    Field API keyField type
    carouselAsset gallery (gallery)

In this example we'll use the buildBlockRecord() function to help us creating blocks more easily. You just need to specify the block model ID and all the fields, like a normal record:

const { buildClient, buildBlockRecord } = require("datocms-client");
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
// upload file using a local file:
const path = await client.createUploadPath("./2018-10-17-194326.jpg");
// you can then use the returned path to create a new upload:
const upload = await client.uploads.create({ path });
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Buddy",
description: [
// prize block
buildBlockRecord({
item_type: { type: "item_type", id: "1235" },
name: "Best dog in the world",
year: 2020,
picture: { upload_id: upload.id },
}),
// description block
buildBlockRecord({
item_type: { type: "item_type", id: "1236" },
description: "Very friendly and calm.\nI love it.",
}),
// carousel block
buildBlockRecord({
item_type: { type: "item_type", id: "1237" },
carousel: [{ upload_id: upload.id }],
}),
],
});
console.log(record);
}
run();
const resoinse = {
type: "item",
id: "4579897",
name: "Buddy",
description: ["4579894", "4579895", "4579896"],
meta: {
created_at: "2020-04-20T13:38:35.972+01:00",
updated_at: "2020-04-20T13:38:36.005+01:00",
published_at: "2020-04-20T13:38:36.004+01:00",
first_published_at: "2020-04-20T13:38:36.004+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "8065211",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

To make our dog's description more dynamic we are going to add a Structured text field. This will allow us to compose the description by combining text and blocks of different kind.

This is our "Dog" model (ID: 1234, API key: dog):

Field API keyField type
nameSingle-line string (string)
descriptionStructured text (structured_text)

The structured text field accepts three different types of blocks:

  • A "Prize" block model (ID: 1235, API key: prize_block):

    Field API keyField type
    nameSingle-line string (string)
    yearInteger (integer)
    pictureSingle asset (upload)
  • A "Description" block model (ID: 1236, API key: description_block):

    Field API keyField type
    descriptionMulti-paragraph text (text)
  • A "Carousel" block model (ID: 1237, API key: carousel_block):

    Field API keyField type
    carouselAsset gallery (gallery)

In this example we'll use the buildBlockRecord() function to help us creating blocks more easily. You need to specify the block model ID and all the fields, just like a normal record. We'll also use the client.uploads.createFromUrl() method to create new upload resources — you can see all the alternative upload creation methods in the Create a new upload endpoint documentation.

const { buildBlockRecord, buildBlockRecord } = require("datocms-client");
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
// create upload resource from URL (or return an existing upload if it's already present in the media area):
const upload = await client.uploads.createFromUrl({
url: "https://example.com/image.png",
skipCreationIfAlreadyExists: true,
});
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Buddy",
description: {
schema: "dast",
document: {
type: "root",
children: [
// simple header
{
type: "heading",
level: 1,
children: [
{
type: "span",
marks: [],
value: "Hello world!",
},
],
},
// prize block
{
type: "block",
item: buildBlockRecord({
item_type: { type: "item_type", id: "1235" },
name: "Best dog in the world",
year: 2020,
picture: { upload_id: upload.id },
}),
},
// description block
{
type: "block",
item: buildBlockRecord({
item_type: { type: "item_type", id: "1236" },
description: "Very friendly and calm.\nI love it.",
}),
},
// gallery block
{
type: "block",
item: buildBlockRecord({
item_type: { type: "item_type", id: "1237" },
carousel: [{ upload_id: upload.id }],
}),
},
],
},
},
});
console.log(record);
}
run();
const response = {
type: "item",
id: "4579897",
name: "Buddy",
description: {
schema: "dast",
document: {
type: "root",
children: [
{
type: "heading",
level: 1,
children: [{ type: "span", marks: [], value: "Hello world!" }],
},
{ type: "block", item: "4579894" },
{ type: "block", item: "4579895" },
{ type: "block", item: "4579896" },
],
},
},
meta: {
created_at: "2020-04-20T13:38:35.972+01:00",
updated_at: "2020-04-20T13:38:36.005+01:00",
published_at: "2020-04-20T13:38:36.004+01:00",
first_published_at: "2020-04-20T13:38:36.004+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "8065211",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

Asset & Link Fields

These reference fields require specific formats. For a comprehensive breakdown of the expected format for every field type, please refer to the Field Types Overview in our main records guide.

As discussed in the Field type values section, a Single asset (upload) type of field requires an object in the following form:

{
upload_id: "1737955",
alt: "Dog picture",
title: "Buddy",
focal_point: {
x: 0.3,
y: 0.2,
},
custom_data: {
add_watermark: true,
},
}

While the Asset gallery field (gallery) requires an array of object with the same format.

All the keys except upload_id are optional and can be omitted.

Suppose our project contains a "Dog" model (ID: 1234, API key: dog) with the following fields:

Field API keyField type
nameSingle-line string (string)
pictureSingle asset (upload)
carouselAsset gallery (gallery)

To fill in the upload_id property of both fields, we could either reference an already-existing asset, or create a brand new Upload resource. In the following example we're using the client.uploads.createFromUrl() method to create a new upload resource — you can see all the alternative upload creation methods in the Create a new upload endpoint documentation.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
// create upload resource using URL (or return an existing upload if it's already present in the media area):
const upload = await client.uploads.createFromUrl({
url: "https://example.com/image.png",
skipCreationIfAlreadyExists: true,
});
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Buddy",
picture: {
// in this case we're just passing the upload ID, as the
// upload resource's defaults for alt, title, etc. are fine:
upload_id: upload.id,
},
carousel: [
// here we want to override the upload resource's defaults:
{
upload_id: upload.id,
alt: "Dog picture",
title: "Buddy",
focal_point: {
x: 0.3,
y: 0.2,
},
custom_data: {
add_watermark: true,
},
},
],
});
console.log(record);
}
run();
const response = {
type: "item",
id: "4725799",
name: "Buddy",
picture: {
upload_id: "1737955",
alt: null,
title: null,
focal_point: null,
custom_data: {},
},
carousel: [
{
upload_id: "1737955",
alt: "Dog picture",
title: "Buddy",
focal_point: {
x: 0.3,
y: 0.2,
},
custom_data: {},
},
],
meta: {
created_at: "2020-05-18T14:46:26.470+01:00",
updated_at: "2020-05-18T14:46:26.484+01:00",
published_at: "2020-05-18T14:46:26.483+01:00",
first_published_at: "2020-05-18T14:46:26.483+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "8526589",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

Suppose our project contains a "Dog" model (ID: 1234, API key: dog) with the following fields:

Field API keyField type
nameSingle-line string (string)
friendsMultiple links field (links), referencing other dog records
best_friendSingle link field (link), referencing other dog records

In the example we first retrieve some records and then we create a new record which references them. To keep the example short, our links point to other dog records, so that we don't need other models.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
// Let's retrieve some other dogs first
const friends = await client.items.list({ filter: { type: "dog" } });
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Buddy",
friends: friends.map((f) => f.id),
best_friend: friends[0].id,
});
console.log(record);
}
run();
const response = {
type: "item",
id: "4579273",
name: "Buddy",
friends: ["4572300", "4572298", "4572297", "4572180", "4572128"],
best_friend: "4572300",
meta: {
created_at: "2020-04-20T11:06:29.126+01:00",
updated_at: "2020-04-20T11:06:29.150+01:00",
published_at: "2020-04-20T11:06:29.150+01:00",
first_published_at: "2020-04-20T11:06:29.150+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "8062918",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

Localization

If the record's model contains localized fields, your creation payload must adhere to specific rules:

  • You must provide a value for every locale that is marked as required by the model's settings.
  • All localized fields in a single payload must specify the same set of locales to ensure consistency.

Unlike record updates where you can send a subset of locales, the creation endpoint is stricter about enforcing required locales. For a full explanation of how to structure localized data, refer to the Localization Guide.

If your model does not require all environment's locales to be present (that is the "All locales required?" option is turned off), then you can create a record defining only a subset of the environment's locales.

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: {
it: "Asso",
},
age: {
it: 12,
},
});
console.log(record);
}
run();
const response = {
type: "item",
id: "4668524",
name: {
it: "Asso",
},
age: {
it: 12,
},
meta: {
created_at: "2020-05-06T13:11:27.442+01:00",
updated_at: "2020-05-06T13:11:27.483+01:00",
published_at: "2020-05-06T13:11:27.482+01:00",
first_published_at: "2020-05-06T13:11:27.482+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "8329850",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

Body parameters

id string Optional

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

Example: "hWl-mnkWRYmMCSTq4z_piQ"
meta.created_at string Optional

Date of creation

meta.first_published_at null, string Optional

Date of first publication

item_type Required

The record's model

creator Optional

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

Returns

Returns a resource object of type item

Other examples

Suppose our project primary environment is set up to have two different locales ("en" and "it"), and contains a "Dog" model (ID: 1234, API key: dog) with the following fields:

Field API keyLocalizedField type
nameSingle-line string (string)
ageInteger (integer)

On localized fields, records express a value for multiple locales using an object, whose keys represent the locale itself:

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const record = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: {
en: "Alfie",
it: "Asso",
},
age: {
en: 30,
it: 12,
},
});
console.log(record);
}
run();
const response = {
type: "item",
id: "4668524",
name: {
en: "Alfie",
it: "Asso",
},
age: {
en: 30,
it: 12,
},
meta: {
created_at: "2020-05-06T13:11:27.442+01:00",
updated_at: "2020-05-06T13:11:27.483+01:00",
published_at: "2020-05-06T13:11:27.482+01:00",
first_published_at: "2020-05-06T13:11:27.482+01:00",
publication_scheduled_at: null,
unpublishing_scheduled_at: null,
status: "published",
is_valid: true,
is_current_version_valid: true,
is_published_version_valid: true,
current_version: "8329850",
},
item_type: { type: "item_type", id: "1234" },
creator: { type: "access_token", id: "322" },
};

Suppose our project contains a "Menu Item" model (ID: 1234, API key: menu_item) that is configured to be organized as a tree, and has a single field:

Field API keyLocalizedField type
nameSingle-line string (string)

In this example we'll create a hierarchy of three records.

In tree-like collections, records can specify two additional fields, parent_id and position, so that they can express who is their parent record, and which is their position relative to its siblings:

import { buildClient } from "@datocms/cma-client-node";
async function run() {
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
const parent = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Parent",
});
console.log(parent);
const child1 = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Child 1",
parent_id: parent.id,
position: 1,
});
console.log(child1);
const child2 = await client.items.create({
item_type: { type: "item_type", id: "1234" },
name: "Child 2",
parent_id: parent.id,
position: 2,
});
console.log(child2);
}
run();
const parent = {
type: "item",
id: "4679744",
name: "Parent",
parent_id: null,
position: 1,
// ...
};
const child1 = {
type: "item",
id: "4679745",
name: "Child 2",
parent_id: "4679744",
position: 1,
// ...
};
const child2 = {
type: "item",
id: "4679746",
name: "Child 1",
parent_id: "4679744",
position: 2,
// ...
};