Calling Plumber APIs from R#
Requirements#
To call APIs hosted in RStudio Connect from R, you'll need:
- URL for the API endpoint hosted on RStudio Connect
- API key (if your API requires authorization)
R Example#
You can use the httr
package in R to call Plumber APIs from Shiny
applications, R Markdown reports, other Plumber APIs, or R scripts:
library(httr)
connectApiUrl <- "https://connect.yourcompany.com/rest-api/route"
connectApiKey <- "YfB5XBRB7slkkBSEi5qr93mWJvbpXQQy"
resp <- GET(connectApiUrl,
add_headers(Authorization = paste0("Key ", connectApiKey)))
result <- content(resp, "parsed")
print(result)
You can replace the values of connectApiUrl
and connectAPIKey
with your API
URL and API key from RStudio Connect.
Scope#
The code examples assume that you are calling a published API in RStudio Connect that is:
- Hosted on a secure server with TLS/SSL at an HTTPS endpoint
- Using an API key to make an authorized call to an API
- Making an HTTP GET request to the API
If your use case is different, then you can modify the example code accordingly.