Retrieving Phone Number Pricing for Twilio Programmable Voice using Laravel
Time to read: 2 minutes
The foremost issue when building solutions with third-party services is estimating the cost implications of consuming the API. This tutorial is a guide through the process of understanding the costs of Twilio’s phone number services using a Laravel application.
Installation Requirements
To begin with a Laravel project, all relevant modules required for proper functionality need to be installed. The modules are:
Having installed Laravel on your machine, proceed to create a new project using the Laravel CLI:
Or using the Composer create-project command:
Next, install Twilio’s PHP SDK to call the APIs needed. To make this happen, use Composer to install the dependency with the command:
After the installation is done, add your Twilio API credentials in the .env file as that will be used to authenticate the API requests.
Once the configuration above is completed, create a controller where the functions to retrieve the cost of phone numbers can be written.
Next, import the Twilio PHP SDK into the controller by declaring the namespace at the top of the controller file:
Initialize a Constructor
Next, create a constructor to initialize the Twilio client, so that the appropriate APIs can be used. This is done to avoid repeating a block of code across all controller methods following the KISS principle.
The code for the constructor is as follows:
Create the View
A visual interface for testing all the code is important. For this guide, we will create one and call it pricing.blade.php. It is also important to be aware that all Laravel views are stored in the resources/views/ directory of the Laravel project. Add this code to the newly created view:
Get Phone Number Prices
This API can also be used to find the cost of phone numbers for different countries depending on the options. Add the following function to the PricingController
class:
List Countries with Twilio Phone Numbers
The Pricing API offers solutions to determine the list of countries where Twilio services are available. Add this code to the PricingController
to retrieve the list of countries with the available support for Twilio phone numbers:
Update the Routes
We will now create the routes responsible for returning the view and for calling the methods to retrieve the pricing information in the routes/web.php file.
The route file should look like this:
Final Controller Code
Now lets review the completed controller code:
Testing
To see the application in action, start up your development server using this command in the terminal of your local machine:
Finally, use the web browser of your choice and visit http://localhost:8000. Fill in the forms and submit to get the expected response.
Conclusion
After completing all the enlisted steps, you can successfully retrieve account-specific pricing for Twilio phone numbers in a Laravel application.
I can’t wait to see what you build!
Ugendu Martins Ositadinma is a software developer at filerskeepers. He is passionate about problem solving with the aid of codes and programs. He is also pretty familiar with PHP, Javascript and a couple of frameworks and cloud services. You can always reach him via any of the channels below.
- Email: ugendu04@gmail.com
- Twitter: https://twitter.com/ohssie_
- Github: https://github.com/ohssie
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.