Getting started with an API example using NASA's Near Earth Object Web Service
One of the most powerful tools available to tinkerers and prototypers is the Application Program Interface. An API is the structure by which two computer programs talk to each other – think of it as a traffic cop for information traveling between two applications.
In a workshop at SXSW EDU last year, I encountered NASA's open data platform. My mind was blown by the fact that a good percentage of NASA's projects reveal their data and code through an open API, and it's all fairly well supported and reasonably easy to get started with.
From here I clicked on the API button:
This brought me to the opening page for the NeoWs.
If you go to the Getting Started documents, it will ask you to sign up for a development key. You don't necessarily need one; you can browse the API catalog and pull out example URLs and use them at will without a key. I chose object 2018GG – there's a nifty visual of this house-sized space rock here.
After pulling out the URL for the data on 2018GG, I put together a simple Python script that I ran in Trinket.
import urllib.request contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3542519?api_key=DEMO_KEY").read() print(contents)
When I ran this, I actually got a return in the console matching all the data available for 2018GG:
I called in the urllib library and used the request method. Next, I created a variable and stored the return coming from the URL connected to 2018GG. Finally, I printed out what's stored in the variable. That’s a lot of data for this simple call. We can simplify things by loading everything into a Json object using the code below.
import urllib.request import json contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3802394?api_key=DEMO_KEY").read() varab = json.loads(contents) print(varab)
This gave me a value and an object notation connected to the value, all comma-separated.
I wanted to find out what the key titles associated with each element of data are, so I wrote a short script that separates out and prints just the keys.
import urllib.request import json contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3802394?api_key=DEMO_KEY").read() varab = json.loads(contents) print(varab.keys())
The printout now shows a whole list of the keys I can work with.
Continuing to narrow things down, I picked out a single element from my list of keys and added some text to it and built a small, very specific printout. I separated out the element line I really wanted by a set of asterisks for readability.
import urllib.request import json contents = urllib.request.urlopen("https://api.nasa.gov/neo/rest/v1/neo/3802394?api_key=DEMO_KEY").read() varab = json.loads(contents) print(varab.keys()) print('*' * 79) print(('object 3802394 is: ')+varab['close_approach_data']['miss_distance']['miles']+(" miles away!!!")) print('*' * 79)
The fun part of all this is that not only can this be implemented in the Trinket environment like I've used here, but it's a great project for Python on the Raspberry Pi. Further steps might include a warning light or buzzer if an object got too close for comfort. This example would employ the GPIO pins on the Raspberry Pi and give a great overview of connected data through an API and physical computing. Many thanks to Cameron here at SFE for wisdom and help in getting this all going!
I hope you have fun with this and happy hacking.