Skip to content
Sign upLog in
← Back to Community

Getting Started with Amtrak.js - Track Trains in just 5 lines!

Profile icon
piemaddHacker

Recently, I took it upon myself to both learn TypeScript and reverse engineer Amtrak's train tracking API. The result of this was the Amtrak NPM Package and my own Amtrak Train Tracking API. I wanted to make it easier for others to use the API, as the data received from Amtrak's endpoint isn't useable without the use of decryption, and even then the resulting data is all in one massive JSON object.

More detailed docs about the API and this library will always be available here, so if you want to use this library in more depth, that is where you should go.

Note: These two examples are available in this repl.

The 5 Lines

The 5 line example I'm showing here is grabbing the list of trains passing through Chicago Union Station (CHI), logging the number of trains in the returned list, and then logging the list itself.

const amtrak = require('amtrak'); // Imports the library as `amtrak` let stationCode = "CHI"; // Station code for Chicago Union Station amtrak.fetchStation(stationCode).then((station) => { // Fetches the data relating to "CHI" (Chichago Union Station) console.log(`There are ${station.length} trains going in and out of ${stationCode} today.`); // Log the number of trains going in/out of the station console.log(station); // Logs the entire list });

Feel free to modify the three letter code in the code example (list of stations and their codes can be found here), for example this is what the code for ATL (Atlanta) would look like:

const amtrak = require('amtrak'); // Imports the library as `amtrak` let stationCode = "ATL"; // Station code for Atlanta Peachtree Station amtrak.fetchStation(stationCode).then((station) => { // Fetches the data relating to "CHI" (Chichago Union Station) console.log(`There are ${station.length} trains going in and out of ${stationCode} today.`); // Log the number of trains going in/out of the station console.log(station); // Logs the entire list });

More Detailed Example

Chances are, your application isn't going to print out the raw JSON you get from the library, so I've made a much cleaner code sample, though it does take up a whopping 20 lines (so much bloat I know /s). This example does the same exact thing, but makes the train information pretty before logging it to the console. I'm also taking advantage of the Date objects returned by the library to print the dates/times in a pleasing fashion and using the library's built in tzConv() function to convert a three-letter timezone to a fully expanded one (EX: EST => America/New_York):

const amtrak = require('amtrak'); let stationCode = "CHI"; // Station code for Chicago Union Station amtrak.fetchStation(stationCode).then((station) => { console.log(`There are ${station.length} trains going in and out of ${stationCode} today.`) station.forEach((train) => { let optionsDate = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', timeZone: amtrak.tzConv(train.tz) }; let optionsTime = { timeStyle: "short", timeZone: amtrak.tzConv(train.tz) }; console.log(`Train Number: ${train.trainNum}`); if (train.schArr) {console.log(`Train scheduled to arrive on ${train.schArr.toLocaleString('en-US', optionsDate)} at ${train.schArr.toLocaleString('en-US', optionsTime)} ${train.tz}`)}; if (train.schDep) {console.log(`Train scheduled to depart on ${train.schDep.toLocaleString('en-US', optionsDate)} at ${train.schDep.toLocaleString('en-US', optionsTime)} ${train.tz}`)}; if (train.postArr) {console.log(`Train arrived on ${train.postArr.toLocaleString('en-US', optionsDate)} at ${train.postArr.toLocaleString('en-US', optionsTime)} ${train.tz}`)}; if (train.postDep) {console.log(`Train departed on ${train.postDep.toLocaleString('en-US', optionsDate)} at ${train.postDep.toLocaleString('en-US', optionsTime)} ${train.tz}`)}; console.log(""); }) })
Voters
Profile icon
Bunnytoes
Profile icon
ch1ck3n
Profile icon
JDOG787
Profile icon
piemadd
Comments
hotnewtop
Profile icon
Bunnytoes

this is cool

Profile icon
ch1ck3n

nice project

Profile icon
ch1ck3n

piero :peepoShy: