Runtime Client - Twilio
Register for SIGNAL by 8/31 for $250 off. Register now.

Runtime Client

Twilio Runtime Client provides a simple and intuitive way of orchestrating the various parts of the Twilio Runtime. Using the Runtime Client, developers can access Sync to store data, reference other Functions to better organize their code, and access configuration stored in Assets.

Runtime Client Methods

Method Return Type Description
getAssets() Array of Assets Provides an array of Asset objects that can be used to retrieve files served on Twilio Assets.
getFunctions() Array of Functions Provides an array of Function objects that can be used to compose code hosted on Twilio Functions.
getSync() Twilio Sync Service

Sync Service instance used to manage and access data stored in Twilio Sync

Assets

The Asset object allows developers to load and reference files stored in Twilio Assets. This is deal for configuration data or minified libraries.

Asset Properties

Property Type Description
path String String specifying the location of the Asset

 

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
  
    let path = Runtime.getAssets()['asset-path'].path;
    
    console.log('The path is: ' + path);
    
    callback();
};
Example of how to get the file path for an Asset
Retrieve the path of an Asset

Example of how to get the file path for an Asset

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {

	// First, get the path for the Asset
	let path = Runtime.getAssets()['asset-path'].path;
	
	// Next, simply use the standard require() to bring the library into scope
	let module = require(path);
	
	// Finally, use the module as you would any other 
  console.log('The answer to your riddle is: ' + module.getAnswer());
    
  callback();
};
Example of how to load a third party library stored in an Asset
Load a module from an Asset

Example of how to load a third party library stored in an Asset

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
    var fs = require('fs');
    
    let fileName = 'my_file.txt';
    let file = Runtime.getAssets()[fileName].path;
    let text = fs.readFileSync(file).toString('utf-8');
    
    console.log('Your file contents: ' + text);
    
    callback();
};
Example of how to read the contents of an Asset
Read the contents of an Asset

Example of how to read the contents of an Asset

Functions

The Function object enables developers to compose complex applications by allowing them to include and execute code stored in multiple Functions.

Property Type Description
path String String specifying the location of the Function
Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
    
    let path = Runtime.getFunctions()['function-path'].path;
    
    console.log('The path to your Function is: ' + path);
    
    callback();
}
Example of how to retrieve the file path for a Function
Retrieve the path for a Function

Example of how to retrieve the file path for a Function

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
	
	// First, get the path for the Function
	let path = Runtime.getFunctions()['function-path'].path;
	
	// Next, simply use the standard require() to bring the library into scope
	let zoltar = require(path);
	
	// Finally, use the module as you would any other 
	console.log('The answer to your riddle is: ' + zoltar.ask());
	
	callback();
}
Example of how to include code from other Functions
Include code from a Function

Example of how to include code from other Functions

Sync

We’ve made it easy for you to access the Sync REST API from Functions. Use the Runtime Client to access any of Sync’s real-time data primitives and store information between Function invocations. The same data can be accessed using the Sync API library, making Sync from Functions the perfect way to update real-time apps and dashboards.

How does it work?

The Runtime Client provides a wrapper around the Twilio REST API Helper to make it easy to use Twilio Sync. Calling Runtime.getSync()will return a Sync Service object that has been configured to work with your Default Sync Instance. 

 

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
  
  Runtime.getSync()
    .fetch()
    .then(defaultSyncService => {
        console.log('Sync Service SID: ' + defaultSyncService.sid);

        callback(null, defaultSyncService.sid);
    });
};
Example of how to get the default Sync Service Instance
Get the default Sync Service Instance

Example of how to get the default Sync Service Instance

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
  
  Runtime.getSync({serviceName: 'ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'})
    .fetch()
    .then(syncService => {
        console.log('Sync Service Name: ' + syncService.friendly_name);

        callback(null, syncService.friendly_name);
    });
};
Example of how to use Runtime Client to get an Sync Service Instance by providing the SID
Get an existing Sync Service Instance

Example of how to use Runtime Client to get an Sync Service Instance by providing the SID

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
  
  // First get the default Sync Service
  let sync = Runtime.getSync();
  
  // Then use the maps object to create a new Sync Map
  sync.maps.create({
    uniqueName: "getMap"
  }).then(function(response){
    console.log(respone);
    callback(null);
  });
  
};
  
Example of how to create a Map in Sync with the Runtime Client
Create a Sync Map using Runtime Client

Example of how to create a Map in Sync with the Runtime Client

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
    
	let sync = Runtime.getSync();
	let payload = {
		'name': 'Millenium Falcon'
  };

	sync.maps('greatMap').syncMapItems.create({
        key: 'fastest_ship',
        data: payload
    }).then(function(response){
        console.log(response);
        callback(null);
    });
};
Example of how to add an entry to a Sync Map with the Runtime Client
Add entry to a Sync Map with Runtime Client

