DEV Community

Cover image for How On Earth Do You Use NASA APIs with Azure Functions? ๐Ÿš€๐ŸŒ
Chloe Condon ๐ŸŽ€ for Microsoft Azure

Posted on • Edited on

How On Earth Do You Use NASA APIs with Azure Functions? ๐Ÿš€๐ŸŒ

This post (co-created with Sophia Li) will walk you through how to use Azure Functions, VSCode, and Twilio to create a text thread between you and Earth (well, you and the NASA EPIC API ๐Ÿคณ).

We built this using the Azure portal + VSCode and is intended for students and learners new to Azure. You can of course build this however you'd like! These particular steps have been written for those new to our portal + VSCode (with lots of visuals to help you along the way!).

Love this post? Hiring? Sophia is job searching! You can contact her on Twitter or check out her website if youโ€™re interested in chatting with her (seriously- hire her before someone else snatches her up- she also bakes amazing sourdough bread! ๐Ÿฅ–๐Ÿž๐Ÿฅฏ).

Happy coding! -๐Ÿ’• Chloe

P.S. There's a bit of a preamble here to give context on this project. If you'd like to skip to the code, scroll down to "Let's Get to the Code! ๐Ÿ‘ฉ๐Ÿผโ€๐Ÿ’ป" below!

Last year, I had the opportunity to experience the launch of SpaceX CRS-18 at NASA's Kennedy Space Center as a NASA Social attendee, and it changed my entire world (errr- universe?) view.

Alt Text

Before my visit to Kennedy Space Center, space (and NASA specifically) felt like something far, far, away- almost untouchable by anyone other than the few and brave astronauts and scientists that made our US launches successful and possible. In elementary school I'd be green with envy over the kid contestants on Double Dare and Legends of the Hidden Temple who'd win grand prizes to space camp for completing obstacle courses. ๐Ÿ†


Believe it or not, this was the ultimate in cool for a young child of the 90s, but alas, I attended theatre & horse camp instead...


...ah, what could have been! ๐Ÿš€๐Ÿ˜ญ

As a theatre kid with ADHD (++ a dash of dyscalculia to add some fun ๐Ÿงฎ), I had always assumed working with NASA was something completely unattainable. Unless I could somehow convince my parents to allow me to compete on Family Double Dare, there was no way I'd ever get close to touching anything space or NASA related. Not to mention the fact I had never seen or heard of anyone who looked/acted/sounded like me doing anything with NASA.

