Using Walk Score's Travel Time API with JavaScript

Walk Score's Travel Time API allows developers to calculate travel times and distances for driving, walking, biking, and public transportation between two points. This API can be particularly useful for applications that provide directions or recommendations for things like restaurants or events. In this post, we'll explore how to use the Travel Time API with JavaScript.

Before we begin

Before using the Travel Time API, developers must create an account with Walk Score and obtain an API key. The API key is used to authenticate requests and should be kept confidential.

Getting started

To start using the Travel Time API with JavaScript, you'll need to make a request using the HTTP GET method. The endpoint for the API is: https://api.walkscore.com/traveltime/?

The Travel Time API requires several parameters to be passed in the request URL. These parameters include:

  • origin: The starting location for the travel time calculation.
  • destination: The destination location for the travel time calculation.
  • mode: The mode of transportation to use in the calculation. Values can be drive, walk, bike, or transit.
  • time: The time at which the travel time should be calculated. This must be in UNIX timestamp format.
  • api_key: The API key obtained from Walk Score.

Example code

Below is some example code that demonstrates how to use the Travel Time API in JavaScript. In this example, we'll calculate the travel time and distance between the Empire State Building in New York City and the White House in Washington, D.C., for driving, walking, biking, and public transportation using the Travel Time API.

const apiKey = 'YOUR_API_KEY_HERE';
const origin = 'Empire State Building, New York, NY';
const destination = 'The White House, Washington, D.C.';
const time = Math.floor(Date.now() / 1000); // Current time in UNIX timestamp format

const modes = ['drive', 'walk', 'bike', 'transit'];
modes.forEach((mode) => {
    const url = `https://api.walkscore.com/traveltime/?origin=${encodeURIComponent(origin)}&destination=${encodeURIComponent(destination)}&mode=${mode}&time=${time}&api_key=${apiKey}`;
    fetch(url)
        .then((response) => response.json())
        .then((data) => {
            console.log(`Travel time for ${mode}: ${data.duration} minutes`);
            console.log(`Distance for ${mode}: ${data.distance} miles`);
        })
        .catch((error) => {
            console.error('Error calculating travel time.', error);
        });
});

In this example, we first define our API key, starting location, and destination location. We then calculate the current time in UNIX timestamp format, which is a required parameter for the Travel Time API. Next, we define an array of the four different modes of transportation (driving, walking, biking, and public transit) that we want to calculate travel times for.

We then loop through each mode of transportation in the modes array and construct a URL for the API request. We use the encodeURIComponent function to safely encode the origin and destination parameters in the URL.

We then use the fetch function to make the API request and return the results as JSON. Finally, we log the travel time and distance for each mode of transportation to the console.

Conclusion

With Walk Score's Travel Time API, developers can easily calculate travel times and distances between two points for different modes of transportation. By using JavaScript, developers can integrate this functionality into their web applications.

Related APIs