Transport for Victoria, AU
TransportationPTV Timetable Public API
PTV Timetable Public API is a free API provided by Public Transport Victoria that allows developers to access up-to-date timetable and service information for all metropolitan and regional train, tram and bus services in Victoria, Australia. In this blog, we will explore the API and provide examples of how to use it in JavaScript.
Getting Started
Before you can start using the PTV Timetable Public API, you will need to create an account and obtain an API key from Public Transport Victoria. You can sign up for an account at https://www.ptv.vic.gov.au/login/.
API Endpoints
The PTV Timetable API provides a range of endpoints that allow you to retrieve different types of data related to public transportation services in Victoria. Here are some examples:
Get All Train Lines
fetch('https://timetableapi.ptv.vic.gov.au/v3/pattern/runs-by-route/8844/route_type/0', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer API_KEY_GOES_HERE'
},
method: 'GET'
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error))
Get Departures from a Specific Station
fetch('https://timetableapi.ptv.vic.gov.au/v3/departures/route_type/0/stop/1064', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer API_KEY_GOES_HERE'
},
method: 'GET'
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error))
Get All Tram Stops on a Specific Route
fetch('https://timetableapi.ptv.vic.gov.au/v3/stops/route/1/route_type/1', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer API_KEY_GOES_HERE'
},
method: 'GET'
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error))
These are just a few examples of the types of data you can retrieve using the PTV Timetable API.
Conclusion
Using the PTV Timetable Public API, developers can access up-to-date public transportation information for all metropolitan and regional train, tram, and bus services in Victoria. With a little bit of JavaScript knowledge, you can easily retrieve data from the API and use it to build helpful public transportation apps or services. Happy coding!