Transport for Sao Paulo, Brazil

Transportation

Getting Started with SPTrans API

If you're looking to integrate SPTrans transit data into your web or mobile application, you've come to the right place. In this blog post, we will provide an overview of the SPTrans API and demonstrate how to make basic API calls in JavaScript.

Overview of the SPTrans API

The SPTrans API is a public platform that provides real-time information about bus lines, stops, and schedules in São Paulo, Brazil. With the SPTrans API, you can access comprehensive transit data and develop applications that help commuters plan their trips. To use the SPTrans API, you will need to create an account and request an API key.

Basic API Calls in JavaScript

Before you start making API calls in JavaScript, you will need to include the SPTrans API JavaScript SDK in your project. You can do this by adding the following code to the head of your HTML document:

<script src="https://api.olhovivo.sptrans.com.br/publico/g3/svc/apijavascript/mapasintegrados?apikey={YOUR_API_KEY}"></script>

Note that you will need to replace {YOUR_API_KEY} with your actual API key.

Once you have included the SDK in your project, you can start making API calls. Below are some examples of basic API calls you can make in JavaScript:

Get All Bus Lines

const allBusLines = await $.get('http://api.olhovivo.sptrans.com.br/v2.1/linhas', { apikey: {YOUR_API_KEY} });
console.log(allBusLines);

This API call will return a JSON object containing information about all the bus lines in São Paulo.

Get Stops for a Bus Line

const stopsByBusLine = await $.get('http://api.olhovivo.sptrans.com.br/v2.1/linhas/2932', { apikey: {YOUR_API_KEY} });
console.log(stopsByBusLine);

This API call will return a JSON object containing information about the stops for Bus Line 2932.

Get Departure Times for a Stop

const departureTimes = await $.get('http://api.olhovivo.sptrans.com.br/v2.1/partida/parada/178', { apikey: {YOUR_API_KEY} });
console.log(departureTimes);

This API call will return a JSON object containing information about the departure times for Stop 178.

Conclusion

We hope this short tutorial has given you a basic understanding of how to make API calls to the SPTrans API in JavaScript. With the SPTrans API, you can create applications that help commuters plan their trips and navigate São Paulo's public transit system with ease. If you need more information about the SPTrans API, you can consult the official documentation on their website.

Related APIs