Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Kinesis Quickstart



Create a Kinesis stream for your Account

create-a-kinesis-stream-for-your-account page anchor

We have prepared a script for you that sets up a Kinesis stream with a shard count. You can get the script here. Copy the script and save it to a file named create_kinesis_stream.sh. Make it executable with chmod 755. The script will set up the policies and roles needed to allow Twilio to write to your stream. The only thing you need to do is run the script and pass the stream name and shard count as arguments.

(warning)

Warning

You will need to ensure that your terminal has valid AWS credentials(link takes you to an external page) before executing both this script and the script you will use later in this quickstart.

Use the name twilio-events for the Kinesis stream. You can use any name, but if you use a different one than twilio-events, you will need to change the permission instructions later on with your custom name.

Set the number of shards to 1. This makes initial validation easier. It can be modified after the Sink has been set up.

Here's an example:


_10
./create_kinesis_stream.sh twilio-events 1 | jq . > twilio-sink.json

The script uses the AWS CLI(link takes you to an external page) to create the Kinesis stream with the specific shard count. It will return a JSON payload with the details you need to create a Sink. In the above example, the received data will be written to a file named twilio-sink.json.

(information)

Info

The stream is created in the region specified by the environment variable AWS_DEFAULT_REGION.

You can update the shard count of the Kinesis stream after it has been validated by using this script:


