How to Find Where the International Space Station is Right Now with Code

Proto Bioengineering
5 min readMar 20, 2023

--

Find the ISS with Python, R, Bash, or even no code at all.

Photo by Bhavya Pratap Singh on Unsplash

The International Space Station (ISS) is orbiting the Earth at about 17,100 mph. Does anybody know where it is?

Yes! Besides the government space agencies who work on it (in the US, Japan, European Union, Russia, and Canada), we scientists and engineers can find where the ISS is right now using the Open Notify API.

Further reading: What the Heck is an API?

All we have to do is write 1–5 lines of code to ask the Open Notify API for the latitude and longitude of ISS right this second. Examples for several languages (Bash, Python, and R) are below.

We’ll get data from the API that looks like this (this format is called JSON):

{
"iss_position": {
"latitude": "34.1509",
"longitude": "-9.2171"
},
"message": "success",
"timestamp": 1679250290
}

Which can be used in your own project to generate maps like this:

This is the example app from the Open Notify API.

Let’s see some examples.

Example 1: The No-Code Version

If you don’t want to code but want to see the data, all you have to do is visit the Open Notify API directly in your browser here.

This data is just like the JSON example from the last section (minus the {brackets}). This is the location of the ISS right this second (along with a Unix timestamp — a universal timestamp that tells time down to the second)

This is not very useful to us as scientists and engineers, since we’d have to refresh the page manually every time we want fresh data. Thus the examples below will show several ways to get the data in an automated way with code.

Example 2: The Command Line Version (AKA “Bash”)

This is almost as easy as the no-code example. We’re going to type one line into the command line.

The command we’ll type into our command line. This example is from a MacBook.

If you’re new to the command line, it’s where software engineers do a lot of their coding and development stuff day to day. To open your command line:

To use the API, once we have our command line app open, we will:

  • Type curl http://api.open-notify.org/iss-now.json
  • Press Enter.

After 2–3 seconds, the API at open-notify.org will respond back with JSON, just like in the last section, but it will be pure text instead of a webpage.

This is the same data as the previous examples but without indented formatting to make it easy to read. It’s all in one line.

We can run this script as many times as we want to get ISS data. Just be polite and don’t run it 100 times a second, because API servers cost Open Notify money to run.

Example 3: Python

The full Python script is:

import requests

open_notify_api = "http://api.open-notify.org/iss-now.json"
iss_location = requests.get(open_notify_api)
print(iss_location.json())

This asks the Open Notify API for the ISS location data, extracts the JSON from Open Notify’s response, and prints it.

We use the requests library, which handles HTTP requests in Python. This can be installed with pip install requests or you can go here if you need more assistance installing `requests`.

Put the above code in a Python file called iss.py and run it from the command line.

Our script will reach out to the Open Notify API, then print the JSON:

Note how it’s in a totally different position (latitude and longitude) than the previous examples, since the ISS moves thousands of kilometers per hour.

Example 4: R

The full R script is:

install.packages("httr")
library(httr)

open_notify_api <- "http://api.open-notify.org/iss-now.json"
response <- VERB("GET", open_notify_api)

iss_location <- content(response, "text")
iss_location

Above, we are:

  • installing and importing the R HTTP library httr
  • GETting the data from the Open Notify API and storing it in response
  • extracting the JSON (which has the latitude and longitude) from the HTTP response
  • printing the location JSON to the console by writing iss_location

In RStudio, this looks like:

At the bottom, we see some JSON with the current latitude and longitude of the space station.

The Gist

In each of these languages, we’re using the backbone of the Internet, HTTP requests, to ask an API to send back some interesting numbers back to us. That is one of the major uses of web APIs for scientists: getting cool data.

You can now do things like write a for loop to continuously ask Open Notify about the ISS’s location once per second and use that data in a live graphic, just like in the example on Open Notify’s website.

Be polite when using free APIs by adding a sleep() statement or something similar to make sure you only query the API once per second or around that time frame. Or else your computer may run through the loop as quickly as it can, sending 1000 requests all at once.

Next Steps

Turn this data into a live dashboard with our tutorial, How to Make a Live Dashboard of the ISS’s Location with Python and Plotly.

Questions and Feedback

If you have questions or feedback, email us at protobioengineering@gmail.com or message us on Instagram (@protobioengineering).

If you liked this article, consider supporting us by donating a coffee.

Related Articles

--

--

Proto Bioengineering

Learn to code for science. “Everything simple is false. Everything complex is unusable.” — Paul Valery