Real Time Trip Update API - return Code 401

Hi,

I’m trying to use the ‘/v2/gtfs/realtime/sydneytrains’ API to get real time trip updates.

I have an apikey. I have used the apikey on the Explore page of the API and successfully received a response that looked sensible.

The Explore page showed the following Curl command …

curl -X ‘GET’ \ ‘https://api.transport.nsw.gov.au/v2/gtfs/realtime/sydneytrains’ \ -H ‘accept: application/x-google-protobuf’ \ -H ‘Authorization: apikey ’.

I tried to repeat this process in a python program by following the curl above …

    url = 'https://api.transport.nsw.gov.au/v2/gtfs/realtime/sydneytrains'
    params = {'accept': 'application/x-google-protobuf',
                     'Authorization': 'apikey <myapikey>'}

    response = requests.get(url = url, params = params)

I receive a 401 with the following error details:

“ErrorDetails”:{ “TransactionId”:“000001946d135754-2f2a45f”, “ErrorDateTime”:“2025-01-23T12:53:16.474+11:00”, “Message”:“The calling application has not been authenticated.”, “RequestedUrl”:“/v2/gtfs/realtime/sydneytrains”, “RequestMethod”:“GET” }

The only thing I can think of is that there is a parameter missing but I have not been able to find anything in the documentation.

Any advice appreciated.

Thanks.

Kevin.

Hi,

After some further experimentation I have worked out the problem. I used the wrong parameter. It should have been …

response = requests.get(url = url, headers = params)

Regards.

K.