Until I watched the film Hidden Figures and learned about Katherine Johnson, and started following folks like Bianca and Molly on Twitter, NASA (and space related things, for that matter) felt like something men in white lab coats did. And while I do own a lab coat (for Dexter's Lab cosplay reasons) I definitely did not see myself or identify with anyone I saw in pop-culture working on space related things.


Except maybe this look from Miss Cracker on season 10 of Rupaul's Drag Race, but I digress...

Turns out that's far from the truth! Believe it or not, doing cool things with NASA requires way less math, astronaut training, and eating space food than you think (although, that space ice cream stuff they have in museum gift shops is pretty tasty! ๐Ÿฆ).

I was lucky enough to once again attend a NASA Social event- this time, with 2 of my favorite women in STEM: the aforementioned and incredibly talented Bianca and my coworker (+ BFF) April! This time, instead of a launch, we visited Armstrong Flight Research Center in Palmdale California and learned all about the five new NASA Earth science airborne campaigns getting ready to take to the field in 2020! ๐ŸŒ๐Ÿ›ฉ๐ŸŒŠ๐Ÿ›ฐ

For those unfamiliar with NASA, you may be puzzled to hear they would have such a focus on Earth science (vs. the usual coverage of other planets, space travel, etc). However, knowing about our own home planet's behavior and health is just as important as learning what's beyond it! You can learn more about each mission in detail here, but here's a brief summary of the 5 missions discussed at this event:

โ˜ƒ๏ธ Intense Snowfall Events
IMPACTS (the Investigation of Microphysics and Precipitation for Atlantic Coast-Threatening Snowstorms), will study how snow is distributed in the clouds. This is the first major field campaign to study East Coast snowstorms in 30 years (and is particularly exciting since the instrumentation on the aircrafts used to test this has madesignificant advancement since then!

๐ŸŒก Ocean-Atmosphere Heating
Ever heard of an "eddie"? Nope, not this kind. They are circular currents of water that facilitate the exchange of heat between the ocean and the atmosphere and the vertical transport of nutrients, oxygen, and dissolved gases in the upper ocean. Researches using the S-MODE (Sub-Mesoscale Ocean Dynamics Experiment) will make these observations 200 miles off the coast of San Francisco.

๐ŸŒŠ River Deltas and Sea Level Rise
Coastal deltas act as nurseries for fish, crustaceans and other animals, in addition to protecting our infrastructure against hurricanes and tsunamis. The Delta-X mission will provide scientists with data to better understand sea level rise (most major deltas are sinking under and disappearing) and understand and determine if they can mitigate the impacts of the very important coastal resources the deltas provide.

โ˜๏ธ Aerosols Changing Clouds
ACTIVATE (the Aerosol Cloud Meteorology Interactions Over the Western Atlantic Experiment) will measure a broad range of aerosol, cloud and meteorological conditions. Using a NASA Falcon and King Air, researchers will be measuring these conditions, providing them with a more comprehensive measurement to draw definite conclusions about the effects of these interactions.

โ›ˆ When Strong Storms Punch into the Stratosphere
DCOTSS (or Dynamics and Chemistry of the Summer Stratosphere) will be targeting intense storms to investigate that form over the central US in the Summer months. NASA's ER-2 high-altitude aircraft (that can fly 70,000 feet, significantly higher than most research aircraft can go!) as well as weather satellites and ground-based radar, will help us "learn how these storms affect today's stratosphere, and how their impacts might change as the atmosphere changes in coming decadesโ€ according to Ken Bowman, DCOTSS principal investigator from Texas A&M University.

I learned so many things during my 2 visits to NASA, but by far the biggest takeaway has been this: NASA is basically the grandfather of space open sourceโ„ข๏ธ. I used to view NASA as a big powerful organization with top secret vaults, men in astronaut suits, and imposing dudes with ear pieces talking about space things in their top secret space offices with their secret space phones. And while sure, there are many classified things and behind the scenes at NASA that we don't (and may never ๐Ÿ‘ฝ๐Ÿคซ๐Ÿ›ธ) get to see, a majority of NASA's research, data, and findings are publicly available for all to use.

Which is how this project was born! I met Sophia when she attended our Bootcamp Office Hours at the SF Microsoft Reactor. We bonded over our non-traditional backgrounds and shared love of bread (blog post involving this coming VERY soon! ๐Ÿฅ–๐Ÿž). This project was really fun to create together since Sophia was a complete newbie to Azure Functions before this project! We hope you enjoy this project and getting your sea-legs (er- space-legs? ๐Ÿค”๐Ÿš€) with Twilio, Azure, and the many many open NASA APIs available to you!

Alt Text

Let's Get to the Code! ๐Ÿ‘ฉ๐Ÿผโ€๐Ÿ’ป

To follow this tutorial, youโ€™ll need a few things:

โ˜๏ธ An Azure account

Azure Functions make it possible to get projects up and running very quickly, without worrying about spinning up a server. Weโ€™ll be triggering our function with an Azure Functions Timer, but there are many other ways to execute (for other examples, check out other posts on my Dev.to!). If youโ€™ve never worked with Azure before, this Microsoft Learn tutorial on creating your first function could be helpful, and you can dive into the docs too.

โ˜Ž๏ธ: A Twilio account and phone number

Create a Twilio account, and add a trial phone number that can receive SMS and MMS.

This mightโ€™ve been obvious, but youโ€™ll also need your cell phone handy for texting/testing your app. ๐Ÿคณ

๐Ÿ‘ฉ๐Ÿผโ€๐Ÿ’ป: VSCode

We'll be using VSCode in this tutorial and using its Azure Functions extension. Never used VSCode before? Totally confused by what an extension is? ๐Ÿคฏ๐Ÿ˜ฐ Don't worry- we have docs for that! Also, check out the intro video below.

๐Ÿ›ฐ: A NASA API Key

Head over to the NASA API website and generate an API key (keep this handy- we'll need it later to communicate with the Earth!).

We'll be using the EPIC API (Earth Polychromatic Imaging Camera) in our example, but you're welcome to use any NASA API you'd like!

earf

Prepare to Launch ๐Ÿš€

Ok- that was a lot of set-up.

Now that we have all the tools we need, let's get to coding!

1๏ธโƒฃ: Create an Azure Function

For the sake of easy to understand visuals/screenshots I used the Azure portal to create this. You can also use VS Code, the Azure CLI, etc.โ€‹ With Azure Functions you are given the the ability to code and test Functions locally on your machine without having to deploy to the cloud every single time you want to test (a huge time saver!).

To create an Azure Function, you can just start from the Get Started menu and select Function App in the Azure portal.

Then youโ€™ll need to fill in some basic info about your function here. Including the app name, the Azure subscription youโ€™d like to use, a resource group (Iโ€™m creating a new one in this case), the Operating System youโ€™d like to use, the hosting plan (Iโ€™m using consumption), the location Iโ€™d like to use (Iโ€™m in California, so West US 2 is usually my default), the runtime stack Iโ€™d like to use (Iโ€™m using NodeJS in this case), and I have the option to create new storage or use existing. I created a new one in this case.โ€‹โ€‹

Click though until you reach the Review & Create page, and when you're ready click create!

Now it's time to head over to VSCode. Never used VSCode before? No problem! Here's a great video by Rachel Weil to get you up and running in 10 minutes!

Once you have a lay of the land, go ahead and add the Azure Functions VSCode Extension.

2๏ธโƒฃ: Add Your Function to VSCode

Once your function is deployed, head over to VSCode and find your function.

Alt Text

Click the folder icon (as shown above) and create a new file for your project. Then select your language (JavaScript), template (timer trigger), a name for your function (we chose "NASATimerTrigger1"), then enter what time you'd like your timer to go off in the form of a cron expression (we used 0 0 15 * * * to send our texts in the morning, but you'll want to choose this based on your own preference).

Once you've clicked though all of these options, create and open your VSCode project.

In your index.js file, add the following code (don't worry- we'll walk through this in sec!):

You'll notice we have a couple variables here that have not been defined! We want to keep our Twilio credentials, phone numbers, and API keys safe (and secret!)... lucky for us, there's a simple way to do this in Azure using the portal!

3๏ธโƒฃ: Configure Your Application Settings

Navigate over to Configuration and add the following with their values: TWILIO_TOKEN + TWILIO_SID (these can be found on the main page of your Twilio account), SENDER_NUMBER (your Twilio #), RECIPIENT_NUMBER (your number), and your NASA API key (which can be generated here). Once you save these, we'll almost be ready for launch! ๐Ÿš€

Alt Text

Stuck? You can check out our docs for more details on using the Azure Key Vault in the portal as well as through the Azure CLI.

4๏ธโƒฃ: Install a Couple Additional Things

There are a couple additional things we'll need for our app to work. In your VSCode terminal install Twilio with the following command:

npm install twilio

And then install node-fetch with the following:

npm install node-fetch --save

5๏ธโƒฃ: Deploy from VSCode

We're almost there! As a final step, let's deploy the work we did in VSCode to Azure!

Alt Text

Click the deploy button (as shown above- the blue arrow pointing up). You may get some alerts confirming the difference in version here- click through these (we're just confirming our VSCode version differs from the current version in our Azure portal).

If all goes well, and your deployment was successful, it's time to...

5๏ธโƒฃ: Get a Text From Earth!

Now it's time to see our Azure Function in action! Click the Run button on your Azure Function in the Azure portal to test things out! Your text should look a lil somethin' like this:

the earf text

For extra fun, add your Twilio phone # to your contacts, so you know the Earth is hitting up your phone with a selfie each day (errr... is it a selfie if the Earth doesn't have hands so NASA took it? ๐Ÿค”๐ŸŒŽ๐Ÿคณ lol).

6๏ธโƒฃ: Enjoy Your Daily Earth Texts!

If your text sent successfully- congrats! You now will have a daily text from Earth (ok, it's technically the NASA API, but we can pretend!).

As always- please reach out with any questions in the comments below โคต๏ธ- Sophia and I are here to help! We look forward to hearing about all the amazing things you build with the NASA API + Azure Functions, and hope you enjoy the daily pics of our beautiful planet โ™ฅ๏ธ๐ŸŒŽ

Top comments (1)

Collapse
 
timrodz profile image
Juan Alejandro Morais

Hey Chloe & Sophia! Thank you for writing this post! Excited to try it out soon :) P.S. Glad Twilio supports phone numbers in New Zealand (If someone else wonders which other countries are available, head to twilio.com/guidelines/sms).