I need to work out the time it takes to get from two postcodes (multiple combinations hence the code) to arrive by a certain time. Below is the code I have been using:
# Function to fetch journey time between two postcodes
def fetch_journey_time(origin, destination, arrival_date, arrival_time, retries=3):
url = (f"{base_url}/{origin}/to/{destination}?app_id={API_ID}&app_key={API_KEY}"
f"&date={arrival_date}&time={arrival_time}&timeIs=Arriving"
f"&journeyPreference=leasttime&walkingSpeed=Fast&mode=publicTransport,walking"
)
Error message: Journey from SE5 8UF to E15 1BB: No data available Attempt 1 failed with status code: 400 Attempt 2 failed with status code: 400 Attempt 3 failed with status code: 400
However, if I remove &mode=publicTransport,walking. I get: Journey from SE5 8UF to E15 1BB: 58 minutes
The problem I have is when I manually search the postcodes I get times of 48, 51 and 54 minutes. So it feels like I am not asking the same questions. yet I have Fastest route, Fast walking and Arriving Thursday 15th August by 9am on both. So I wondered if it had something to do with mode of transport.
Hopefully, this makes sense and someone can point me in the right direction. It would such a time saver if I could make it work properly.
By default (if you don’t specify the Mode parameter), you should receive a combination of public transport journeys and a walking-only journey.
When you’re manually searching the postcodes, I assume you’re using the Plan a journey tool on our website? That uses a few parameters that differ from the API default (e.g. useMultiModalCall) that may produce different results to what you see in your API request.
Furthermore, if after entering the postcode in the search box on the website, you select a result in the drop-down labelled “powered by Google”, it will translate the postcode into a coordinate pair (latitude and longitude) and plan the journey to/from that rather than the postcode itself. This may also produce a different result compared to if you simply make a request to the original postcode.
Also, please keep in mind that a short journey isn’t necessarily the one that will allow you to leave latest. Consider the following example:
Option A
Depart: 08:30
Arrive: 08:55
Duration: 25 mins
Option B
Depart: 08:20
Arrive: 08:40
Duration: 20 mins
Just going by the duration, Option B may look like the better option because it takes 5 minutes less than Option A. However, if you want to arrive by 09:00, Option A might actually be better because it means you can start your journey 10 minutes later than with Option B. So please keep this in mind.
By the way, just a minor point, but there is no need to specify an app_id in the request URL. Just the app_key is fine.
app_id was a feature of our old API solution, which we migrated away from in 2020:
The ‘new’ solution only uses app_key, which we migrated across. But app_id no longer exists; the parameter is simply ignored. The app_key is all you need for the higher rate limit and stats reports.