CTM Lambda Functions

Lambdas are custom functions that can be fired using a trigger action. Creating a lambda allows you to execute a custom code you created using any triggers' available rules. Lambdas run within a Node.js 18.x environment and are given 256MB and 15 seconds to complete.

Lambdas are available on Sales Engage, Enterprise, and Connect plans.

Creating a Lambda

  1. Navigate to Flows → Lambdas.
  2. Enter a name for this lambda function.
  3. In the Code section, use the interface to write the custom code you wish to execute. The lambda interface gives you the ability to:
    • Select font size
    • Select your editor (Ace, Vim, or Emacs)
    • Full-screen the editor
    • Test the script by selecting a specific call
  4. If you wish to add environment variables to your lambda, click +Add Variable and specify the name and value for your variable.
  5. Click Save Changes.


Running Lambdas via Triggers

To execute your new lambda, you will need to create a trigger or add a new action to an existing trigger. Click here to learn more about trigger setup.

  1. Navigate to Calls → Triggers. If you wish to edit an existing trigger, click edit next to thetrigger namer you want to use to run the lambda. Otherwise, you will need to create a new trigger.
  2. Use the trigger settings to configure when you want the trigger to run and add any necessary rules to the workflow.
  3. To run the lambda function as part of the trigger, click Add Action, then select Run Lambda Function.
  4. Use the drop-down menu to select the name of the lambda you wish to run.
  5. Click Save Changes.



API details


Here is a simple lambda function that exports the handler method and passes an event and a context into the method.   


exports.handler = async (event, context) => {

    const ctm = context.ctm;

    const call = event.activity;



event has a method event.activity that represents the call, text, form, or chat i.e. activity object. context has a few methods that may be helpful.

context.ctm is a helper object to interface with the event.activity and the CTM API directly.

context.done is a useful method when calling a lambda function for voice routing or if you need to let lambda function with data.


If you are asked to score a call/activity when the weather is above 80 at the postal_code
location then you can invoke the following method after helping the fetch the weather via the event.activity.postal_code

await ctm.score("Hot", 5, 80, true, new Date())

You can also update a field on the activity record using the ctm.update method passing it an object.
Update the name and assign the user_field1 custom field to xyz. In this case the user can tell you they have a custom field user_field1

await ctm.update({name: "First Last", custom_fields: {user_field1: "xyz"}})

You can generate code that calls an AI function to answer questions about the activity record e.g.
const name = await ctm.ask("What was the name of the caller?")
const email = await ctm.ask("What was the email address the caller provided?")

Important: all of our integrations e.g. salesforce, use OAuth2 instead of username and password. for salesforce use jsforce
We invoke all methods with a valid access token but other options like instanceUrl are still necessary.
Integration instanceUrl and accessToken are always exposed via the event object e.g. for salesforce, event.salesforce.token. or zoho, event.zoho.token
IMPORTANT: we never ever update the id property with ctm.update

ctm.api_post(path, data)
ctm.api_patch(path, data)
ctm.api_put(path, data)
ctm.api_delete(path, data)

for each of the above you can skip the https://api.calltrackingmetrics.com/api/v1/accounts/{{account_id}}/ prefix, as it is added automatically.



Tagging calls can be made with tag_list when updating the activity. Keep in mind that tag_list will overwrite any previously set tags. For this reason, you should append your new tags to the list of tags. Also, keep in mind that tag_list is a comma-separated string of tags. Tags are always lowercase, so if you try to set a tag with an uppercase letter, it will be downcased automatically.


Running Lambdas via Voice Phone Calls.

In addition to running lambda functions that make external API calls and modify call/text and other activity data, you can also use them to process and route phone calls.

To do this, either select a single tracking number and set its Dial Route to point to the lambda function or, from a Voice Menu or Smart Router, direct the call to the lambda function.


Example Voice Routing Lambda function:


In the following example we'll play a message to the caller and then route them to a receiving number, "dial_number".


context.done(null, {say: "We are routing to the phone number now", route: dial_number});


You can also pass any object to route e.g. an voice menu, call queue or agent by id e.g. VMIxyz or QUExyz or USRxyz. 


You may also want to whisper something to the receiving number using the whisper property.


context.done(null, {whisper: "a call is being routed to you.", route: dial_number});


Collect input from a caller and pass some context data to the collection routine.


context.done(null, {say: "Press one if they are interested in product 1, and press 2 if they are not.", gather: {
finishOnKey: '#',
numDigits: 1,
timeout: 15,
state: context_state


After the caller either timeout is reached or the caller enters a keypress or # in the case above then the function will be called again but with both call.context_state the original context you passed in and the call.digits the key press options the caller entered.

Was this article helpful?
0 out of 0 found this helpful



Article is closed for comments.