_14
if [ $# -ne 2 ]; then
_14
echo
_14
echo "usage: $0 <stream_name> <shard_count> <scaling_type>"
_14
echo "<scaling_type> possible values UNIFORM_SCALING”
_14
echo
_14
exit 1
_14
fi
_14
_14
# update stream
_14
STREAM_NAME=$1
_14
SHARD_COUNT=$2
_14
SCALING_TYPE=$3
_14
_14
aws kinesis update-shard-count --stream-name $STREAM_NAME --scaling-type $SCALING_TYPE --target-shard-count $SHARD_COUNT

Here is an example showing how the Kinesis stream shard count is increased to 6:


_10
./update_kinesis_shard_count.sh twilio-events 6 UNIFORM_SCALING


(warning)

Warning

You will need to install and set up the Twilio CLI for your account to use the following command.

To create a new Kinesis Sink, run this command:


_10
twilio api:events:v1:sinks:create --description <add description here> \
_10
--sink-configuration '{"arn":"${your kinesis arn}","role_arn":"${your role arn created above}","external_id":"${external id created above}"}' \
_10
--sink-type kinesis


You can test your sink by sending a test message to your Kinesis using this Twilio CLI command:


_10
twilio api:events:v1:sinks:test:create --sid ${sid from above}

Now download this script as cat_kinesis.sh and make it executable with chmod 755. Again, you will need to ensure your terminal has valid AWS credentials(link takes you to an external page) before executing this command:


_10
./cat_kinesis.sh twilio-events

The script will output a test event like this:


_10
{
_10
"specversion": "1.0",
_10
"type": "com.twilio.eventstreams.test-event",
_10
"source": "Sink",
_10
"id": "AC1234567890123456789012",
_10
"dataschema": "https://events-schemas.twilio.com/EventStreams.TestSink/1.json",
_10
"datacontenttype": "application/json",
_10
"time": "2020-08-18T21:10:02.113Z",
_10
"data": "{\"test_id\":\"95001c1e-5b8e-45f5-830d-f9cbf1d16420\"}"
_10
}

(information)

Info

If we are unable to deliver events to your Sink due to a problem with the Sink, we send errors through Twilio Debugger. After the first error about Sink failure, we will continue to notify you through Debugger every 20 minutes. The notification will include the Sink ID and details about the error.


Subscribe to Twilio events

subscribe-to-twilio-events page anchor

Now that you have tested your Sink, you can subscribe to one or more events. Below are some of the available events. You can also view a full list of available events.

Event TypeSchema Version
com.twilio.voice.insights.call-summary.partial1
com.twilio.voice.insights.call-summary.predicted-complete1
com.twilio.voice.insights.call-summary.complete1

You can subscribe to any of these events by making an API call. This is done with the following command. The new subscription is configured to read the event-types from the --types argument — you'll need the event type and schema version from the table above. The event-types you specify in the--types argument will be sent to the Sink specified by the --sink-sid argument. Use the sink SID of the sink you created above.


_10
twilio api:events:v1:subscriptions:create --description <description> \
_10
--sink-sid <sink id above DGxxx> \
_10
--types '{"type": "<event_type>","schema_version": <version>}'

For instance, to subscribe to all call summary events, you would run:


_10
twilio api:events:v1:subscriptions:create \
_10
--description "Subscription on 3 call_summary events" \
_10
--sink-sid <sink id above DGxxx> \
_10
--types '{"type":"com.twilio.voice.insights.call-summary.partial","schema_version":1}' \
_10
--types '{"type":"com.twilio.voice.insights.call-summary.predicted-complete","schema_version":1}' \
_10
--types '{"type":"com.twilio.voice.insights.call-summary.complete","schema_version":1}'


You can quickly tail the events coming out of Kinesis with the cat_kinesis.sh script from above. Again, you will need to ensure your terminal has valid AWS credentials(link takes you to an external page) before executing the script:


_10
./cat_kinesis.sh twilio-events

The script iterates over the specified shards in a stream and requests records from them. Any data that is received is Base64 encoded, so the script decodes it and makes it human readable using jq(link takes you to an external page).


Data is sent to Kinesis in the CloudEvents(link takes you to an external page) format. For Kinesis the Data is automatically base64 encoded/decoded by the SDK. Therefore depending on how you are reading the data from kinesis you may have to base64 decode the data.

For example, here's a Voice Insights Call Summary Predicted Complete event:


_10
{
_10
"specversion": "1.0",
_10
"type": "com.twilio.voice.insights.call-summary.predicted-complete",
_10
"source": "/v1/Voice/CA00000000000000000000000000000000/Summary",
_10
"id": "EZ00000000000000000000000000000000",
_10
"dataschema": "https://events-schemas.twilio.com/VoiceInsights.CallSummary/1",
_10
"datacontenttype": "application/json",
_10
"time": "2020-07-23T22:56:33.000Z",
_10
"data": "{\"call_sid\":\"CA00000000000000000000000000000000\",\"account_sid\":\"AC00000000000000000000000000000000\",\"parent_call_sid\":\"\",\"parent_account_sid\":\"\",\"start_time\":\"2020-07-23T22:56:28Z\",\"end_time\":\"2020-07-23T22:56:33Z\",\"duration\":0,\"connect_duration\":0,\"call_type\":\"client\",\"call_state\":\"canceled\",\"from\":{\"caller\":\"+55555555\",\"callee\":\"\",\"carrier\":\"MEGA:CORP\",\"connection\":\"landline\",\"number_prefix\":\"5555\",\"location\":{\"lat\":0.0,\"lon\":-0.0},\"city\":\"\",\"country_code\":\"US\",\"country_subdivision\":\"\",\"ip_address\":\"\",\"sdk\":null},\"to\":{\"caller\":\"\",\"callee\":\"client:SOMECLIENT\",\"carrier\":\"\",\"connection\":\"twilio_sdk\",\"number_prefix\":\"\",\"location\":null,\"city\":\"\",\"country_code\":\"\",\"country_subdivision\":\"\",\"ip_address\":\"\",\"sdk\":null},\"processing_state\":\"partial\",\"processing_version\":1,\"sip_edge\":null,\"carrier_edge\":null,\"sdk_edge\":null,\"client_edge\":null,\"tags\":[],\"attributes\":null,\"properties\":{\"q850_cause\":0,\"last_sip_response_num\":0,\"pdd_ms\":0,\"route_id\":\"\",\"media_region\":\"unknown_realm\",\"signaling_region\":\"unknown_realm\",\"twilio_media_ip\":\"\",\"twilio_signaling_ip\":\"\",\"external_media_ip\":\"\",\"external_signaling_ip\":\"\",\"sip_call_id\":\"\",\"user_agent\":\"\",\"selected_region\":\"unknown_realm\",\"region\":\"unknown_realm\",\"trunk_sid\":\"\",\"disconnected_by\":\"unknown_disconnected_by\",\"direction\":\"outbound_api\",\"settings\":null}}"
_10
}

The data field can be expected to meet the Voice Insights Call Summary schema(link takes you to an external page).


Rate this page: