How to access ADS-B data from OpenSky live API?

Warning

OpenSky data are subject to particular terms of use. In particular, if you plan to use data for commercial purposes, you should contact them.

Anonymous access to the OpenSky live API is possible, but functionalities may be limited. The first thing to do once you have an account is to put your credentials in you configuration file. Add the following lines to the [opensky] section of your configuration file.

[opensky]
username =
password =

You can check the path to your configuration file here. The path is different according to OS versions so do not assume anything and check the contents of the variable.

>>> import traffic
>>> traffic.config_file
PosixPath('/home/xo/.config/traffic/traffic.conf')

State vectors

The most basic usage for the OpenSky REST API is to get the instant position for all aircraft. This part actually does not require authentication.

  import matplotlib.pyplot as plt
  import pandas as pd

  from cartes.crs import EuroPP
  from cartes.utils.features import countries

  from traffic.data import opensky

  sv = opensky.api_states()

  with plt.style.context('traffic'):
      fig, ax = plt.subplots(subplot_kw=dict(projection=EuroPP()))

      ax.add_feature(countries())
      ax.gridlines()
      ax.set_extent((-7, 15, 40, 55))
      ax.spines['geo'].set_visible(False)

      sv.plot(ax, s=10, color="#4c78a8")

      now = pd.Timestamp("now", tz="utc")
      ax.set_title(
        f"Snapshot generated at {now:%Y-%m-%d %H:%MZ}",
        fontsize=14
      )
../_images/opensky_rest_0_0.png

Flight tables

Flight tables are accessible by airport (use the ICAO code) given temporal bounds:

# Have you seen Santa Claus coming to Toulouse?
opensky.api_arrival("LFBO", "2021-12-24 20:00", "2021-12-25 06:00")
firstSeen lastSeen icao24 callsign estDepartureAirport estArrivalAirport
3 2021-12-24 19:09:00+00:00 2021-12-24 20:01:26+00:00 3944e8 AFR81YX LFPO LFBO
2 2021-12-24 20:15:51+00:00 2021-12-24 21:35:14+00:00 44ce72 BEL9D EBBR LFBO
1 2021-12-24 20:41:45+00:00 2021-12-24 21:40:43+00:00 394c17 AFR84KV LFPG LFBO
0 2021-12-24 21:06:03+00:00 2021-12-24 22:37:26+00:00 3c6597 DLH11C EDDM LFBO
# Or maybe leaving?
opensky.api_departure("LFBO", "2021-12-24 20:00", "2021-12-25 06:00")
firstSeen lastSeen icao24 callsign estDepartureAirport estArrivalAirport
3 2021-12-24 20:15:26+00:00 2021-12-24 21:03:57+00:00 4b1a1f EZS32CR LFBO LSGG
2 2021-12-24 20:35:00+00:00 2021-12-24 21:50:51+00:00 3922ed TAY4947 LFBO EBLG
1 2021-12-24 20:41:23+00:00 2021-12-24 21:39:38+00:00 344498 IBS36NA LFBO LEMD
0 2021-12-25 05:01:41+00:00 2021-12-25 06:11:08+00:00 394c17 AFR81DB LFBO LFPG

A basic route database is also accessible through the REST API:

opensky.api_routes("AFR292")
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 opensky.api_routes("AFR292")

File ~/work/traffic/traffic/src/traffic/data/adsb/opensky.py:226, in OpenSky.api_routes(self, callsign)
    224 @copy_documentation(rest.REST.routes)
    225 def api_routes(self, callsign: str) -> tuple[str, str]:
--> 226     return self.rest_client.routes(callsign)

File ~/work/traffic/traffic/.venv/lib/python3.11/site-packages/pyopensky/rest.py:214, in REST.routes(self, callsign)
    210 """Returns the route associated to a callsign."""
    211 c = self.session.get(
    212     f"https://opensky-network.org/api/routes?callsign={callsign}"
    213 )
--> 214 c.raise_for_status()
    215 json = c.json()
    217 return tuple(json["route"])

File ~/work/traffic/traffic/.venv/lib/python3.11/site-packages/requests/models.py:1021, in Response.raise_for_status(self)
   1016     http_error_msg = (
   1017         f"{self.status_code} Server Error: {reason} for url: {self.url}"
   1018     )
   1020 if http_error_msg:
-> 1021     raise HTTPError(http_error_msg, response=self)

HTTPError: 404 Client Error:  for url: https://opensky-network.org/api/routes?callsign=AFR292