When using Programmable Chat or Conversations, you may want to create Webhooks to retrieve data or trigger an event when message are sent or members are removed. Our SDK-driven clients (like mobile phones or browser) will invoke the webhooks without further actions. This includes both Service-level webhooks and Channel-Scoped Webhooks, and as a default behavior helps avoid infinite feedback loops.
However, when using API or Server-side Helper Libraries to invoke some events e.g. send message/remove members (especially from your server), the webhook will not be invoked unless the header Webhook Enabled is set to true e.g. X-Twilio-Webhook-Enabled=true
in your API request.
API Request with X-Twilio-Webhook-Enabled header
For cURL, an example of create member resource would look like this:
curl -H “X-Twilio-Webhook-Enabled:true” -X POST https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members \
--data-urlencode “Identity=identity” \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
For Axios, an example could look like this:
var axios = require('axios');
var qs = require('qs');
var data = qs.stringify({
'Identity': 'identity'
});
var config = {
method: 'post',
url: 'https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members',
headers: {
'X-Twilio-Webhook-Enabled': 'true',
'Authorization': 'Basic VFdJTElPX0FDQ09VTlRfU0lEOiRUV0lMSU9fQVVUSF9UT0tFTg==',
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
Helper Libraries Request with X-Twilio-Webhook-Enabled header
For Helper Libraries, there is equivalent options when invoking the events and it varies from different programming languages. This table shows the differences:
Language | Parameters |
NodeJS, C#, PHP, Java | xTwilioWebhookEnabled |
Ruby, Python | x_twilio_webhook_enabled |
cURL | X-Twilio-Webhook-Enabled |
Twilio-CLI | --x-twilio-webhook-enabled |
An example of a NodeJS call with this option would be:
client.chat.services(ISXXXXXXXXXXXX).channels(CHXXXXXXXXXXXXXX)
.members.create({
identity: identity,
roleSid: “RLXXXXXXXXXXXXXXXX”,
xTwilioWebhookEnabled: true
})
.then(conversation => console.log(conversation.sid));
These are all available in respective resources (e.g. members/channel/message) in both Chat and Conversation. For example, in Conversation Message Resource Document or Chat Member Resource Document.