TypeScript wrapper for Transport Canberra's NXTBUS API.
This package allows translation to a more modern JSON format compared to the XML garbage which NXTBUS spits out.
Register for an API key here
-
Install with
npm
:npm i nxtbus
-
Use in a Node JS/TS project:
import { NxtbusAPI } from 'nxtbus'; const nxtbus = new NxtbusAPI(process.env.NXTBUS_API_KEY) const route45BusLocations = await nxtbus.getBusLocations(45) const locationJson = await route45BusLocations.json() console.log(JSON.stringify(locationJson, null, 2))
Produces:
{ "timestamp": "2023-01-03T16:19:03+11:00", "status": true, "vehicles": [ { "recordedAt": "2023-01-03T16:18:51.549+11:00", "validUntil": "2023-01-03T16:29:03+11:00", "route": "VM_ACT_0045", "progress": { "distance": 294, "percentage": 54 }, "bus": { "direction": "B", "datedVehicleJourney": "3043-00004-1", "pattern": "115", ...
Our nxtbus
module provides integration with ACT Government's bus stop and vehicle monitoring API.
Each API function returns a form of ServiceResponse
, which can display both XML and JSON outputs, as well as an interactive cheerio
interface within Node.
Note: see the official API reference PDF linked above for information on specific fields.
Once you have an API key, you can create an instance of nxtbus
like so:
- First, import the
NxtbusAPI
class:import { NxtbusAPI } from 'nxtbus'
- Then, create an instance of the object using your key:
const nxtbus = new NxtbusAPI(process.env.NXTBUS_API_KEY)
Now you can pull data from the API.
To track all buses on a specific route, use nxtbus.getBusLocations
.
Let's get the bus locations on route 45, and return it in both XML and JSON formats.
- First,
await
thegetBusLocations
function:const locations = await nxtbus.getBusLocations(45)
- To display the XML, you can simply log it to the console:
console.log(locations.xml)
- JSON is a 2 line process, as the
nxtbus
package does some work in the background to add the locations of bus stops.const locationsJson = await locations.json() console.log(JSON.stringify(locationsJson, null, 2))
To track incoming buses for a specific stop, first you need the bus stop's ID. That can be obtained from the CSV located here (The nxtbus
package pulls this file in the background for up-to-date bus stop information).
The same XML/JSON rules apply here. XML will be skipped; we'll just print the JSON output.
This will uses the bus stop 4455 - Erldunda Cct opp Hawker PS
await
thegetIncomingBuses
function:const busesToStop = await nxtbus.getIncomingBuses(4455)
await
the JSON return:const stopJson = await busesToStop.json()
- Log the output to the console:
console.log(JSON.stringify(stopJson, null, 2))
Add productionTimetable
and estimatedTimetable
endpoints.