Transport for Washington, US

Transportation

Exploring the WMATA Public API with JavaScript

WMATA provides an easy-to-use API that allows developers to access a large amount of transit data in the DC Metro area. This can be extremely useful for creating applications that help commuters navigate the transit system. In this article, we will explore the WMATA Public API and provide some example code in JavaScript.

Getting Started

First, you will need to sign up for a developer account on the WMATA Developer website (https://developer.wmata.com/). After signing up, you will receive a unique API key that you can use to access the WMATA API.

The WMATA API provides a variety of endpoints that you can use to access transit data. Let's explore a few of the most commonly used endpoints.

Endpoint 1: Station Information

The Station Information endpoint provides information about all Metro stations, including the station name, station code, and location. Here is an example of how to access this endpoint using JavaScript:

const apiKey = 'YOUR_API_KEY';
const url = `https://api.wmata.com/Rail.svc/json/jStationInfo?api_key=${apiKey}`;

fetch(url)
  .then(response => response.json())
  .then(data => { 
    // Do something with the data 
  })
  .catch(error => console.error(error));

In this example, we are using the Fetch API to make a GET request to the Station Information endpoint. We are passing our API key as a parameter to the endpoint, which is required for authentication. Once we receive the data, we can perform any necessary processing.

Endpoint 2: Train Positions

The Train Positions endpoint provides real-time information about the location and status of all Metro trains. Here is an example of how to access this endpoint using JavaScript:

const apiKey = 'YOUR_API_KEY';
const url = `https://api.wmata.com/TrainPositions/TrainPositions?contentType=json&api_key=${apiKey}`;

fetch(url)
  .then(response => response.json())
  .then(data => { 
    // Do something with the data 
  })
  .catch(error => console.error(error));

In this example, we are making a GET request to the Train Positions endpoint, passing our API key as a parameter. We are also specifying that we want to receive the data in JSON format. Once we receive the data, we can perform any necessary processing.

Endpoint 3: Next Trains

The Next Trains endpoint provides information about the next trains departing from a given station. Here is an example of how to access this endpoint using JavaScript:

const apiKey = 'YOUR_API_KEY';
const stationCode = 'A01'; // Replace with desired station code
const url = `https://api.wmata.com/StationPrediction.svc/json/GetPrediction/${stationCode}?api_key=${apiKey}`;

fetch(url)
  .then(response => response.json())
  .then(data => { 
    // Do something with the data 
  })
  .catch(error => console.error(error));

In this example, we are making a GET request to the Next Trains endpoint, passing our API key and the station code as parameters. This will return information about the next trains departing from the station with the specified station code.

Conclusion

In this article, we explored the WMATA Public API and provided some example code in JavaScript. WMATA provides a wealth of transit data that can be extremely useful for creating applications to help commuters navigate the Metro system. With the power of JavaScript and the WMATA API, the possibilities are endless!

Related APIs in Transportation