Amtrak
Disclaimer
This library and it's creator have no relation to Amtrak. Amtrak and the Atmrak Logo are trademarks of The National Railroad Passenger Corporation (NRPC). The API endpoint used is not intended for use outside of Amtrak's Train Tracking map.
Don't be an A**
- Please refrain from spamming the API, as that is an a**hole move. The API returns an object with everything in it, so there is literally no reason to spam the API.
- If you want to keep your application with the most up to date, I would simply update once per minute (central server) or every 5 to 10 minutes (individual clients).
- In the future, I plan on hosting my own free API which will serve the same data as the Amtrak API, just with some added benefits, such as pulling information for specific trains and stations. I plan on allowing this library to interact with said API, so stay tuned.
Installation
It'n an NPM package lol:
npm install amtrak
Usage/Documentation
Types/Objects
Currently the library returns what it gets from the API, a massive array of trainData
objects. Simply calling fetchTrainData()
fetches the data from the API, cleans it up, and returns it. The following is what a trainData
object looks like:
interface trainData {
routeName: string;
trainNum: number;
coordinates: number[];
lat: number;
lon: number;
heading: string;
velocity: number;
lastValTS: Date;
lastArr: Date;
trainState: string;
statusMsg: string;
serviceDisruption: boolean;
eventCode: string;
destCode: string;
origCode: string;
originTZ: string;
origSchDep: Date;
aliases: number[];
updatedAt: Date;
stations: station[];
};
If you look at the bottom, you see an array of station
objects as well, so it's probably important to know what those are as well:
interface station {
code: string;
tz: string;
bus: boolean;
schArr: Date;
schDep: Date;
schMnt: string;
autoArr: boolean;
autoDep: boolean;
postArr?: Date;
postDep?: Date;
postCmnt?: string;
estArr?: Date;
estDep?: Date;
estArrCmnt?: string;
estDepCmnt?: string;
}
To make better sense of this information, check out examples/completed.json
to see what a completed train's data looks like and examples/inprogress.json
to see what an in progress' train looks like. You can also look in examples/full.json
to see what a full object looks like in JSON. Do note, the dates in these JSON objects are just what happens when you convert a Date
object to a string. In reality, you will have a Date object to work with.
Also, everything is currently synchronous, though I do plan on moving to asynchronous functions in the future.
TS Example
As this library was written in TypeScript, it is naturally easy to use it:
import { fetchTrainData, cleanTrainData } from 'amtrak';
fetchTrainData().then((trainData) => {
console.dir(trainData, { depth: null })
})
JS Example
And of course, as the TS is compliled to JS, you can use that as well:
const amtrak = require("amtrak");
amtrak.fetchTrainData().then((trainData) => {
console.dir(trainData, { depth: null })
});
(wait they're like the same lmao)
Contributing
I don't currently have any official contributing templates, but please make sure to add some testing code to testing/src/testAll.ts
. Anything you add should take one of the existing types and return a modified version of it or a new type which you define in src/types/types.ts
.