Integrate Twilio alerting and the CoinApi

Aim

Quix allows you to create complex and efficient infraestructure in a simple and quick way. To show you that, you are going to build an application that sends alerts to your phone when the bitcoin price reaches certain threshold.

tutorial graph 1

This guide will take you through the steps to start working with Twilio (developer platform for communications) and the coinAPI (platform that provides data APIs to cryptocurrency) using Quix.

By the end you will have:

  • Built and deployed a realtime Quix model integrated with Twilio

  • Built and deployed a currency exchange rate capture Quix model to feed the Twilio model

Prerequisites

We assume that all you have is a Quix account that you haven’t started using yet.

If you don’t have a Quix account yet, go here and create one.

Overview

This walkthrough covers the following steps:

  1. Create third party accounts: Twilio and CoinApi

  2. Create a workspace

  3. Create a topic

  4. Create new projects for the data collection and reporting models

  5. Deploy both projects

Create free third party accounts

Create a free CoinApi account

tutorial graph 2

CoinAPI is a platform that provides data APIs to cryptocurrency. We will be using it to get live BTC data.

Let’s create a free coinAPI account:

  1. Go to https://www.coinapi.io/.

  2. Click the "Get a free API key" button and complete the dialog to create a free account.

  3. You will receive your API key on your provided email. Keep it safe for later.

Create a free Twilio account

tutorial graph 3

Twilio is a developer platform for communications. We can use Twilio, for instance, to send SMSs.

Let’s create a free Twilio account:

  1. Go to https://www.twilio.com/.

  2. Click the "Sign Up" button and complete the dialog. Do the email and text message verifications.

  3. Then, we will need to setup the Twilio account and create a messaging service. It’s not too hard but there are a couple of sticky points, so we’ll take you through it.

  • Step 3 video-instructions

  • Step 3 written-instructions

Complete step 3 following our short 2 min video here.

Remember to gather your IDS and keep them safe for later: the Messaging Service SID in the Properties section and the Account SID and Auth Token in the Dashboard.

  1. On your first login, tell Twilio:

    • Which product to use: SMS

    • What to build: Alerts & Notifications

    • How to build it: With no code at all

    • What is your goal: Build something myself

  2. Click "Get Started with Twilio".

  3. Once in the Dashboard menu, get a phone number for sending messages:

    • Click "Get Trial Number"

    • You can accept the recommended one by clicking "Choose this Number"

    • That’s the number Twilio is going to be sending SMSs from (and you can receive SMSs there too)

  4. Head over to the Programmable Messaging menu to create a messaging service.

    • Click the Messaging Services section

    • Click the "Create Messaging Service" button

      • Name: Something like "QuixAlerting"

      • Use Case: Notify my users

      • Click "Create"

    • Go to the Senders section to especify the sender phone number:

      • Click "Add Senders" button

      • In the dialog: check that Sender Type is on "Phone Number" and click "Continue"

      • Tick the phone number (it is the one you created earlier)

      • Click "Add Phone Numbers"

  5. Finally, gather your IDS and keep them safe for later:

    • Find the Messaging Service SID in the Properties section

    • Find the Account SID and Auth Token in the Dashboard menu

Create a Workspace

Ok, you have set up free accounts both for Twilio and CoinApi. Now you will use Quix to easily create a model that employs CoinApi prices to generate alerts to your phone via Twilio!

So get to the Quix Portal and log in.

Table 1. Creating a workspace

tutorial graph 4

A workspace is a collection of all the components for a single application. Find more info here.

new workspace

First, click the + sign to create a workspace.

naming new workspace

Use a name such as "Bitcoin Tutorial Workspace" and click create.

This may take half a minute since a discreet, fully featured and highly scalable data platform is being created for you.

Once it is ready, open the workspace.

Create a Topic

Once the workspace has been created, we will generate a topic.

Table 2. Creating a topic

tutorial graph 5

Topics are streams of data or parameters built on Kafka. Find more info here.

create topic

To create a topic, go to the Topics Page (see menu on the left) and then click on the "+ CREATE TOPIC" button.

