Access to live bus and river bus arrivals API (stream)

Hi all,

I’ve been trying to figure out how to access the Live bus and river bus arrivals API stream: http://countdown.api.tfl.gov.uk/interfaces/ura/stream_V1

It says a username and password will be sent but I have not received anything (and there’s not much documentation about it available from what I can find).

I’d like to know where all vehicles are in real-time for my app. Does anyone know how I can get access to this stream?

Thanks,
Nathan

Welcome to the forum, @nathanodong

Just pop and email to [email protected] asking for access to the Live Stream, with a subject like " TfL Bus Stream API for the XXX Services for XXX".

I think a human sent me back some credeintials within about four hours.

Here’s my PHP class showing how you access the live stream, once you have some credentials. It’s a bit picky!

 namespace a1z\mtrCrossrail\services\tflDigest;
 class digestCommon
{
const DEBUGMODE = false;
const LINEID = "UL16,UL51,90";
public $isAlreadyRunning = null;
public function setAlreadyRunning($isAlreadyRunning)
{
    $this->isAlreadyRunning = $isAlreadyRunning;
}

public function startDigest($strURL = "http://countdown.api.tfl.gov.uk/interfaces/ura/stream_V1",
                            $strUsername = "LiveBusXXXXX",
                            $strPassword = "password-goes-here",
                            $arrPostParameters = ["LineID" => self::LINEID,
                                "ReturnList" => "StopCode2,StopPointIndicator,DestinationName,EstimatedTime,ExpireTime,VehicleID,StopID"], $intAuth = CURLAUTH_DIGEST, $strFunctionName = "on_curl_write")
{
    if (self::DEBUGMODE) {
        error_reporting(E_ALL);
        ini_set('display_errors', '1');
    }
    if (is_array($arrPostParameters)) {
        $arrPostParameters = http_build_query($arrPostParameters);
    }
    echo PHP_EOL . __FUNCTION__ . " is sending " . strlen(var_export($arrPostParameters, true)) . " bytes to {$strURL}";
    echo PHP_EOL . PHP_EOL . PHP_EOL;
    $arrOptions = [CURLOPT_URL => $strURL,
        CURLOPT_HEADER => false,
        CURLOPT_VERBOSE => self::DEBUGMODE,
        CURLOPT_RETURNTRANSFER => $strFunctionName !== "",
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_SSL_VERIFYPEER => substr($strURL, 0, 5) === "https",    // for https
        CURLOPT_USERPWD => $strUsername . ":" . $strPassword,
        CURLOPT_HTTPAUTH => $intAuth,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $arrPostParameters];
    if ($strFunctionName != "") {
        $arrOptions = $arrOptions + [CURLOPT_WRITEFUNCTION => array($this, $strFunctionName)];
    }
    $resCurlHandle = curl_init();
    curl_setopt_array($resCurlHandle, $arrOptions);
    try {
        $RawResponce = curl_exec($resCurlHandle);
        // validate CURL status
        if (curl_errno($resCurlHandle)) {
            throw new \Exception(curl_error($resCurlHandle), 500);
        }
        // validate HTTP status code (user/strPassword credential issues)
        $intStatusCode = curl_getinfo($resCurlHandle, CURLINFO_HTTP_CODE);
        if ($intStatusCode != 200) {
            throw new \Exception("Response with Status Code [" . $intStatusCode . "].", 500);
        }
    } catch (\Exception $ex) {
        if ($resCurlHandle != null) {
            curl_close($resCurlHandle);
        }
        throw new \Exception($ex);
    }
    if ($resCurlHandle !== null) curl_close($resCurlHandle);
    return $RawResponce;
    }
}

Hi @briantist,

Thanks for the reply.

I’ve noticed that there are multiple Unified API endpoints: api.tfl.gov.uk & push-api.tfl.gov.uk

There is only information about the former in the developer portal. Is there any documentation on the latter? I’m wondering if I can use this for what I need instead.

Also, do you know if there is any documentation on the Live bus and river bus arrivals API stream as I can’t find any.

Thanks!

There’s also http://cloud.tfl.gov.uk/TrackerNet/ which isn’t in the docs either. As a general rule, use the unified API address provided unless it says otherwise!

Hmm… TrackerNet doesn’t look to be what I need for my bus tracking application.

Is there some other kind of way to retrieve the location of all vehicles in service with one API call maybe? I know of this /Vehicle/{ids}/Arrivals endpoint but this would require a request for each vehicle (then further filtering to pick-up the latest ETA which could be quite timely) so I’d rather not go down this route unless I have to. I’d also need to know the vehicle ID ahead of time which would make it difficult to pick up vehicles that are not known to the system.

I’ve done this before by making a call to http://data.tfl.gov.uk/tfl/syndication/feeds/bus-sequences.csv to get all the bus route IDs, and then using that to call http://countdown.api.tfl.gov.uk/interfaces/ura/stream_V1

If you don’t want to call for live updates, don’t use Digest mode, the call will return the bus positions as a CSV and then exit.

You can cache bus-sequences.csv as it doesn’t change very often (weekly, perhaps).

This might be something I can work with. I’m still waiting on credentials for access to the stream. Is there a way I can make the same call to the Instant call? (http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1)

I’m not sure what parameters to put into the HTTP request for this as there’s no documentation to help.

As per the example…

“LineID” => “UL16,UL51,90”,
“ReturnList” => “StopCode2,StopPointIndicator,DestinationName,EstimatedTime,ExpireTime,VehicleID,StopID”

Thanks! This really help me out!

Do you know of any other fields for the ‘ReturnList’ parameter?

This might help:

Simon

2 Likes