# Using API in R: optimized route

**URL:** <https://support.routexl.com/t/using-api-in-r-optimized-route/2674>\
**Category:** API\
**Created:** [November 28, 2023, 12:55pm UTC](https://support.routexl.com/t/using-api-in-r-optimized-route/2674 "2023-11-28T12:55:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TijmenW](https://support.routexl.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@TijmenW](https://support.routexl.com/u/TijmenW)\
**Post date:** [November 28, 2023, 12:55pm UTC](https://support.routexl.com/t/using-api-in-r-optimized-route/2674/1 "2023-11-28T12:55:25Z")

</div>

Hi All,

I would like to create a request to the API in R, but I am trying to find the correct inputs to httr2 [https://httr.r-lib.org/reference/POST.html](https://httr.r-lib.org/reference/POST.html), even after reading its documentation. I am not sure of the placement of the locations input argument.

locations:  
[{“address”:“Stratemakersveste 7, Arnhem”,“lat”:“51.944183168583265”,“lng”:“5.8522888008339029”},{“address”:“Spoordijk 8, Staphorst”,“lat”:“52.615909311957573”,“lng”:“6.2417455514837954”},{“address”:“Papekopperdijk1 / Hogebrug, Papekop”,“lat”:“52.040796021740633”,“lng”:“4.8417212064734638”},{“address”:“Spoordijk 3, Heukelom”,“lat”:“51.571771503906803”,“lng”:“5.1497328563086251”},{“address”:“Knoevenoordstraat 3, Brummen”,“lat”:“52.099297487035557”,“lng”:“6.1498974393968391”},{“address”:“Parallelweg 19, Sliedrecht”,“lat”:“51.832969772618505”,“lng”:“4.763065453190281”}]

req = POST(url2, authenticate(username, password) ,…)

How do i finish the statement above using locations as input the correct way?

UPDATE: I was able to solve it using the httr2 library:

> url2 = ‘[https://api.routexl.com/tour](https://api.routexl.com/tour)’  
> req ← request(url2) |\> req\_auth\_basic(username, password)  
> req %\>% req\_dry\_run() # test  
> req ← req %\>% req\_body\_form(locations = locations)  
> resp ← resp\_body\_json(req\_perform(req))

---

<div class="post-metadata">

**Author:** ![RouteXL](https://support.routexl.com/user_avatar/support.routexl.com/routexl/32/347_2.png) [@RouteXL](https://support.routexl.com/u/RouteXL)\
**Post date:** [November 28, 2023, 7:16pm UTC](https://support.routexl.com/t/using-api-in-r-optimized-route/2674/2 "2023-11-28T19:16:25Z")

</div>

We’re no R experts and we didn’t test, but it seems you should add your locations JSON to the arguments. We’d also think it may help to set the encoding.

`req = POST(url2, authenticate(username, password), locations = "<your JSON>", encode = "form")`

---

<div class="post-metadata">

**Author:** ![TijmenW](https://support.routexl.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@TijmenW](https://support.routexl.com/u/TijmenW)\
**Post date:** [November 29, 2023, 10:19am UTC](https://support.routexl.com/t/using-api-in-r-optimized-route/2674/3 "2023-11-29T10:19:41Z")

</div>

Thanks, I’ve tried it, but I receive an eror code 409.

edit: solved (see first comment)
