Research Västtrafik API for view #332

Closed
opened 2026-04-16 07:02:40 +00:00 by a24vinla · 2 comments
Collaborator

It would be nice to be able to show incoming/outgoing traffic from Skövde Centralstation (maybe other bus stations).
@a24hirsa 's idea!

https://developer.vasttrafik.se/apis

Parent Issue: #309 #21

It would be nice to be able to show incoming/outgoing traffic from Skövde Centralstation (maybe other bus stations). @a24hirsa 's idea! https://developer.vasttrafik.se/apis Parent Issue: #309 #21
Collaborator

What is needed to get started

To use the API, a developer account is required.

The first steps are:

  • Create an account in the Västtrafik developer portal
  • Create an application
  • Get a client ID and client secret
  • Subscribe the application to the API
  • Use the client ID and client secret to request an access token with OAuth 2
  • Use that access token in API requests

Get started guide:
[https://developer.vasttrafik.se/guides/get-started](Get started)

Relevant APIs

GET /journeys
The main function used to find travel routes between two places.
It returns a list of possible journeys based on the search parameters you provide.

Link:
[https://developer.vasttrafik.se/apis/13/v4#/Journeys/get_journeys](Get Journey)


GET /locations/by-text
This endpoint is used to search for stops by name.
It is useful when the application needs to show journeys from Skövde to other cities, because the destination stop first needs to be found by name.

Link:
[https://developer.vasttrafik.se/apis/13/v4#/Locations/get_locations_by_text](Get locations by text)


GET /locations/by-coordinates
This endpoint is used to find stops close to a specific coordinate.
It is useful when the application needs to show journeys within Skövde, especially from nearby bus stops.

Link:
[https://developer.vasttrafik.se/apis/13/v4#/Locations/get_locations_by_coordinates](Get locations by coordinates)

For Skövde resecentrum, the coordinates are according to Jernhusen:

  • latitude = 58.38955
  • longitude = 13.85193

GID for relevant stops:

  • Skövde gid (Tåg/train station) = 9021014035004000
  • Skövde gid = 9021014035000000
  • Högskoaln, Skövde (bus stop) = 9021014035294000

To find the gid go to västtrafik Hållplatser and in the "Enter stop, address or place" field enter the desired bus/train stop, and in the URl above in your web browser, you will find the gid.


A simple way to structure the solution is

  1. Authenticate with the Västtrafik API and get an access token
  2. Find the origin stop and destination stop
    • use by-text if searching by stop or city name
    • use by-coordinates if finding nearby stops
  3. Get the gid values for the stops
  4. Use those gid values in the journey request
  5. Show the returned journey results in the web application

To work with the API in JavaScript, the package can be installed with:
npm install planera_resa --save

Example of a simple journey request in JavaScript: (extracted from vsttrafik-api/github-repo) All comments and unnecessary fields were removed for readability.

import PlaneraResa from 'planera_resa';

let defaultClient = PlaneraResa.ApiClient.instance;

let auth = defaultClient.authentications['auth'];
auth.accessToken = 'YOUR_ACCESS_TOKEN';

let apiInstance = new PlaneraResa.JourneysApi();

let opts = {
  originGid: "9021014035000000",
  destinationGid: "9021014035340000",
  limit: 5,
  includeNearbyStopAreas: true
};

apiInstance.journeysGet(opts, (error, data, response) => {
  if (error) {
    console.error(error);
  } else {
    console.log(data);
  }
});

In this example:

originGid is the starting stop
destinationGidis the destination stop
limit decides how many journey results should be returned
includeNearbyStopAreas: true allows the API to also consider nearby stop areas

## What is needed to get started To use the API, a developer account is required. ### The first steps are: - Create an account in the Västtrafik developer portal - Create an application - Get a client ID and client secret - Subscribe the application to the API - Use the client ID and client secret to request an access token with OAuth 2 - Use that access token in API requests Get started guide: [https://developer.vasttrafik.se/guides/get-started](Get started) ### Relevant APIs `GET /journeys ` The main function used to find travel routes between two places. It returns a list of possible journeys based on the search parameters you provide. Link: [https://developer.vasttrafik.se/apis/13/v4#/Journeys/get_journeys](Get Journey) ------- `GET /locations/by-text ` This endpoint is used to search for stops by name. It is useful when the application needs to show journeys from Skövde to other cities, because the destination stop first needs to be found by name. Link: [https://developer.vasttrafik.se/apis/13/v4#/Locations/get_locations_by_text](Get locations by text) -------------------------- `GET /locations/by-coordinates ` This endpoint is used to find stops close to a specific coordinate. It is useful when the application needs to show journeys within Skövde, especially from nearby bus stops. Link: [https://developer.vasttrafik.se/apis/13/v4#/Locations/get_locations_by_coordinates](Get locations by coordinates) For Skövde resecentrum, the coordinates are according to Jernhusen: - latitude = 58.38955 - longitude = 13.85193 GID for relevant stops: - Skövde gid (Tåg/train station) = 9021014035004000 - Skövde gid = 9021014035000000 - Högskoaln, Skövde (bus stop) = 9021014035294000 To find the gid go to [västtrafik Hållplatser](https://www.vasttrafik.se/reseplanering/hallplatser/) and in the "Enter stop, address or place" field enter the desired bus/train stop, and in the URl above in your web browser, you will find the gid. ------- ### A simple way to structure the solution is 1. Authenticate with the Västtrafik API and get an access token 2. Find the origin stop and destination stop - use by-text if searching by stop or city name - use by-coordinates if finding nearby stops 5. Get the gid values for the stops 6. Use those gid values in the journey request 7. Show the returned journey results in the web application ---------- To work with the API in JavaScript, the package can be installed with: `npm install planera_resa --save` Example of a simple journey request in JavaScript: (extracted from [vsttrafik-api/github-repo](https://github.com/simonbengtsson/vasttrafik-api/blob/master/mirror/docs/JourneysApi.md#journeysGet)) All comments and unnecessary fields were removed for readability. ``` import PlaneraResa from 'planera_resa'; let defaultClient = PlaneraResa.ApiClient.instance; let auth = defaultClient.authentications['auth']; auth.accessToken = 'YOUR_ACCESS_TOKEN'; let apiInstance = new PlaneraResa.JourneysApi(); let opts = { originGid: "9021014035000000", destinationGid: "9021014035340000", limit: 5, includeNearbyStopAreas: true }; apiInstance.journeysGet(opts, (error, data, response) => { if (error) { console.error(error); } else { console.log(data); } }); ``` In this example: `originGid` is the starting stop `destinationGid`is the destination stop `limit` decides how many journey results should be returned `includeNearbyStopAreas`: true allows the API to also consider nearby stop areas
Collaborator

The research is well structured and follows a clear flow that is quite straightforward, which makes it easy to understand. The research clearly explains all the necessary basic steps that exists from account to app to the API use.

The research has also identified the most important endpointers that are needed and clearly explains what they are used for and how they connect together.

The flow from finding stops to then requesting journeys is also described in such a way that makes it easy to understand how the system would work in practice

overall, the research is clear, accurate and well structured and will provide a solid understanding of how the Västtrafik API works.

The research is well structured and follows a clear flow that is quite straightforward, which makes it easy to understand. The research clearly explains all the necessary basic steps that exists from account to app to the API use. The research has also identified the most important endpointers that are needed and clearly explains what they are used for and how they connect together. The flow from finding stops to then requesting journeys is also described in such a way that makes it easy to understand how the system would work in practice overall, the research is clear, accurate and well structured and will provide a solid understanding of how the Västtrafik API works.
Sign in to join this conversation.
No milestone
No project
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Andras/BoundlessFlowCampus2K#332
No description provided.