A lightweight Go client for the Public Transport Victoria (PTV) Timetable API.
- Simple request builder types in
modelwith strongly-typed path/query parameters - An HTTP
clientthat expands{path}placeholders and builds query strings from struct tags - An
authsigner that sends yourdevidand HMAC-SHA1signatureon request - A high-level
ptvpackage that ties everything together behind a single Service
- Requirements: Go installed, PTV API credentials (Developer ID and API key)
- Install:
go get github.com/4gth/ptv@latest- Option 1 (recommended): Environment variables (supports
.envvia github.com/joho/godotenv)
PTV_DEV_ID=1234567
PTV_API_KEY=your_api_key_here- Option 2: Pass credentials directly when constructing the high-level Service
svc := ptv.New("1234567", "your_api_key_here")package main
import (
"context"
"fmt"
"github.com/4gth/ptv/ptv"
"github.com/4gth/ptv/model"
)
func main() {
ctx := context.Background()
// Load from env (or .env)
svc := ptv.NewFromEnv()
// Example: list routes filtered by name
routes, err := svc.Routes(ctx, func(p *model.RoutesParameters) {
p.RouteName = "Sandringham"
})
if err != nil { panic(err) }
fmt.Println(len(routes.Route))
// Example: departures for a stop
deps, err := svc.DeparturesByRouteTypeAndStopID(ctx, func(p *model.DeparturesParameters) {
p.RouteType = 0 // train
p.StopID = 1071 // Flinders Street
p.MaxResults = 10
})
if err != nil { panic(err) }
fmt.Println(len(deps.Departures))
}package main
import (
"fmt"
"github.com/4gth/ptv/auth"
"github.com/4gth/ptv/client"
"github.com/4gth/ptv/model"
)
func main() {
a := &auth.Auth{DevID: "123", APIKey: "<your_api_key_here>"}
aw := auth.NewAuthWriter(a)
c := client.NewClient()
req := model.NewRequest[model.DeparturePayload, model.DeparturesParameters](model.DeparturesByRouteTypeAndStopIDRequest{})
req.Parameters.RouteType = 0
req.Parameters.StopID = 23
c.SetDefaults("timetableapi.ptv.vic.gov.au", "", "https", aw).
SetQuery(req.Path, req.Parameters)
resp, err := c.Get()
if err != nil { panic(err) }
if err := req.UnMarshalPayload(resp); err != nil { panic(err) }
fmt.Printf("%+v\n", req.Payload.Departures)
}auth: Sign requests using your PTVdevidandsignatureclient: Minimal HTTP client with path templating and query buildermodel: Request builders and payload/parameter types for PTV endpointsptv: Unified high-level Service over auth, client, and model
PTV_DEV_ID: Your PTV developer IDPTV_API_KEY: Your PTV API key- Optional
.envin repository root is auto-loaded
This project is licensed under the GPL-3.0. See LICENSE.