Menu

Expand
Rate this page:

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.

Loading Code Sample...
        
        
        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 Sample...
              
              
              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 Sample...
                    
                    
                    Leverage the built-in open method for convenience

                    Read the contents of an Asset

                    Leverage the built-in open method for convenience
                    Loading Code Sample...
                          
                          
                          Directly read the contents of an Asset using filesystem methods

                          Read the contents of an Asset

                          Directly read the contents of an Asset using filesystem methods

                          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

                          Loading Code Sample...
                                
                                
                                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

                                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:

                                Loading Code Sample...
                                      
                                      
                                      Implement Zoltar and define an ask method

                                      Define a private helper Function

                                      Implement Zoltar and define an ask method

                                      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:

                                      Loading Code Sample...
                                            
                                            
                                            Example of how to include code from other Functions

                                            Include code from a Function

                                            Example of how to include code from other Functions

                                            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

                                            Loading Code Sample...
                                                  
                                                  
                                                  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 Sample...
                                                        
                                                        
                                                        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 Sample...
                                                              
                                                              
                                                              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 Sample...
                                                                    
                                                                    
                                                                    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 Sample...
                                                                          
                                                                          
                                                                          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 Sample...
                                                                                
                                                                                
                                                                                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 Sample...
                                                                                      
                                                                                      
                                                                                      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
                                                                                      Loading Code Sample...
                                                                                            
                                                                                            
                                                                                            Example of creating a Sync Map and adding data to it in the same Function execution

                                                                                            Perform multiple Sync actions in a Function

                                                                                            Example of creating a Sync Map and adding data to it in the same Function execution
                                                                                            Rate this page:

                                                                                            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.

                                                                                            Loading Code Sample...
                                                                                                  
                                                                                                  
                                                                                                  

                                                                                                  Thank you for your feedback!

                                                                                                  Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                                                                                                  Sending your feedback...
                                                                                                  🎉 Thank you for your feedback!
                                                                                                  Something went wrong. Please try again.

                                                                                                  Thanks for your feedback!

                                                                                                  thanks-feedback-gif