A Sync Document is an object with these characteristics:
ttl
parameter. By default, it is persisted permanently.A Sync Document is best suited for basic use cases, such as rudimentary publish/subscribe flows, or situations where history synchronization is not a requirement.
Documents can be created, updated, subscribed to, and removed via the client JavaScript SDK. See the latest JavaScript SDK documentation for full details. Servers wishing to manage these objects can do so via the REST API.
The unique string that we created to identify the Document resource.
^ET[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
An application-defined string that uniquely identifies the resource. It can be used in place of the resource's sid
in the URL to address the resource and can be up to 320 characters long.
The SID of the Account that created the Document 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 current revision of the Sync Document, represented as a string. The revision
property is used with conditional updates to ensure data consistency.
An arbitrary, schema-less object that the Sync Document stores. Can be up to 16 KiB in length.
The date and time in GMT when the Sync Document expires and will be deleted, specified in ISO 8601 format. If the Sync Document does not expire, this value is null
. The Document 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 Sync Document's creator. If the Sync Document is created from the client SDK, the value matches the Access Token's identity
field. If the Sync Document was created from the REST API, the value is system
.
POST https://sync.twilio.com/v1/Services/{ServiceSid}/Documents
The SID of the Sync Service to create the new Document resource in.
application/x-www-form-urlencoded
An application-defined string that uniquely identifies the Sync Document
A JSON string that represents an arbitrary, schema-less object that the Sync Document stores. Can be up to 16 KiB in length.
How long, in seconds, before the Sync Document expires and is deleted (the Sync Document's time-to-live).
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 createDocument() {11const document = await client.sync.v112.services("ServiceSid")13.documents.create({ uniqueName: "user_prefs" });1415console.log(document.sid);16}1718createDocument();
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"revision": "revision",9"service_sid": "ServiceSid",10"sid": "ETaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",11"unique_name": "user_prefs",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Documents/ETaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",13"links": {14"permissions": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Documents/ETaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Permissions"15}16}
1client.document('user_prefs').then(function(doc) {2doc.set({3foregroundColor: "#ffff00",4backgroundColor: "#ff0000"5});6});
Using set will overwrite any existing data in a document.
GET https://sync.twilio.com/v1/Services/{ServiceSid}/Documents/{Sid}
The SID of the Document resource to fetch. Can be the Document resource's sid
or its unique_name
.
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 fetchDocument() {11const document = await client.sync.v112.services("ServiceSid")13.documents("Sid")14.fetch();1516console.log(document.sid);17}1819fetchDocument();
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"revision": "revision",9"service_sid": "ServiceSid",10"sid": "Sid",11"unique_name": "unique_name",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Documents/ETaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",13"links": {14"permissions": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Documents/ETaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Permissions"15}16}
1client.document('user_prefs').then(function(doc) {2console.log(doc.value);3});
1syncClient.document("user_prefs").then(function(doc) {2doc.on("updated",function(data) {3console.log(data);4});5});
GET https://sync.twilio.com/v1/Services/{ServiceSid}/Documents
By default, this will return the first 50 Documents. Specify a PageSize
value to fetch up to 100 items at once. See paging for more information.
The SID of the Sync Service with the Document resources to read.
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 listDocument() {11const documents = await client.sync.v112.services("ServiceSid")13.documents.list({ limit: 20 });1415documents.forEach((d) => console.log(d.sid));16}1718listDocument();
1{2"documents": [],3"meta": {4"first_page_url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Documents?PageSize=50&Page=0",5"key": "documents",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/Documents?PageSize=50&Page=0"11}12}
POST https://sync.twilio.com/v1/Services/{ServiceSid}/Documents/{Sid}
The If-Match HTTP request header
The SID of the Document resource to update. Can be the Document resource's sid
or its unique_name
.
application/x-www-form-urlencoded
A JSON string that represents an arbitrary, schema-less object that the Sync Document stores. Can be up to 16 KiB in length.
How long, in seconds, before the Sync Document expires and is deleted (time-to-live).
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 updateDocument() {11const document = await client.sync.v112.services("ServiceSid")13.documents("Sid")14.update({ data: {} });1516console.log(document.sid);17}1819updateDocument();
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"revision": "revision",9"service_sid": "ServiceSid",10"sid": "Sid",11"unique_name": "unique_name",12"url": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Documents/ETaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",13"links": {14"permissions": "https://sync.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Documents/ETaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Permissions"15}16}
This will modify the foregroundColor key in the Document
1client.document('user_prefs').then(function(doc) {2doc.update({foregroundColor: "#ff0000"});3});
Use mutate for more fine grained control over updates.
1client.document('user_prefs').then(function(doc) {2doc.mutate(function(remoteData) {3remoteData.foregroundColor = "#e2e2e2";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.
DELETE https://sync.twilio.com/v1/Services/{ServiceSid}/Documents/{Sid}
The SID of the Document resource to delete. Can be the Document resource's sid
or its unique_name
.
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 deleteDocument() {11await client.sync.v1.services("ServiceSid").documents("Sid").remove();12}1314deleteDocument();
1syncClient.document("user_prefs").then(function(doc) {2doc.removeDocument().then(function() {3console.log('Document removed.');4});5});