More complex APIs: Multiple endpoints with plumberΒΆ

To specify multiple end-points in your plumber API, you simply need to decorate multiple functions with routing comments. An example from the plumber documentation is the following, which allows you to GET an estimate of the mean of the normal distribution given a sample size, and POST two numbers (a and b) and receive back the sum.

The source code for this API is:

# myapi.R

#* @get /mean
normalMean <- function(samples=10){
  data <- rnorm(samples)
  mean(data)
}

#* @post /sum
addTwo <- function(a, b){
  as.numeric(a) + as.numeric(b)
}

Once you have configured your API in SherlockML (see plumber_api), you can query your API from the command line of your development server:

curl localhost:8000/mean

We can pass parameters in our API call, as well:

curl "localhost/mean?samples=10000"

And we can query our second endpoint:

curl \
  -H "Content-Type: application/json" \
  --data '{"b": 3, "a": 5}' \
  localhost:8000/sum