naming new topic

Use a name such as "CurrencyDataTopic".

Click create.

This may take half a minute. A discreet, fully featured and highly scalable data platform is being created for you.

Once it is ready, open it.

data persistance on

Turn on data persistence (this will store the data stremead into you topic).

You can do it whilst the topic is being created.

select topic details

copy topic id

Copy the Topic id. To do so:

  1. Click the >> icon on the left of our CurrencyDataTopic row

  2. Then on the right hand side click the "Copy to Clipboard" icon

Keep the Topic ID safe for later: you will need this in the project code.

Create Projects and Deploy

Now let’s create the 2 projects we are going to need.

Table 3. Creating the two projects

tutorial graph 6

A project is a set of code which can be deployed as one Docker image. Find more info here.

new project

Go to the Projects Page using the menu on the left.

Then, click the "+ CREATE PROJECT" button to create a new project.

naming new project 1

naming new project 2

You don’t have to name your two projects the same as this. But something similar will be good.

  1. CoinApiDataProject: This will be reading data from the CoinApi!

  2. TwilioAlertingProject: This will be sending messages to your phone!

The examples are in Python, so choose that for now. If you don’t know Python don’t worry, we’ve done all the hard work for you and you don’t need to run anything on your local machine. Get started with these samples then adapt them to your preferred language later.

CoinApiDataProject Code

Now open up the CoinApiDataProject. This is the place where we will code to stream CoinAPI data into your Quix topic.

Code

  1. Your main code will be placed at your project’s main.py, which is the file that the deployment will invoke.

  2. Copy the certificatepath, username, password and broker address in your main.py and keep it safe somewhere, you will need it later.

  3. You don’t have to do the coding this time, it’s done for you. So head over to our GitHub pages for our CoinApi Code and copy it. Paste this into the main.py file.

  4. After you have pasted the code in from GitHub, replace the "{placeholders}" with the propper ids:

  • {placeholder:broker.security.certificatepath}

  • {placeholder:broker.security.username}

  • {placeholder:broker.security.password}

  • {placeholder:broker.address}

The project’s certificatepath, username, password and broker address ids respectively that you have just saved (step 2 above).

  • {placeholder:topic}

This one will be the same Topic Id you obtained earlier. This project will read from the topic.

  • {placeholder:token}

The CoinAPI key you received on your email.

  1. Save all the changes in main.py

Dependencies

You can easily solve dependencies in the requirement.txt file.

In our example, Python needs to be told about an additional dependency required by the code. So, open requirements.txt and add the following line to the bottom of this file and save it:

requests==2.25.1

Deploy CoinApiDataProject

Now the CoinApiData project can be deployed.

Find more info on deployments here.

commit history

Have a look at the right section in your screen. This is your commit history.

Your code is all committed to Git repository in GitHub. Find more info here.

naming new tag

The top row is the most recent commit. Click on the three dots next to it and then on "Create tag".

Give the tag a meaningful name such as 'CoinApiProject_V1'.

Click Create.

deploy button 2

With our work properly saved, we can now deploy the project. Click the Deploy button.

deployment dialog 1

Let’s have a look at the deployment dialog (but don’t Deploy yet).

  • General Tab

  • Variables Tab

  • Network Tab

On the General tab you can select:

  • The version of the code you want to deploy

  • The deployment type

    • Job will run the code once.

    • Service will run the code and restart it if it stops.

  • The resources you want to allocate

The Variables tab allows you to enter any environment variables required by the code at runtime.

You may, for example, want to deploy multiple instances of this code, each looking for specific currencies.

The Network tab allows you to make the deployment public on the internet.

general deployment settings

In the General tab, select the Version Tag you created earlier. You decide which code to deploy this simply.

Also change the Deployment Type to Service.

You can change the name too. For example, to CoinApiProject_V1_Deployment_1

deployment dialog 2

Then select the Variables tab.

