PTV Timetable API Key and Signature Document
PTV Timetable API Key and Signature Document
PTV Timetable API Key and Signature Document
Page 1 of 12
Public Transport Victoria
Table of Contents
Getting your API key and user Id........................................................................3
How to register for an API key and user Id......................................................3
How to calculate a signature...............................................................................4
Sample code for creating a signature.................................................................5
Example in .net C#..........................................................................................5
Example in Java..............................................................................................7
Example in Objective C...................................................................................9
Example in Python.........................................................................................12
Page 2 of 12
Public Transport Victoria
To calculate the signature, you’ll need the request, which includes your user
Id, and an API key.
Note
The API key and the request (including user Id) are used to
calculate a signature for every request.
Once we’ve got your email request, we’ll send you an API key and a user
Id by return email.
Note
A high volume of requests may result in a delay in providing you
with your API key and user Id. We’ll try to get it to you as soon as
we can.
We’ll also add your email address to our API mailing list so we can keep
you informed about the API.
Note
PTV does not provide technical support for the API.
The “APIKeyRequest” email address is only used to send you the
API key and user ID as well as any relevant notifications. Only
requests for keys will be responded to.
Note
We’ll be monitoring the use of our API to make sure our mailing
list is current and sustainable. If you haven’t used the API for
over 3 months, we may disable your API key and remove you
Page 3 of 12
Public Transport Victoria
from the list – but you can always register for a new key if you
need one.
Privacy
Your email address is the only bit of information about you that PTV will hold in
its register. View PTV’s privacy policy online.
signature = crypto.HMACSHA1(request,key)
Page 4 of 12
Public Transport Victoria
The following is the .net C# code snippet for the signature calculation.
// add developer id
// encode key
// encode url
url = string.Format("{0}&signature={1}",url,sb.ToString());
Page 5 of 12
Public Transport Victoria
// extra code to add base URL – the resultant url should be:
// http://timetableapi.ptv.vic.gov.au/v2/mode/2/line/787/stops-
for-line?
devid=2&signature=D5474F344CDAA7B92F2253169F6C1D66
C1A15001
Page 6 of 12
Public Transport Victoria
Example in Java
The following is the Java code snippet for the signature calculation.
/**
* @throws Exception
*/
.append("devid=" + developerId);
Page 7 of 12
Public Transport Victoria
mac.init(signingKey);
signature.append("0");
signature.append(Integer.toHexString(intVal));
.append("devid=" +
developerId).append("&signature=" +
signature.toString().toUpperCase());
return url.toString();
Page 8 of 12
Public Transport Victoria
Example in Objective C
The following is the Objective C code snippet for the signature calculation.
-(NSURL*) generateURLWithDevIDAndKey:(NSString*)urlPath {
NSString *hardcodedURL = @”
http://timetableapi.ptv.vic.gov.au”;
/* urlPath = @"
http://timetableapi.ptv.vic.gov.au/v2/mode/2/line/787/stops-for-
line";
*/
[urlString deleteCharactersInRange:deleteRange];
[urlString appendString:@"&"];
else
[urlString appendString:@"?"];
[urlString appendFormat:@"devid=%@",hardcodedDevID];
Page 9 of 12
Public Transport Victoria
NSString *hash;
hash = output;
}else{
Page 10 of 12
Public Transport Victoria
return url;
Page 11 of 12
Public Transport Victoria
Example in Python
The following is the Python code snippet for the signature calculation (our
thanks to Serge in the developer community for providing this after the initial
API release in 2014).
import hmac
import binascii
def getUrl(request):
devId = 2
key = '7car2d2b-7527-14e1-8975-06cf1059afe0'
raw = request+'devid={0}'.format(devId)
signature = hashed.hexdigest()
return
'http://tst.timetableapi.ptv.vic.gov.au'+raw+'&signature={1}'.form
at(devId, signature)
print getUrl('/v2/healthcheck')
Page 12 of 12