Test TwiML Bins using Postman
Postman is an application that you can use either as a standalone application or in your browser to make HTTP requests. You can, for example, use Postman to explore our Twilio APIs, or in this case create an HTTP request to trigger your TwiML Bin.
In order to make a request to your TwiML Bin using Postman, you'll need:
- Have your Account SID and Auth Token available
- Your TwiML Bin Handler — it has the format of
https://handler.twilio.com/twiml/EHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- Generate a valid
X-Twilio-Signature
to pass to your request.
Getting started with our Postman collection
The best way to get started with triggering TwiML Bins from Postman is by using our Twilio Webhook Requests collection on postman.com/twilio.
Once you opened the collection hover over the Send button, and you'll be promoted to Create a fork. Click the button to create a new fork (you might be prompted to sign-up or sign-in with Postman).
Afterwards, create the fork by giving it a label, and selecting the workspace in your account that you want the collection to be added to.
After creating a fork, you'll have to create a new Environment in your account by clicking New at the top left and choosing Environment. Once created, you want to give the environment a name, such as "Your Twilio Environment", by pressing the pencil icon next to New Environment. Next, you have to add two new Variables:
TWILIO_ACCOUNT_SID
with the Current Value of your Twilio Account SIDTWILIO_AUTH_TOKEN
with the Current Value of your Twilio Auth Token
Afterwards, click Save, and select your new environment at the top right in the dropdown reading "No Environment".
You are now ready to make your requests. Open the TwiML Bin Request with Signature request from the left navigation bar, replace the placeholder URL of https://handler.twilio.com/twiml/EHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
with your own TwiML Bin URL, and click Send.
Already familiar with Postman?
If you are already familiar with Postman, you can use the following code in your Pre-request Script section in Postman:
const authToken = pm.environment.get('TWILIO_AUTH_TOKEN');
function interpolate (value) {
return value.replace(/{{([^}]+)}}/g, function (match, $1) {
return pm.variables.get($1);
});
}
function crypto(authToken, data) {
let signature = CryptoJS.HmacSHA1(CryptoJS.enc.Utf8.parse(data), authToken);
let base64 = CryptoJS.enc.Base64.stringify(signature);
return base64;
}
function getSignature(authToken, url, params) {
let data = Object.keys(params).sort().reduce((acc, key) => acc + key + interpolate(params[key]), interpolate(url));
return crypto(authToken, data);
}
pm.environment.set('TWILIO_SIGNATURE', getSignature(authToken, request.url, request.data));
Afterwards, make sure to:
- Create a Postman environment with the
TWILIO_ACCOUNT_SID
andTWILIO_AUTH_TOKEN
variables defined. - Change the
Body
configuration to contain anAccountSid
Key with the Value{{TWILIO_ACCOUNT_SID}}
. - Change the
Headers
configuration to include a Key ofX-Twilio-Signature
with the Value{{TWILIO_SIGNATURE}}
.
This will ensure that a valid X-Twilio-Signature
is automatically generated and inserted into your request headers.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.