What is Xata?
Creating records

Creating records

Edit on GitHub

You can create a record like this:

const record = await xata.db.Users.create({
  email: 'keanu@example.com',
  name: 'Keanu Reeves'
});

The SDK returns the created record and the response looks like the following:

{
  "email": "keanu@example.com",
  "id": "rec_cd8rqcoavc42pi67lgd0",
  "name": "Keanu Reeves",
  "bio": NULL,
  "address": NULL,
}

In the above the API returns a special set of columns:

If you want to return more columns, you can specify them via the columns parameter.

#

Creating a record with a given ID

If you want to specify your own ID, you can do it like this:

const record = await xata.db.Users.create('myid', {
  email: 'keanu@example.com',
  name: 'Keanu Reeves'
});

In the REST API example, note the change from POST to PUT. The createOnly query parameter signals Xata to return an error in case a record with the given ID already exists.

In the schema that we chose, the Posts table has an author column of type link that links to the Users table. To insert a record with a linked field, use the ID of the target record in the link column.

const record = await xata.db.Posts.create({
  title: 'Filming the Matrix',
  author: 'rec_cd8rqcoavc42pi67lgd0'
});

If you have multiple records to insert, you can send them in a single request via the /bulk endpoint. For example:

const users = await xata.db.Users.create([
  {
    email: 'laurence@example.com',
    name: 'Laurence Fishburne',
    team: 'rec_cd8s4kbo8dsvsjilo1ug'
  },
  {
    email: 'hugo@example.com',
    name: 'Hugo Weaving',
    team: 'rec_cd8s4kbo8dsvsjilo1ug'
  },
  {
    email: 'joe@example.com',
    name: 'Joe Pantoliano',
    team: 'rec_cd8s4kbo8dsvsjilo1ug'
  }
]);

The bulk create method fails if the record ID already exists.

It is possible to provide IDs for each of the records. If records with the same IDs already exist, the bulk operation is canceled and an error is returned.

The file and file[] (file array) columns are not permitted in bulk requests.

Great! We can insert data into our databases. Let's now explore how we get data from a database. Alternatively, we can also look into updating data or deleting data. We've got guides for each of these operations.

On this page

Creating a record with a given IDCreating a record with a linked fieldCreating records in bulkNext Steps