Example of how to add an entry to a Sync Map with the Runtime Client

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
    
	let sync = Runtime.getSync();
	
	sync.lists.create({
        uniqueName: "list_of_space_ships",
    }).then(function(response){
        console.log(response);
        callback(null);
    });
};
Example of how to create Sync List with Runtime Client
Create a Sync List with Runtime Client

Example of how to create Sync List with Runtime Client

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {

	let sync = Runtime.getSync();
	let payload = {
		text: 'Millennium Falcon'
	};

	sync.lists('list_of_space_ships').syncListItems.create({
		data: payload
	}).then(function(response){
		console.log(response);
		callback(null);
	});
};
Example of how to append to a Sync List using the Runtime Client
Append to Sync List with Runtime Client

Example of how to append to a Sync List using the Runtime Client

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
	
	let sync = Runtime.getSync();
	let payload = {
		greeting: "Ahoyhoy!"
	};

	sync.documents.create({
		uniqueName: "user_preferences",
		data: payload
	}).then(function(response) {
		console.log(response);
		callback(null);
	});
};
Example of how to create a Sync Document using the Runtime Client
Create a Sync Document with Runtime Client

Example of how to create a Sync Document using the Runtime Client

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Samples...
exports.handler = function(context, event, callback) {
  
    let path = Runtime.getAssets()['asset-path'].path;
    
    console.log('The path is: ' + path);
    
    callback();
};
exports.handler = function(context, event, callback) {

	// First, get the path for the Asset
	let path = Runtime.getAssets()['asset-path'].path;
	
	// Next, simply use the standard require() to bring the library into scope
	let module = require(path);
	
	// Finally, use the module as you would any other 
  console.log('The answer to your riddle is: ' + module.getAnswer());
    
  callback();
};
exports.handler = function(context, event, callback) {
    var fs = require('fs');
    
    let fileName = 'my_file.txt';
    let file = Runtime.getAssets()[fileName].path;
    let text = fs.readFileSync(file).toString('utf-8');
    
    console.log('Your file contents: ' + text);
    
    callback();
};
exports.handler = function(context, event, callback) {
    
    let path = Runtime.getFunctions()['function-path'].path;
    
    console.log('The path to your Function is: ' + path);
    
    callback();
}
exports.handler = function(context, event, callback) {
	
	// First, get the path for the Function
	let path = Runtime.getFunctions()['function-path'].path;
	
	// Next, simply use the standard require() to bring the library into scope
	let zoltar = require(path);
	
	// Finally, use the module as you would any other 
	console.log('The answer to your riddle is: ' + zoltar.ask());
	
	callback();
}
exports.handler = function(context, event, callback) {
  
  Runtime.getSync()
    .fetch()
    .then(defaultSyncService => {
        console.log('Sync Service SID: ' + defaultSyncService.sid);

        callback(null, defaultSyncService.sid);
    });
};
exports.handler = function(context, event, callback) {
  
  Runtime.getSync({serviceName: 'ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'})
    .fetch()
    .then(syncService => {
        console.log('Sync Service Name: ' + syncService.friendly_name);

        callback(null, syncService.friendly_name);
    });
};
exports.handler = function(context, event, callback) {
  
  // First get the default Sync Service
  let sync = Runtime.getSync();
  
  // Then use the maps object to create a new Sync Map
  sync.maps.create({
    uniqueName: "getMap"
  }).then(function(response){
    console.log(respone);
    callback(null);
  });
  
};
  
exports.handler = function(context, event, callback) {
    
	let sync = Runtime.getSync();
	let payload = {
		'name': 'Millenium Falcon'
  };

	sync.maps('greatMap').syncMapItems.create({
        key: 'fastest_ship',
        data: payload
    }).then(function(response){
        console.log(response);
        callback(null);
    });
};
exports.handler = function(context, event, callback) {
    
	let sync = Runtime.getSync();
	
	sync.lists.create({
        uniqueName: "list_of_space_ships",
    }).then(function(response){
        console.log(response);
        callback(null);
    });
};
exports.handler = function(context, event, callback) {

	let sync = Runtime.getSync();
	let payload = {
		text: 'Millennium Falcon'
	};

	sync.lists('list_of_space_ships').syncListItems.create({
		data: payload
	}).then(function(response){
		console.log(response);
		callback(null);
	});
};
exports.handler = function(context, event, callback) {
	
	let sync = Runtime.getSync();
	let payload = {
		greeting: "Ahoyhoy!"
	};

	sync.documents.create({
		uniqueName: "user_preferences",
		data: payload
	}).then(function(response) {
		console.log(response);
		callback(null);
	});
};