In the realm of e-commerce, enhancing customer retention can be achieved through a superior post-purchase experience. By improving communication with customers after they make a purchase, businesses can significantly boost the chances of repeat purchases.
One effective approach involves integrating shipment tracking updates from Shippo with Twilio’s SMS API. This integration allows for automated notifications to customers whenever there’s a status change in their order while it’s in transit. Additionally, businesses can track how many customers opt-in for these notifications and gauge the frequency of updates needed. AWS Lambda and Amazon API Gateway are integral to this process, as they alleviate the burden of resource allocation to handle user load. With serverless, pay-per-use Lambda functions, there’s no need to maintain expensive infrastructure. Moreover, the deployment automation tool, ClaudiaJS, simplifies the entire process.
In this guide, I will take you through a sample project that utilizes the Twilio API alongside the Shippo API to create a service that automatically sends SMS updates regarding shipment tracking. Twilio is responsible for dispatching SMS messages while Shippo communicates order updates to our API Gateway webhook URL.
The following diagram illustrates the workflow (the green path indicates data flow).
Prerequisites
To kick off this tutorial, ensure you have the following:
- Twilio Account: Obtain your account SID and auth token from the dashboard after signing up.
- Shippo Account: Simply enter your API endpoint URL in the webhooks section.
- Node.js 4.3.2: The functions run on NodeJS in AWS.
- ClaudiaJS: This tool is essential for automating the deployment process.
ClaudiaJS is a tool that streamlines the creation and management of AWS Lambda functions and Amazon API Gateway endpoints, automating deployment and configuration tasks.
You can install ClaudiaJS globally on your machine with the command:
npm install -g Claudia
Claudia needs access to your AWS account. Refer to the Installing and Configuring ClaudiaJS for guidance on setting up access credentials to create Lambda functions and API endpoints. To utilize a separate user account, ensure the following roles are assigned:
- AWSLambdaFullAccess – Required for all Claudia deployments.
- IAMFullAccess – Necessary if you want Claudia to automatically create execution roles for your Lambda function (recommended for beginners). If this role is not feasible due to corporate policies or if you prefer not to grant Claudia access to AWS IAM, create a Lambda execution role manually.
- AmazonAPIGatewayAdministrator – Needed if using Claudia API Builder or if you wish to deploy API Gateway Proxy APIs.
Creating Your Project
Create your project folder using the command below (you may skip this if you cloned the repository):
mkdir twilio-shippo && cd twilio-shippo
To quickly initialize your project, use the following command to generate package.json
:
npm init --yes
Once package.json
is created, install the necessary dependencies for your function:
npm install -S twilio claudia-api-builder
The ultimate goal is to reach a setup similar to what’s found in app.js
in the repo. You can copy and modify from there or follow the steps in this tutorial to deploy with ClaudiaJS.
Developing Your Lambda Function
Next, create an application file named app.js
to develop your Lambda function and define your API endpoint. Start by adding your function dependencies at the top of the file:
var ApiBuilder = require('claudia-api-builder'),
api = new ApiBuilder(),
twilio = require('twilio')('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');
Here, you’ll create the API endpoint to capture tracking updates from Shippo. Whenever Shippo detects a new update to the tracking number you’ve posted, it will send updates to your endpoint.
You must export your function so Claudia can package everything for deployment to AWS. Continue in your app.js
file:
module.exports = api;
api.post('sms-updates', function(req){
// Our Lambda logic will go here
});
You are creating a POST endpoint since Shippo will be sending tracking updates. Next, parse the data to relay it to Twilio for SMS messages.
For instance, let’s start by parsing the message body from Shippo. Set up variables to avoid redundancy and add logic to manage cases where a location is not provided along with the tracking update:
api.post('/sms-updates', function(req) {
var body = req.body,
trackingStatus = body.tracking_status,
trackingLocation = 'UNKNOWN';
if (trackingStatus.location && trackingStatus.location.city) {
trackingLocation = trackingStatus.location.city + ', ' + trackingStatus.location.state;
}
// Next, we’ll implement our call to Twilio
});
With the logic for handling the response body in place, we can now proceed to send a formatted SMS through Twilio. The core requirements include the destination number, your Twilio number, and the message itself.
Here’s how it looks after incorporating the code to send the message:
api.post('/sms-updates', function(req) {
var body = req.body,
trackingStatus = body.tracking_status,
trackingLocation = '';
if (trackingStatus.location && trackingStatus.location.city) {
trackingLocation = trackingStatus.location.city + ', ' + trackingStatus.location.state;
}
return twilio
.sendMessage({
to: '+1-TEST_NUMBER', // This should be your destination number
from: '+1-TWILIO_NUMBER', // Your Twilio number
body: 'Tracking #: ' + body.tracking_number +
'nStatus: ' + trackingStatus.status +
'nLocation: ' + trackingLocation
})
.then(function(success) {
// Promise handling to ensure the request is successfully processed
});
});
For a comprehensive guide, consider checking out this excellent resource which offers additional insights. Furthermore, for authoritative information on this topic, visit Chvnci.
Leave a Reply