This section covers how to associate your Messaging Service with your Link Shortening domain.
A Link Shortening domain can be used with multiple Messaging Services, as long as the Messaging Services all belong to an Account within your Twilio Organization.
If you want to set up Link Shortening for another Messaging Service, repeat the steps in this section.
If you're using the Console, follow the Console steps below. If you're using the REST API, use the API steps below.
Console
API
POST
request to the Link Shortening domain's Messaging Services subresource. Place your domain's SID and Messaging Service SID in the URL. An example request is shown below.1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createLinkshorteningMessagingService() {11const linkshorteningMessagingService = await client.messaging.v112.linkshorteningMessagingService(13"DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",14"MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"15)16.create();1718console.log(linkshorteningMessagingService.domainSid);19}2021createLinkshorteningMessagingService();
1{2"domain_sid": "DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",4"url": "https://messaging.twilio.com/v1/LinkShortening/Domains/DNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/MessagingServices/MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"5}
Open a new browser tab or window and navigate to your domain's configuration page.
example.com
), find the DNS record configuration.link.example.com
), find the subdomain's CNAME record configuration.If you're using a root domain, point the A record of the domain to the following 3 IP addresses only. If other IPs are included, Link Shortening does not work.
3.233.187.46
3.233.108.250
54.157.2.211
If you're using a subdomain, point the CNAME record of the subdomain to the following:
lsct.ashburn.us1.twilio.com
Click 'Validate' to validate your DNS settings.
With Link Shortening, Twilio serves redirects for your domain. A TLS certificate allows Twilio to establish an encrypted connection between a customer's browser and Twilio's servers on your behalf (redirects are served over HTTPS). We offer two options for setting up TLS certificates for your Link Shortening domain: Twilio-managed Certificates (preferred) and Bring Your Own Certificate.
Keep the following in mind as you complete this section:
Option 1 (Preferred) - Twilio-managed Certificate: Use our new Certificate Manager feature to entrust Twilio with handling certificate requests and renewals. This is the preferred option.
Console
API
Send a POST request to the Link Shortening RequestManagedCert subresource, placing your domain's SID in the URL.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function updateRequestManagedCert() {11const requestManagedCert = await client.messaging.v112.requestManagedCert("DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.update();1415console.log(requestManagedCert.domainSid);16}1718updateRequestManagedCert();
1{2"certificate_sid": "CWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"domain_name": "https://api.example.com",4"domain_sid": "DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",5"date_created": "2021-02-06T18:02:04Z",6"date_updated": "2021-02-06T18:02:04Z",7"date_expires": "2021-02-06T18:02:04Z",8"url": "https://messaging.twilio.com/v1/LinkShortening/Domains/DNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/RequestManagedCert",9"managed": true,10"requesting": true11}
Option 2 - Bring Your Own Certificate: Most domain hosting services offer managed certificate solutions. There are paid options with annual subscriptions and free options such as letsencrypt.org (with the caveat that most free options only provide an expiration of up to 3 months).
Keep the following in mind when generating your certificate and key:
-----BEGIN CERTIFICATE-----
and end with -----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
and end with -----END PRIVATE KEY-----
or be in PKCS #8 format.Often, third party vendors export different formats of private keys. Twilio Link Shortening accepts PKCS #8 private keys so if your key does not start with -----BEGIN PRIVATE KEY-----
, you need to convert the key using OpenSSL or export the private key again with the proper format specified.
Make note of the certificate's expiration date and upload a new certificate when your current one is about to expire. It is your responsibility to keep track of certificate expiration dates. Link Shortening will not work without a valid TLS certificate; your customers may receive broken links and your Account may be charged for additional message segments due to long URLs.
If you're using the Console, follow the Console steps below. If you're using the REST API, use the API steps below.
Console
Under Upload certificate, click Upload new certificate.
Read the information in the pop-up and click Continue.
In the Upload TLS certificate and private key pop-up, paste your TLS certificate into the TLS certificate input box.
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
Paste your private key into the Private key input box.
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
Click Upload. It takes up to five minutes to validate the certificate and key. Check the status by checking the Cert status under Current certificate details. If you encounter an error at this step, make sure your certificate and private key are in the correct format as described in the previous step.
Go to the "Configure your domain's default behavior" section below.
API
Send a POST
request to your Link Shortening domain's Certificate subresource.
In the request, the TlsCert
parameter's value must be the concatenated certificate and private key. The proper format of the TlsCert
value is shown in the example below.
It can take up to five minutes to validate a certificate. Because of this, Twilio's response to this POST
request may contain a cert_in_validation.status
property with a value of pending
.
Use the request in Step 2 (below) to check the certificate's validation status.
Don't hard-code or save your certificate/key anywhere that might be pushed into version control. If using a Helper Library, use environment variables. The certificate/key is saved as a variable for illustration purposes only.
1// Download the helper library from https://www.twilio.com/docs/node/install2// Find your Account SID and Auth Token at twilio.com/console3// and set the environment variables. See http://twil.io/secure4const accountSid = process.env.TWILIO_ACCOUNT_SID;5const authToken = process.env.TWILIO_AUTH_TOKEN;6const client = require('twilio')(accountSid, authToken);78const certAndPrivateKey = `-----BEGIN CERTIFICATE-----9MIIDqDCCApACCQCBT5e22Q01fjANBgkqhkiG9w0BAQsFADCBlTELMAkGA1UEBhMC10VVMxCzAJBgNVBAgMAkNBMRYwFAYDVQQHDA1TYW4gRnJhbmNpc2NvMRYwFAYDVQQK11DA1FeGFtcGxlLCBJbmMuMRIwEAYDVQQLDAlUZXN0IERlcHQxFDASBgNVBAMMC2V412YW1wbGUuY29tMR8wHQYJKoZIhvcNAQk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n7WXBA+HYEp5LhIlhGuzKlpIET3481sHKim7uywKBgQCyudOURzrd6Qa0SWGFHNNEm4DY0I97S9lhfl3UVMIG9UijXAHi49r0EXu6RGxIHJiwfz7PTaZJMWaWe1h/PP2xtZdo0YvO5scL5UYFZhytC2D7APCJDB50sS4hBVJP7IGKq/vYjfLPunY4mK13SmcpbK/AHhXYDZIe3MTuiu7+twYHQQKBgQDX514IZPq9NFcVvK8nP4pyLcJ80egNcD96iL+bVZWli8O3SzgAixtTE6SVWShNrbrqJk52LOAmZKGCBQd/+R244QLF8CKuBFbaaeHMO96nRwcOQNwg6o/pmdwz21KsmSemYSi453vt+d8vFlIYHSv9LIWhKLTTXl5AGuNlXp2+8o2AjI9QKBgQDXQzJWmiVmTsBYOTWD54YTp2o4Tn0HlLk3KHaBKHGiO0YGVuhXa2jz0u3jg5LLG7vusOZhQBkd4tN7L10ZMM55l5jSr8uncW4/3gZ/O/URQZcMH+Uzttw8nVL3mzLS/M600I+ryZdLkrmtDQWUBRwj56fRre92n5B+9AgpEl6RyegsXToA==57-----END PRIVATE KEY-----`5859client.messaging.v1.domainCerts('DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')60.update({61tlsCert: certAndPrivateKey62})63.then(domain_certs => console.log(domain_certs.domainName));
1{2"certificate_sid": "CWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"domain_name": "https://www.example.com",4"domain_sid": "DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",5"date_expires": "2021-02-06T18:02:04Z",6"date_created": "2021-02-06T18:02:04Z",7"date_updated": "2021-02-06T18:02:04Z",8"url": "https://messaging.twilio.com/v1/LinkShortening/Domains/DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Certificate",9"cert_in_validation": {10"date_expires": "2021-02-06T18:02:04Z",11"status": "pending"12}13}
Check the domain's certificate validation by sending a GET
request to your Link Shortening domain's Certificate subresource. An example request is shown below.
If the certificate's validation is still pending, Twilio's response to this GET
request contains a cert_in_validation.status
property with a value of pending
.
Once the certificate has been validated, Twilio's response to this GET
request contains a cert_in_validation
property with a value of null
.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function fetchDomainCertV4() {11const domainCert = await client.messaging.v112.domainCerts("DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.fetch();1415console.log(domainCert.domainSid);16}1718fetchDomainCertV4();
1{2"certificate_sid": "CWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"domain_name": "https://api.example.com",4"domain_sid": "DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",5"date_expires": "2021-02-06T18:02:04Z",6"date_created": "2021-02-06T18:02:04Z",7"date_updated": "2021-02-06T18:02:04Z",8"url": "https://messaging.twilio.com/v1/LinkShortening/Domains/DNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Certificate",9"cert_in_validation": {10"date_expires": "2021-02-06T18:02:04Z",11"status": "pending"12}13}
This section covers how to configure your Link Shortening domain's default behavior.
The table below describes the domain default settings that can be configured.
Domain default setting | Description |
---|---|
Fallback URL | The URL to which Twilio redirects recipients after a shortened link has expired. |
Callback URL | The URL to which Twilio sends click event data. Twilio sends a request to the Callback URL after a recipient clicks on a shortened link. |
Deliver messages anyway in case of Link Shortening failure (Console) ContinueOnFailure (API) | If there is an error with Link Shortening (e.g. changes to your domain verification or an expired certificate), you can tell Twilio to send messages with the original long links or to not send the messages. The default behavior is to continue sending messages with the original long links (default value is true ). This may result in multiple message segments, which may incur higher costs.Note: This setting only affects Messages with a ShortenUrls parameter set to true . |
Disable HTTPS | Whether or not Twilio removes the https:// prefix from shortened links. If true , shortened links do not have the https:// prefix (e.g., twil.io/j9kj9K3huK9u7 ).If set to false , shortened links have the https:// prefix (e.g., https://twil.io/j9kj9K3huK9u7 ).Default value is false . Please note, it takes up to 15 minutes for the change to be applied. |
Link Shortening domain default configurations are associated with the domain rather than a Messaging Service. This means that you can update the domain defaults and the changes apply to all Messaging Services that use the Link Shortening domain.
In the future, if you want to change a Link Shortening domain's default configuration, repeat the steps in this section.
If you're using the Console, follow the Console steps below. If you're using the REST API, use the API steps below.
Console
In the Domain configuration section, fill in the following fields:
Select or deselect the checkboxes for the following fields:
Click Save.
Go to the "Next Steps" section below.
API
Send a POST
request to the Link Shortening domain's Config subresource with the following parameters:
FallbackUrl
CallbackUrl
ContinueOnFailure
DisableHttps
A sample request is shown below.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function updateDomainConfig() {11const domainConfig = await client.messaging.v112.domainConfig("DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")13.update({14callbackUrl: "https://example.com/my-click-event-callback-endpoint",15continueOnFailure: true,16disableHttps: true,17fallbackUrl: "https://example.com/",18});1920console.log(domainConfig.domainSid);21}2223updateDomainConfig();
1{2"domain_sid": "DNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"config_sid": "ZKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"fallback_url": "https://example.com/",5"callback_url": "https://example.com/my-click-event-callback-endpoint",6"continue_on_failure": true,7"date_created": "2015-07-30T20:00:00Z",8"date_updated": "2015-07-30T20:00:00Z",9"url": "https://messaging.twilio.com/v1/LinkShortening/Domains/DNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Config",10"disable_https": true11}