The code needs values for coinapi_key, from_currency and to_currency.

  • coinapi_key: The coinapi_key should have been obtained earlier from coinapi.io.

  • from_currency: from_currency can be any currency you want to use. We have used BTC (If not using BTC make a note of the one you want to use for later).

  • to_currency: to_currency can again be any currency you like. we have used USD. (If not using USD make a note of the one you want to use for later).

It’s not useful to make this deployment publicly visible. So we’ll skip the network tab.

DEPLOY

Hit it!

Just click Deploy and the code will be built, deployed and the service will start running.

deployment running

It’s alive!

You should see the status change to Running.

contextual options deployment

deployment logs

Checking your deployment

Hover your mouse over the row to find some contextual options.

You can view the logs and deployment logs, download the docker image and delete the deployment.

Open the logs to check the current currency exchange!

TwilioAlertingProject Code

The CoinApiDataProject is ready and live, so let’s focus now on the TwilioAlertingProject. Go to the Develop Page by clicking its icon on the left menu and open then the TwilioAlertingProject.

Code

  1. Same as before, copy the certificatepath, username, password and broker address in your main.py and keep it safe somewhere, you will need it later.

  2. Again same as before, you don’t have to do the coding! Get our TwilioAlerting Code from our GitHub pages and copy it. Paste this into the main.py file.

  3. After you have pasted the code in from GitHub, replace the "{placeholders}" with the propper ids:

  • {placeholder:broker.security.certificatepath}

  • {placeholder:broker.security.username}

  • {placeholder:broker.security.password}

  • {placeholder:broker.address}

The project’s certificatepath, username, password and broker address ids respectively that you have just saved (step 1 above).

  • {placeholder:topic}

This one will be the same Topic Id you obtained earlier. This project will read from the topic.

  • {placeholder:account_sid}

  • {placeholder:auth_token}

  • {placeholder:messaging_service_sid}

Replace these with Twilio’s account_sid, auth_token and messaging_service_sid respectively.

  • {placeholder:numbers}

Your phone number including international code. e.g. +44 for UK or +01. (Can also be a list of numbers separated by commas).

  1. Set up your desired threeshold: When the exchange rate crosses the specified boundary defined you will receive a text message. The exchange rate is set to 50k by default. Feel free to change this to something more relevant at the time you are working on this example to receive a SMS.

  2. Save all the changes in main.py

Dependencies

We need to import Twilio’s library so open requirements.txt and add the following line to the bottom of this file and save it:

twilio

Deploy TwilioAlertingProject

Now as before, make sure you have saved all changes and Tag the latest version with a sensible tag.

deployment twilio

Click Deploy and edit the deployment dialog:

  • Select the Version Tag you just created.

  • Then change the Deployment Type to Service.

  • We have no environment variables or network settings for this deployment so just click Deploy!

Logs

After deployment you should check the logs for both services. Within the Deployment Page (click the Deployment icon in the menu on the left if you are not there), hover your mouse over any of the rows to view the logs access. Click on it.

In the CoinApiData log you should see your currency pair followed by the current rate or value. e.g.

BTC-USD: 53595.61293074192

In the TwilioAlerting log you should see an indication that the code is listening for data and might see a "New stream read" line. e.g.

Listening to streams. Press CTRL-C to exit.
New stream read:coin-api-data

Recap - What did you just do!

Check your phone, have you got a message? Then:

  • You created, coded and deployed a realtime, always on solution to the Quix serverless compute environment. All for Free!

  • You created two services (two code projects). One to read data from another platform and one to make decisions based on that data and send notifications via SMS. In real time.

  • You gained some experience of navigating the Quix portal and how to configure deployments. Including using and setting environment variables.

What’s Next

What else can you use Quix for?

You can stream any kind of data into the Quix platform, from other apps, websites and services. From hardware, machinery or wearables or from anything you can think of that outputs data. You can then process that data in any imaginable way.

See what you can do with it and plase share your experiences with us. In fact, share it with your colleagues, friends and family. Blog, tweet and post about it and tell anyone who will listen about the possibilities.

If you run into trouble please reach out to us. We’ll be more than happy to help.