A Sync MapItem is an individual item that belongs to one or more of your Sync Maps. See the full API reference documentation for the Sync Map resource here.
You need to create a Map first before you can use this resource to create, read, update, and delete items.
Sync MapItems:
The SID of the Account that created the Map 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 Map that contains the Map Item.
^MP[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
An arbitrary, schema-less object that the Map Item stores. Can be up to 16 KiB in length.
The date and time in GMT when the Map Item expires and will be deleted, specified in ISO 8601 format. If the Map Item does not expire, this value is null
. The Map Item 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 Map Item's creator. If the Map Item is created from the client SDK, the value matches the Access Token's identity
field. If the Map Item was created from the REST API, the value is system
.
POST https://sync.twilio.com/v1/Services/{ServiceSid}/Maps/{MapSid}/Items
The SID of the Sync Map to add the new Map Item to. Can be the Sync Map resource's sid
or its unique_name
.
application/x-www-form-urlencoded
The unique, user-defined key for the Map Item. Can be up to 320 characters long.
A JSON string that represents an arbitrary, schema-less object that the Map 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 Map Item expires (time-to-live) and is deleted.
How long, in seconds, before the Map Item's parent Sync Map 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 createSyncMapItem() {11const syncMapItem = await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems.create({15data: {16name: "Foo Bar",17level: 30,18username: "foo_bar",19},20key: "foo",21});2223console.log(syncMapItem.key);24}2526createSyncMapItem();
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"key": "foo",9"map_sid": "MapSid",10"revision": "revision",11"service_sid": "ServiceSid",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items/key"13}
Use the set method
1syncClient.map('users').then(function(map) {2map.set('Taylor', {3phone_number: 12345678,4country: 'UK'5}).then(function(item) {6console.log('Added: ', item.key);7}).catch(function(err) {8console.error(err);9});10});
Please note: You can also use the set
method to update data in existing JSON data in a Map. However, using set
will overwrite any existing data in a MapItem.
Note that there are two separate events for map item adds and map item updates:
1syncClient.map('users').then(function (map) {2map.on('itemAdded', function(item) {3console.log('key', item.key);4console.log('JSON data', item.value);5});67//Note that there are two separate events for map item adds and map item updates:8map.on('itemUpdated', function(item) {9console.log('key', item.key);10console.log('JSON data', item.value);11});12});
GET https://sync.twilio.com/v1/Services/{ServiceSid}/Maps/{MapSid}/Items/{Key}
The SID of the Sync Service with the Sync Map Item resource to fetch.
The SID of the Sync Map with the Sync Map Item resource to fetch. Can be the Sync Map resource's sid
or its unique_name
.
The key
value of the Sync Map 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 fetchSyncMapItem() {11const syncMapItem = await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems("foo")15.fetch();1617console.log(syncMapItem.key);18}1920fetchSyncMapItem();
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"key": "foo",9"map_sid": "MapSid",10"revision": "revision",11"service_sid": "ServiceSid",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items/key"13}
Fetches by a specific key
1syncClient.map('users').then(function(map) {2map.get('Taylor').then(function(item) {3console.log(item.value);4});5});
GET https://sync.twilio.com/v1/Services/{ServiceSid}/Maps/{MapSid}/Items
MapItem read access is performed using the key
that provided as an arbitrary string to identify the item.
By default, this will return the first 50 MapItems. Supply a PageSize parameter to fetch up to 100 items at once. See paging for more information.
The SID of the Sync Map with the Sync Map Item resource to fetch. Can be the Sync Map resource's sid
or its unique_name
.
How to order the Map Items returned by their key
value. Can be: asc
(ascending) or desc
(descending) and the default is ascending. Map Items are ordered lexicographically by Item key.
asc
desc
Whether to include the Map Item referenced by the from
parameter. Can be: inclusive
to include the Map Item referenced by the from
parameter or exclusive
to start with the next Map 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 listSyncMapItem() {11const syncMapItems = await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems.list({ limit: 20 });1516syncMapItems.forEach((s) => console.log(s.key));17}1819listSyncMapItem();
1{2"items": [],3"meta": {4"first_page_url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/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/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=0"11}12}
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 listSyncMapItem() {11const syncMapItems = await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems.list({15from: "foo",16order: "asc",17limit: 20,18});1920syncMapItems.forEach((s) => console.log(s.key));21}2223listSyncMapItem();
1{2"items": [],3"meta": {4"first_page_url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/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/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=0"11}12}
This code sample displays the first item.
1syncClient.map('users').then(function(map) {2map.getItems().then(function(page) {3console.log('show first item', page.items[0].key,4page.items[0].value);5});6});
POST https://sync.twilio.com/v1/Services/{ServiceSid}/Maps/{MapSid}/Items/{Key}
MapItem update access is performed using the key
that provided as an arbitrary string to identify the item.
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 Map Item resource to update.
The SID of the Sync Map with the Sync Map Item resource to update. Can be the Sync Map resource's sid
or its unique_name
.
The key
value of the Sync Map Item resource to update.
application/x-www-form-urlencoded
A JSON string that represents an arbitrary, schema-less object that the Map 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 Map Item expires (time-to-live) and is deleted.
How long, in seconds, before the Map Item's parent Sync Map expires (time-to-live) and is deleted. This parameter can only be used when the Map 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 updateSyncMapItem() {11const syncMapItem = await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems("foo")15.update({16data: {17name: "FooBaz",18level: 31,19username: "foo_baz",20},21});2223console.log(syncMapItem.key);24}2526updateSyncMapItem();
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"key": "foo",9"map_sid": "MapSid",10"revision": "revision",11"service_sid": "ServiceSid",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items/key"13}
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 updateSyncMapItem() {11const syncMapItem = await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems("foo")15.update({16data: {17name: "FooBaz",18level: 31,19username: "foo_baz",20},21ifMatch: "1a",22});2324console.log(syncMapItem.revision);25}2627updateSyncMapItem();
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"key": "foo",9"map_sid": "MapSid",10"revision": "revision",11"service_sid": "ServiceSid",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Maps/MPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Items/key"13}
Use the update method to change the data in a Map Item
1syncClient.map('users').then(function(map) {2map.update('Taylor',{country: "IRL"});3});
Use mutate for more fine-grained control over updates.
1syncClient.map('users').then(function (map) {2map.mutate('david',function(remoteData) {3remoteData.country = "USA";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 that there are two separate events for map item adds and map item updates:
1syncClient.map('users').then(function (map) {2map.on('itemAdded', function(item) {3console.log('key', item.key);4console.log('JSON data', item.value);5});67//Note that there are two separate events for map item adds and map item updates:8map.on('itemUpdated', function(item) {9console.log('key', item.key);10console.log('JSON data', item.value);11});12});
DELETE https://sync.twilio.com/v1/Services/{ServiceSid}/Maps/{MapSid}/Items/{Key}
Permanently delete a specific item from an existing Map.
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 Map Item resource to delete.
The SID of the Sync Map with the Sync Map Item resource to delete. Can be the Sync Map resource's sid
or its unique_name
.
The key
value of the Sync Map 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 deleteSyncMapItem() {11await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems("foo")15.remove();16}1718deleteSyncMapItem();
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 deleteSyncMapItem() {11await client.sync.v112.services("ServiceSid")13.syncMaps("MapSid")14.syncMapItems("foo")15.remove({ ifMatch: "1a" });16}1718deleteSyncMapItem();
Deletes the item with key 'Taylor'
1syncClient.map('users').then(function(map) {2map.remove('Taylor').then(function() {3console.log('item deleted');4});5});