A Sync ListItem is an object you have added to a Sync List.
You need to create a List before you can use this resource to create, read, update, and delete items within it.
Sync Lists generally behave like arrays in most programming languages. However, when working with ListItems, keep these details in mind:
The automatically generated index of the List Item. The index
values of the List Items in a Sync List can have gaps in their sequence.
0
The SID of the Account that created the List Item resource.
^AC[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
The SID of the Sync Service the resource is associated with.
^IS[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
The SID of the Sync List that contains the List Item.
^ES[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
An arbitrary, schema-less object that the List Item stores. Can be up to 16 KiB in length.
The date and time in GMT when the List Item expires and will be deleted, specified in ISO 8601 format. If the List Item does not expire, this value is null
. The List Item resource might not be deleted immediately after it expires.
The date and time in GMT when the resource was created specified in ISO 8601 format.
The date and time in GMT when the resource was last updated specified in ISO 8601 format.
The identity of the List Item's creator. If the item is created from the client SDK, the value matches the Access Token's identity
field. If the item was created from the REST API, the value is system
.
POST https://sync.twilio.com/v1/Services/{ServiceSid}/Lists/{ListSid}/Items
The SID of the Sync List to add the new List Item to. Can be the Sync List resource's sid
or its unique_name
.
application/x-www-form-urlencoded
A JSON string that represents an arbitrary, schema-less object that the List Item stores. Can be up to 16 KiB in length.
An alias for item_ttl
. If both parameters are provided, this value is ignored.
How long, in seconds, before the List Item expires (time-to-live) and is deleted.
How long, in seconds, before the List Item's parent Sync List expires (time-to-live) and is deleted.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createSyncListItem() {11const syncListItem = await client.sync.v112.services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.syncLists("ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")14.syncListItems.create({15data: {16text: "hello world",17user: "Charlies Xavier",18},19});2021console.log(syncListItem.index);22}2324createSyncListItem();
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"created_by": "created_by",4"data": {},5"date_expires": "2015-07-30T21:00:00Z",6"date_created": "2015-07-30T20:00:00Z",7"date_updated": "2015-07-30T20:00:00Z",8"index": 100,9"list_sid": "ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",10"revision": "revision",11"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Lists/ESaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items/100"13}
1syncClient.list('example_list').then(function(list) {2list.push({3text: 'hello world',4user: 'Charles Xavier'5}).then(function(item) {6console.log('Added: ', item.index);7}).catch(function(err) {8console.error(err);9});10});
Note: There are two separate events for list item adds and list item updates:
1syncClient.list('example_list').then(function(list) {2list.on('itemAdded', function(item) {3console.log('index', item.index);4console.log('JSON data', item.value);5});67//Note that there are two separate events for list item adds and list item updates:8list.on('itemUpdated', function(item) {9console.log('index', item.index);10console.log('JSON data', item.value);11});12});
GET https://sync.twilio.com/v1/Services/{ServiceSid}/Lists/{ListSid}/Items/{Index}
The SID of the Sync Service with the Sync List Item resource to fetch.
The SID of the Sync List with the Sync List Item resource to fetch. Can be the Sync List resource's sid
or its unique_name
.
The index of the Sync List Item resource to fetch.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function fetchSyncListItem() {11const syncListItem = await client.sync.v112.services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.syncLists("ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")14.syncListItems(42)15.fetch();1617console.log(syncListItem.index);18}1920fetchSyncListItem();
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"created_by": "created_by",4"data": {},5"date_expires": "2015-07-30T21:00:00Z",6"date_created": "2015-07-30T20:00:00Z",7"date_updated": "2015-07-30T20:00:00Z",8"index": 42,9"list_sid": "ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",10"revision": "revision",11"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Lists/ESaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items/100"13}
Fetch only the first item
1syncClient.list('example_list').then(function(list) {2list.get(0).then(function(item) {3console.log('show first item', item);4});5});
GET https://sync.twilio.com/v1/Services/{ServiceSid}/Lists/{ListSid}/Items
Retrieve all items belonging to a List.
By default, this will return the first 50 List items. Supply a PageSize argument to fetch up to 100 items at once.
See paging for more information.
The SID of the Sync List with the List Items to read. Can be the Sync List resource's sid
or its unique_name
.
How to order the List Items returned by their index
value. Can be: asc
(ascending) or desc
(descending) and the default is ascending.
asc
desc
Whether to include the List Item referenced by the from
parameter. Can be: inclusive
to include the List Item referenced by the from
parameter or exclusive
to start with the next List Item. The default value is inclusive
.
inclusive
exclusive
How many resources to return in each list page. The default is 50, and the maximum is 1000.
1
Maximum: 1000
The page token. This is provided by the API.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function listSyncListItem() {11const syncListItems = await client.sync.v112.services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.syncLists("ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")14.syncListItems.list({ limit: 20 });1516syncListItems.forEach((s) => console.log(s.index));17}1819listSyncListItem();
1{2"items": [],3"meta": {4"first_page_url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Lists/ESaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=0",5"key": "items",6"next_page_url": null,7"page": 0,8"page_size": 50,9"previous_page_url": null,10"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Lists/ESaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=0"11}12}
Display the first item only
1syncClient.list('example_list').then(function(list) {2list.getItems().then(function(page) {3console.log('show first item', page.items[0]);4});5});
POST https://sync.twilio.com/v1/Services/{ServiceSid}/Lists/{ListSid}/Items/{Index}
If provided, applies this mutation if (and only if) the “revision” field of this [map item] matches the provided value. This matches the semantics of (and is implemented with) the HTTP If-Match header.
The SID of the Sync Service with the Sync List Item resource to update.
The SID of the Sync List with the Sync List Item resource to update. Can be the Sync List resource's sid
or its unique_name
.
The index of the Sync List Item resource to update.
application/x-www-form-urlencoded
A JSON string that represents an arbitrary, schema-less object that the List Item stores. Can be up to 16 KiB in length.
An alias for item_ttl
. If both parameters are provided, this value is ignored.
How long, in seconds, before the List Item expires (time-to-live) and is deleted.
How long, in seconds, before the List Item's parent Sync List expires (time-to-live) and is deleted. This parameter can only be used when the List Item's data
or ttl
is updated in the same request.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function updateSyncListItem() {11const syncListItem = await client.sync.v112.services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.syncLists("ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")14.syncListItems(42)15.update({16data: {17user: "Wolverine",18},19});2021console.log(syncListItem.index);22}2324updateSyncListItem();
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"created_by": "created_by",4"data": {},5"date_expires": "2015-07-30T21:00:00Z",6"date_created": "2015-07-30T20:00:00Z",7"date_updated": "2015-07-30T20:00:00Z",8"index": 42,9"list_sid": "ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",10"revision": "revision",11"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Lists/ESaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items/100"13}
Use the set method to change the data in a ListItem
1syncClient.list('example_list').then(function (list) {2var newValue = {3text: 'hello world',4user: 'Wolverine'5};6list.set(0, newValue).then(function(item) {7console.log('updated first item', item);8});9});
Please note: Using set
will overwrite any existing data in a list item.
Use the update method to change data in a ListItem.
1syncClient.list('example-list').then(function(list) {2list.update(0,{user: "Magneto"});3});
Use mutate for more fine-grained control
1syncClient.list('example-list').then(function (list) {2list.mutate(0,function(remoteData) {3remoteData.user = "Cyclops";4return remoteData;5});6});
The mutate
function helps your JavaScript code respond to concurrent updates with versioned control. See the corresponding JavaScript SDK documentation for details.
Note: There are two separate events for list item adds and list item updates:
1syncClient.list('example_list').then(function(list) {2list.on('itemAdded', function(item) {3console.log('index', item.index);4console.log('JSON data', item.value);5});67//Note that there are two separate events for list item adds and list item updates:8list.on('itemUpdated', function(item) {9console.log('index', item.index);10console.log('JSON data', item.value);11});12});
DELETE https://sync.twilio.com/v1/Services/{ServiceSid}/Lists/{ListSid}/Items/{Index}
If provided, applies this mutation if (and only if) the “revision” field of this [map item] matches the provided value. This matches the semantics of (and is implemented with) the HTTP If-Match header.
The SID of the Sync Service with the Sync List Item resource to delete.
The SID of the Sync List with the Sync List Item resource to delete. Can be the Sync List resource's sid
or its unique_name
.
The index of the Sync List Item resource to delete.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function deleteSyncListItem() {11await client.sync.v112.services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.syncLists("ESXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")14.syncListItems(42)15.remove();16}1718deleteSyncListItem();
Deletes the 0-index item in a Sync List
1syncClient.list('example_list').then(function(list) {2list.remove(0).then(function() {3console.log('deleted first item');4});5});