Runtime Client
The Twilio Runtime Client provides a direct way of orchestrating the various parts of the Twilio Runtime without requiring an imported module. Using the Runtime Client, developers can reference other Functions to better organize their code, access configuration and files stored in Assets, and manage real-time data via Twilio Sync.
Access the Runtime Client in a Function by referencing Runtime
, which exposes the following API:
Methods
getAssets()
The getAssets
method returns an object that contains the names each private Asset in a Service. Each Asset name serves as the key to an Asset object that contains the path
to that Asset, as well as an open
method that can be conveniently used to access its contents. These paths can be used to retrieve files served on Twilio Assets.
For example, executing Runtime.getAssets()
could return an object with the following private Assets:
{
'/names.json': {
path: '/var/task/files/ZNdad14da2e70d2533f640cf362fec0609',
open: [Function: open]
},
'/rickroll.mp3': {
path: '/var/task/files/ZNdfbfaf15a02e244fa11337548dabd9d0',
open: [Function: open]
},
'/helper-method.js': {
path: '/var/task/files/ZN5d6d933785a76da25056328a5764d49b',
open: [Function: open]
}
}
getAssets()
only returns private Assets. Public and protected assets can be accessed via their publicly facing urls without the need for calling getAssets()
. Refer to the visibility guide for more context!
Note that an Asset such as names.json
will be returned with a key of '/names.json'
. To correctly retrieve the Asset and its path, the leading /
and extension must be part of the key used to access the object returned by Runtime.getAssets()
.
For example: Runtime.getAssets()['/names.json'].path
Asset Properties
Property | Type | Description |
path |
string |
String specifying the location of the private Asset |
open |
function |
Convenience method that returns the contents of the file from path in utf8 encoding |
Examples
If you would like to include a JavaScript module that isn't available on npm, the best way to do so is to upload the module as a private Asset, then use getAssets
in order to require the module as shown in the Load a module from an asset code example.
getFunctions()
The getFunctions
method returns an object that contains the names of every Function in the Service. Each Function name serves as the key to a Function object that contains the path
to that Function. These paths can be used to import code from other Functions and to compose code hosted on Twilio Functions.
For example, executing Runtime.getFunctions()
could return an object with the following Functions:
{
'sms/reply': {
path: '/var/task/handlers/ZNdad14da2e70d2533f640cf362fec0609.js',
},
'helper': {
path: '/var/task/handlers/ZNdfbfaf15a02e244fa11337548dabd9d0.js',
},
'example-function': {
path: '/var/task/handlers/ZN5d6d933785a76da25056328a5764d49b.js',
},
}
Note that, unlike an Asset, a Function such as sms/reply.js
will be returned with a key of "sms/reply"
. To correctly retrieve the Function and its path, do not include characters such as a leading slash or the .js
extension in the key used to access the object returned by Runtime.getFunctions()
.
For example: Runtime.getFunctions()["sms/reply"].path
Function Properties
Property | Type | Description |
path |
string |
String specifying the location of the Function |
Examples
Include a private Function in another Function
Private Functions are a great way to store methods that may be resused between your other Functions. For example, lets say we have a private Function called Zoltar
that exports a fortune-generating method, ask
:
You could then access this private method by using Runtime.getFunctions()
to get the path for the Zoltar Function, import Zoltar as a JavaScript module using require
, and then access the ask
method as in the following code sample:
getSync(options?)
We’ve made it convenient 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 your real-time apps and dashboards.
The Runtime Client provides a wrapper around the Twilio REST API Helper for Twilio Sync. By default, calling Runtime.getSync()
will return a Sync Service object that has been configured to work with your default Sync Instance.
For added convenience and less typing, the following methods returned from getSync
are renamed from their usual name in the Node.js SDK, as you will see in the examples.
Default method name | Method name in Functions |
syncMaps |
maps |
syncLists |
lists |
Arguments
getSync
optionally accepts a configuration object with the following properties.
Parameter | Type | Description |
serviceName |
string |
String specifying either the serviceSid or uniqueName of the Sync Service to connect to. Defaults to default . |
Examples
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 by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.