My Need
Last year, I worked on an application integrating Twilio API for an on-call application.
We had a scenario where the primary person on-call couldn’t reply to the customer calling.
Therefore, after a certain time, a scheduled task would trigger a call to the backup person through the Call API at Twilio.
To do so, we need to make the call in the following manner:
|
|
However, the API checks if the Url
parameter is valid.
Guess what? Locally, the Url
was valid, but not accessible from Twilio point of view.
Solution
I developed a quick app using Netlify Functions.
The goal was that the Url
above would be [http://domain.com/twiml/instructions/call/{dynamic_value}](http://domain.com/twiml/instructions/call/%7Bdynamic_value%7D)
and would reply:
|
|
Step 1: Structure Your Project For Netlify
|
|
Step 2: Initialize And Implement The Project
With npm init -y
, you can initialize the project.
Then, install the dependencies:
|
|
Next, create the index.html
file to provide instructions when loading the base URL.
|
|
Then, we create the function twiml.js
under functions
directory.
|
|
Be careful with XML: in the template string, avoid having any leading newline. Otherwise, you’ll get “error on line 2 at column 10: XML declaration allowed only at the start of the document”.
Finally, let’s configure the netlify.toml
file with the following content:
|
|
First, we tell Netlify where the functions to run are. Note that, by default, Netlify looks up in the netlify/functions
directory if we don’t provide functions = "functions"
.
Then publish = "public"
tell Netlify where is the root directory to serve the application.
Finally, we define a redirect to tell Netlify, on any request to /twiml/*
, to call the function with the wildcard placeholder (:splat
) that captures and forwards any additional path segments after /twiml/
to the function.
Step 3: Deploy The Application
This step is so simple.
Just create an account at Netlify with your preferred Git provider and deploy the application from your repository.
The default settings work great.
Step 4: Test The Application
Browse to the URL provided by Netlify and enter a value in the input.
Click the link that appears below to preview the XML generated.
Conclusion
There you have it! Now, you can use this hosted application on your local environment and test Twilio API against a URL available on the Internet.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Photo by RealToughCandy.com