NJTAPI is a Go library for accessing data about NJTransit Trains. It wraps the NJTransit HTTP API.
Features include:
- Timetables and statuses of departures from each station.
- Train status including location and stops.
- Station service messages (banner advisories).
- List of all the train stations in the system.
- Service alerts from the RailData GTFS-realtime feed (
Client.Alerts).
See the GoDoc for full details.
import "github.com/bamnet/njtapi"Register with the NJTransit Developer Portal to get a username and password needed to call the API.
Service alerts come from NJTransit's RailData API, which has its own username
and password, separate from the ones above. Pass them with the WithRailData
option:
client := njtapi.NewClient(baseURL, username, password,
njtapi.WithRailData(railDataUsername, railDataPassword))
alerts, err := client.Alerts(context.Background())Without WithRailData, Alerts returns ErrRailDataNotConfigured. The client
logs in to RailData on first use and caches a token for each RailData API.
func main() {
username := "your username"
password := "your password"
client := NewClient("http://njttraindata_tst.njtransit.com:8090/njttraindata.asmx/", username, password)
station, err := client.StationData(context.Background(), "NY")
if err != nil {
log.Fatalf("StationData() error: %v", err)
}
fmt.Println("Departures from New York Penn Station")
for _, departures := range station.Departures {
fmt.Printf("Train to %s at %s", departures.Destination, departures.ScheduledDepartureDate)
}
}Run demo.go for a working demo using a command like:
go run demo/demo.go --base_url="http://njttraindata_tst.njtransit.com:8090/njttraindata.asmx/" --username=<USERNAME> --password=<PASSWORD>Note: Both of the samples above point to a testing api server, not the production one.