Getting Started with Amtrak.js - Track Trains in just 5 lines!
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.
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:
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):