# Idiots guide to API in R please?

**URL:** <https://opendataforum.transport.nsw.gov.au/t/idiots-guide-to-api-in-r-please/2332>\
**Category:** App development\
**Created:** [March 22, 2021, 9:26pm UTC](https://opendataforum.transport.nsw.gov.au/t/idiots-guide-to-api-in-r-please/2332 "2021-03-22T21:26:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deanius](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/deanius/32/502_2.png) [@Deanius](https://opendataforum.transport.nsw.gov.au/u/Deanius)\
**Post date:** [March 22, 2021, 9:26pm UTC](https://opendataforum.transport.nsw.gov.au/t/idiots-guide-to-api-in-r-please/2332/1 "2021-03-22T21:26:54Z")

</div>

Hi all,  
Pretty comfortable using RStudio but never queried an API before.  
I have an API key for Realtime Vehicle Positions API, is there any R savvy forum members who could help me through a brief query template?

---

<div class="post-metadata">

**Author:** ![jacktech](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jacktech](https://opendataforum.transport.nsw.gov.au/u/jacktech)\
**Post date:** [March 23, 2021, 7:55am UTC](https://opendataforum.transport.nsw.gov.au/t/idiots-guide-to-api-in-r-please/2332/2 "2021-03-23T07:55:36Z")

</div>

Hey Dean,

I did some digging about how to get the positions into R and came up with this solution.

```
install.packages('devtools')
install.packages('httr')
devtools::install_github("SymbolixAU/gtfsway")

library(httr)
library(gtfsway)

url = "https://api.transport.nsw.gov.au/v1/gtfs/vehiclepos/sydneytrains"
response <- httr::GET(url, add_headers(Authorization = "apikey yourkey"))

FeedMessage <- gtfs_realtime(response)
positions = gtfs_vehiclePosition(FeedMessage)

View(positions)

positions[sample(1:length(positions),1)]

```

Hope this helps!

Best,  
Jack

---

<div class="post-metadata">

**Author:** ![Deanius](https://sea1.discourse-cdn.com/flex019/user_avatar/opendataforum.transport.nsw.gov.au/deanius/32/502_2.png) [@Deanius](https://opendataforum.transport.nsw.gov.au/u/Deanius)\
**Post date:** [March 23, 2021, 10:00pm UTC](https://opendataforum.transport.nsw.gov.au/t/idiots-guide-to-api-in-r-please/2332/3 "2021-03-23T22:00:31Z")

</div>

Thanks!  
Response was very helpful and I can tweak it for other APIs.

Much appreciated
