Mobile Messaging with Lytics Webhooks and Serverless Functions

View as Markdown
Last updated November 5, 2026

Lytics webhooks paired with serverless functions are a great way to build custom workflows to power your marketing campaigns. This workflow is flexible and can be configured as a trigger for a variety of platforms to send emails, push notifications, and alerts. This example uses a serverless cloud function to send a text message via Twilio to a non-subscribed user when they become highly engaged.

Setup the audience

In this example, you send a personalized text message to a user when they become highly engaged, as determined by Lytics behavioral scores. You also want to send messages only to non-subscribed users who have a phone number, so you need to create an audience with three criteria. First, the user is (or becomes) deeply engaged based on Lytics data science scores. Second, their profile has a phone number. Third, they are not yet a subscriber, because the text message includes a sign-up link.

In the audience builder, use an existing audience for highly engaged users and two custom rules for subscribers and phone numbers. For more instructions on building audiences, see the audience builder docs.

Note: Twilio's API requires phone numbers with a country code (e.g., +11234567890). If phone numbers are not stored in this format in the user's Lytics profile, you can use the cloud function to format the number correctly.

Create the cloud function

This example uses Google Cloud Functions to power this custom workflow; however, it works just as well with other serverless tools, such as AWS Lambda or Azure Functions. For a quickstart on Google Cloud Functions, see Quickstart: Using the gcloud Command-Line Tool.

The Lytics webhook sends the full profile of a user when they enter or exit the audience. The code below receives the profile in the body of the request, then uses the Twilio API to send a custom text message to the user.

Note: You'll also need a Twilio account. When using a Twilio free trial, you can only send text messages to the numbers in your Verified Phone Number list.

// Twilio Client
const accountSid = process.env.ACCOUNT_SID;
const authToken = process.env.AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

exports.sms = (req, res) => {
 const r = req.body; 
 
 // Do not send a text message when the user exits the audience
 if (r.data.segment_events[0].event === "exit") {
  res.status(200).end();
  return;
 } 
 
 const from = 'your twilio number';
 const to = r.data.phone_number;
 const body = `Hey ${r.data.first_name || 'there'}, we thought you might be interested in subscribing. As an added bonus, here is a link to get your first month free: your_link.com?uid=${r.data._uid}`

 // Send the message
 client.messages
  .create({ from, to, body })
  .then(message => console.log(message.sid))
  .then(res.status(200).end('success'))
  .catch(err => res.status(500).end(`error: ${err}`));
}

Next, deploy the function. You can get your account_sid and auth_token from the Twilio dashboard.

gcloud functions deploy sms --set-env-vars ACCOUNT_SID=<your_account_sid>,AUTH_TOKEN=<your_auth_token> --runtime nodejs8 --trigger-http

After you deploy the cloud function, you receive an endpoint that triggers the function. You can also find this endpoint in the Google Cloud console.

Create the Webhook

Next, export the audience you created earlier. Return to Lytics, navigate to Data Pipeline > Jobs, click + Create New, and select the Webhooks provider. Select the Audience Triggers Webhook job type, and then select an authorization or choose to continue without one. From the Audiences list, select the audience you created earlier, enter your cloud function's trigger URL in the Webhook URL field, and click Complete. For the other options on this job, see Audience Triggers. When a user enters or exits the selected audience, the Lytics webhook sends a POST request to the provided URL, which triggers the cloud function.

That’s it! You’ve successfully created a webhook trigger that automatically sends a text message to unsubscribed users when they enter the highly engaged audience.