Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Kinesis quickstart



Prerequisites

prerequisites page anchor

Before continuing with this quickstart, perform the following tasks.


Create a Kinesis stream for your Account

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

The Twilio Kinesis script configures the policies and roles needed to allow Twilio to write to your stream. Run the script and pass the stream name and shard count as arguments.

  1. Copy the script and save it to a file named create_kinesis_stream.sh.

  2. Make this script executable.

    chmod 755 create_kinesis_stream.sh
  3. Run the script.

    • Use the name twilio-events for the Kinesis stream. If you use any other name, change the permission instructions to use your custom name.
    • Set the number of shards to 1. This makes initial validation easier. You can modify it after the Sink has been set up.

    Example of creating a Kinesis stream with shards set to 1

    example-of-creating-a-kinesis-stream-with-shards-set-to-1 page anchor
    ./create_kinesis_stream.sh twilio-events 1 | jq . > twilio-sink.json

To create the Kinesis stream with the specific shard count, the script uses the AWS CLI. It returns a JSON payload with the details you need to create a Sink. In the previous example, the script writes the received data to a file named twilio-sink.json.


Update the Kinesis shard count

update-the-kinesis-shard-count page anchor

To update the shard count of the Kinesis stream after it has been validated, run the following script:

Shard count change script

shard-count-change-script page anchor
1
if [ $# -ne 2 ]; then
2
echo
3
echo "usage: $0 <stream_name> <shard_count> <scaling_type>"
4
echo "<scaling_type> possible values UNIFORM_SCALING"
5
echo
6
exit 1
7
fi
8
9
# update stream
10
STREAM_NAME=$1
11
SHARD_COUNT=$2
12
SCALING_TYPE=$3
13
14
aws kinesis update-shard-count --stream-name $STREAM_NAME --scaling-type $SCALING_TYPE --target-shard-count $SHARD_COUNT

Example of increasing the Kinesis stream shard count to 6

example-of-increasing-the-kinesis-stream-shard-count-to-6 page anchor
./update_kinesis_shard_count.sh twilio-events 6 UNIFORM_SCALING

Create a Kinesis Sink instance

create-a-kinesis-sink-instance page anchor

To create a Kinesis Sink, run this command:

Create a Kinesis Sink example

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

To test your sink, send a test message to your Kinesis.

  1. Run the following Twilio CLI command:

    twilio api:events:v1:sinks:test:create --sid ${SID}
  2. Download the test message script as cat_kinesis.sh.

  3. Make the script executable.

    chmod 755 cat_kinesis.sh
  4. Run the script on your Kinesis stream.

    ./cat_kinesis.sh twilio-events

    The script outputs a test event:

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

If Twilio can't deliver events to your Sink due to a problem with the Sink, errors display in the Twilio Debugger. After the first error about Sink failure, notifications continue in Debugger every 20 minutes. The notification includes the Sink ID and details about the error.


Subscribe to Twilio events

subscribe-to-twilio-events page anchor

With your Sink tested, you can subscribe to one or more events. The following table lists some of the available events. To review other event types, see the Event Types heading in the side table of contents.

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

To subscribe to any of these events, make an API call as shown in the following example. The new subscription reads the event-types from the --types argument. Use the event type and schema version from the previous table. The event-types you specify in the --types argument are sent to the Sink specified given in the --sink-sid argument. Use the SID of the Sink you created.

Example Twilio CLI call to your Sink

example-twilio-cli-call-to-your-sink page anchor
1
twilio api:events:v1:subscriptions:create --description <description> \
2
--sink-sid "DGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
3
--types '{"type": "<event_type>","schema_version": <version>}'

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

Subscribe to all call summary events example

subscribe-to-all-call-summary-events-example page anchor
1
twilio api:events:v1:subscriptions:create \
2
--description "Subscription on 3 call_summary events" \
3
--sink-sid "DGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
4
--types '{"type":"com.twilio.voice.insights.call-summary.partial","schema_version":1}' \
5
--types '{"type":"com.twilio.voice.insights.call-summary.predicted-complete","schema_version":1}' \
6
--types '{"type":"com.twilio.voice.insights.call-summary.complete","schema_version":1}'

Read from your Kinesis stream

read-from-your-kinesis-stream page anchor

You can tail the events coming out of Kinesis with the cat_kinesis.sh script.

Tail the events in the Kinesis stream

tail-the-events-in-the-kinesis-stream page anchor
./cat_kinesis.sh twilio-events

The script iterates over the specified shards in a stream and requests records from them. All data in the stream has been Base64 encoded. The script decodes it and uses jq so humans can read the output.


Twilio sends the data to Kinesis in the CloudEvents(link takes you to an external page) format. The SDK encodes into and decodes from Base64 to Kinesis. Depending on how you read the data from Kinesis, you might have to decode the data from Base64.

Voice Insights Call Summary Predicted Complete event

voice-insights-call-summary-predicted-complete-event page anchor
1
{
2
"specversion": "1.0",
3
"type": "com.twilio.voice.insights.call-summary.predicted-complete",
4
"source": "/v1/Voice/CA00000000000000000000000000000000/Summary",
5
"id": "EZ00000000000000000000000000000000",
6
"dataschema": "https://events-schemas.twilio.com/VoiceInsights.CallSummary/1",
7
"datacontenttype": "application/json",
8
"time": "2020-07-23T22:56:33.000Z",
9
"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 should follow the Voice Insights Call Summary schema(link takes you to an external page).