Blast off! with SpaceX launch alerts using Azure Logic Apps and Twilio

August 21, 2018
Written by

spacex-rocket-launch-how-to-watch.jpg

SpaceX is on pace to launch nearly 30 rockets to space in 2018.  As a space nerd, I really love watching launches and thankfully SpaceX broadcasts launch streams online.  As frequent as launches are I forget when the next launch is so I can tune in.  Thankfully SpaceX has a great API which includes an endpoint that tells you when the next launch is.  In this post I'll show you how you can build a system using that API, Azure Logic Apps and Twilio SMS to send yourself a daily text message telling you if it's a launch day.

You can build this using a free Azure account and a trial Twilio Account.

Log into your Azure account and create a new Logic App Service.

Once you have the Logic App Service created we can begin to define the workflow we want to run.  Logic App workflows are made up of Connectors which provide Triggers and Actions.  All workflows start with triggers which are followed by actions.  Logic Apps includes a growing gallery of over 200 Connectors.  For this app we'll trigger the workflow using the Schedule Connectors' Recurrence trigger:

The Recurrence trigger lets us run the workflow at a certain interval which in our case is once a day:

With the Trigger in place to start the workflow we can start to add steps. The first step in our workflow will be to grab the next launch data from the SpaceX API.  Click the "New Step" button:  

Search for and select the built-in HTTP Action:

Configure the Action to make a GET request to https://api.spacexdata.com/v2/launches/next.

When the workflow runs the HTTP Action will make an HTTP request to the configured endpoint.  The HTTP response elements, like the headers and body, will be stored as parameters we can access later in the workflow.

With the next launch data fetched we need to parse that data to get the specific date of that launch.  The SpaceX API returns a blob of JSON that we can parse to get that date using the Parse JSON action of the Data Operations connector.

Click the "New Step" button again, search for Parse JSON and select the resulting Action to your workflow.  Once added place your cursor in the 'Content' textbox.  This will open a dialog that lists all of the workflow parameters available to the Action, including those parameters created by the previous HTTP action.

Select 'Body' from the list of dynamic content.

Next we need to give the parser a schema it can use to know how to parse our data.  The SpaceX API doesn't publish a schema, but the Parse JSON action does give us the option to provide it with sample data from which it can infer a schema.  

Grab the sample data by either loading the next launch endpoint URL into your browser and copying the JSON returned or using the sample JSON below from this public Gist and clicking the "Use sample payload to generate schema" link on the action.  

Paste the sample JSON into the input area and click OK.  Once the JSON is parsed all of the values in the JSON become parameters we can access and use later in the workflow.  

The next step in our workflow is to test if a launch is happening today.  We'll use a Condition action from the Control connector to perform that check.

As before click the "Next Step" button, search for the Condition action and select it to add it to the workflow.  Notice that when the Action is added it creates an "If True" path and an "If False" path in our workflow.  Let's define a condition that tests whether the value of the launch_date_local parameter returned by the SpaceX API and today's date, which we can get using the built in utcNow expression, are equal.

Both values start as datetime values so first we need to format them so we are only comparing the 'date' portion of the value.  Logic Apps includes a library of built-in expressions we can use to do this.

To convert the launch_date_local value we'll use the formatDateTime expression as shown below:

formatDateTime(body('Parse_JSON')?['launch_date_local'], 'd')

This expression grabs the launch_date_local value from the JSON parsed in the previous step and then formats that value as a short date.

To create the value in the Logic Apps Design place your cursor in the first 'condition' input.  This opens a builder that lets you select from the dynamic parameters and expressions available in the workflow to build the expression:

To convert the utcNow value, we'll use the convertFromUtc expression:

convertFromUtc(utcNow(), 'Eastern Standard Time', 'd')

This expression gets the current UTC date time, converts it to Eastern Standard Time (the local time of most SpaceX launches), then formats it as a short date string.

Next, in the "If True" branch of the condition, click the "Add an action" button, search for and select the "Send Text Message (SMS)" action from the Twilio Connector.  Once added, the action will prompt you to enter your Twilio Account SID and Auth Token which you can get from the Twilio Console.  

This action verifies your Twilio credentials and then asks you to select a From number from your account, to enter a To phone number and finally the message you want to send.  If you need to buy a new Twilio phone number you can do that from the Phone Numbers dashboard in the Twilio Console.

In my text message body I'm including two parameters that the workflow has parsed from the SpaceX API: the type of rocket launching and the link to the Reddit discussion group for this launch.  Feel free to include any other parameters you like in yours.

In the conditions "Is False" branch, repeat the same process for adding and configuring a "Send Text Message (SMS)" action, setting a message that says that no rockets are launching today:

That's it!  In just a few minutes we've built a simple SMS alert system by snapping together some Azure Logic App Connectors and some API's.  There are loads of other connectors available that expose useful triggers and actions.  Try triggering this same workflow based on launch alerts from @NASA on Twitter or create a new workflow that triggers from an incoming SMS message and returns the time and date of the next launch.

I'd love to to hear what are you building with Logic Apps.  Hit me up on Twitter @devinrader or drop an email to devin@twilio.com.