If you're facing issues with long URLs in SMS messages, or you want to use your company domain for better branding and click tracking, Twilio's Link Shortening feature can help.
This functionality enables you to shorten long links with your company-branded domain programmatically without any extra API calls. You can also set up your webhook to receive click events when recipients of the messages click on the shortened link.
This tutorial guides you through setting up link shortening and click-tracking using Twilio and DNS providers, let’s get started!
This how-to guide in the Twilio Docs provides instructions for setting up Link Shortening and Click Tracking using Twilio. However, it does not include samples for domain verification and SSL setup. To complement that guide, I have added samples that cover registering and verifying domains, adding DNS records, generating TLS certificates, and configuring click tracking. The document also includes instructions for testing the shortened links using Twilio's built-in test UI or through the API.
- A Twilio account and phone number (You can sign up for an account for free, here)
- A registered domain name
- Access to a DNS provider to configure domain settings
- Account on sslforfree.com for SSL certificate
- Access to beeceptor.com to test click tracking
Step 1: Setting up an Organization
Step 2: Register domains in the Twilio console and DNS console (Alibaba Cloud)
The DNS provider used in this case is Alibabacloud.com, but other providers should have similar settings for editing DNS entries.
I'll use the domain
midshipman.xyz as an example, and I'll use the sub-domain,
twilio.midshipman.xyz, for this guide. Where you see me use either entry, please substitute your domain or subdomain in the step.
To proceed, follow the initial steps from the link provided here. After setup, you should see
twilio.midshipman.xyz in your Twilio Console, and the
_twilio.twilio record in your DNS console, similar to what you can see shown in the screenshots below. It usually takes only a few minutes for the domain to be verified.
Step 3: Adding DNS record
twilio.midshipman.xyz as a sub-domain, create a CNAME record in DNS and point it to
lsct.ashburn.us1.twilio.com. After adding the record, your DNS settings should resemble the screenshot below:
Step 4: Generating a TLS Certificate
To create a free TLS certificate for
twilio.midshipman.xyz, begin by registering an account on sslforfree.com/ (or your choice of SSL provider). The process should be straightforward.
After registering, verify the domain for the certificate in DNS, similar to as shown in the screenshot below.
Now go back to sslforfree.com console to download the certificate.
Step 5: Uploading TLS Certificate
private.key from step 4 is in PKCS #1 format if you used SSL for Free. If that's the case for your provider, convert it to PKCS #8 using the command below, then upload the resulting file to Twilio in your Console.
openssl pkcs8 -in private.key -out private_pkcs8.pem -topk8 -nocrypt
Step 6: Domain configurations with click tracking
https://beeceptor.com/ is a useful tool for setting up webhooks and inspecting HTTP traffic. The callback URL for click tracking in this demo is linktracker.free.beeceptor.com – it will receive the
link clicked events for my demo. You can see the finished sample settings in the screenshot below:
Test link shortening and click tracking
You can use the built-in test GUI in the Twilio console to send an SMS with the link as shown below:
If you want to use the API, don’t forget to set the parameter
ShortenUrls=true. Check this link for more details.
Receive the SMS with the shortened link on the phone:
If you click the link, the original URL will be opened in the browser and you can see the click event on beeceptor.com as shown below:
Setting up Link Shortening with Twilio can be a great way to improve branding and track clicks on your links. By following the steps outlined in this guide, you can set up Link Shortening using your own company branded domain in a straightforward way. Don't forget to test the shortened links using Twilio's built-in test UI or through the API to ensure everything is working as intended.