Community perspective: Using Anaplan APIs with R

R offers different ways to consume the REST APIs. This article aims to provide a foundation to use Anaplan APIs with R. This article assumes you have installed R or RStudio IDE. To work with APIs in R, we need to install and load R packages. These packages contain functions that facilitate HTTP requests, manipulate and read responses in R. The essential among them are httr and jsonlite.

  1. httr: This package lets R act as the HTTP client to access APIs via GET/PUT/POST/DELETE methods. It has functions to decode the response() object and view useful information such as status code, content, date, time, cookies etc.

  2. jsonlite: This package offers simple, flexible tools for working with JSON data. It converts JSON data to/from R objects making the response readable in R.

Note: Other packages may be used for different scenarios, but the above ones are sufficient for the scope of this article.

Install and load the packages

The code below installs the packages, and loads them:

#Install package----
install.packages("httr", "jsonlite")

#Load package----
library(httr)
library(jsonlite)

Authentication and token extraction

Authentication can be either Basic or Certificate Authentication. This article will focus on how to do Basic Authentication. Once Authenticated, you may want to store the tokenValue for further use.

#Authenticate----
url<- "https://auth.anaplan.com/token/authenticate"
user<- "myemail"
pass<- "mypassword"
Auth<- POST(url, config = authenticate(user, pass, type = "basic"))
                                  
#Use content() to retreive data sent back from the server----
id<- content(Auth, "parsed")

#Extract and store Token value----
token<- id$tokenInfo$tokenValue

Get list of workspaces

GET method retrieves the list of Workspaces.

#Request Workspaces----
WS_list<- GET("https://api.anaplan.com/2/0/workspaces?tenantDetails=true", add_headers("Authorization" = paste("AnaplanAuthToken", token)))

#convert the response object to R list object----
fromJSON(content(WS_list,"text"))

The sample response format in R is shown below:

$meta
$meta$schema
[1] "https://api.anaplan.com/2/0/objects/workspace"

$meta$paging
$meta$paging$currentPageSize
[1] 2

$meta$paging$totalSize
[1] 2

$meta$paging$offset
[1] 0



$status
$status$code
[1] 200

$status$message
[1] "Success"


$workspaces
                                id                       name active sizeAllowance currentSize
1 8a868cd9794dc7k4179aaf6f6981cbc Partner Workspace   TRUE   53687091200 48748876652
2 8a868cdd80f15e9k381073095d86c3c       Training WS   TRUE   10737418240  1872078829

Get models list

Model list can also be obtained in a similar manner. 

Model_list<- GET("https://api.anaplan.com/2/0/models", add_headers("Authorization" = paste("AnaplanAuthToken", token)))
                 
fromJSON(content(Model_list, "text"))

Retrieve all line item metadata in a model

GET method to retrieve all line item metadata

lineitem_meta<- GET("https://api.anaplan.com/2/0/models/{modelId}/lineItems?includeAll=true", add_headers("Authorization" = paste("AnaplanAuthToken", token)),"Accept" = "application/json")
                   
fromJSON(content(lineitem_meta, "text"))

Get files/imports/exports/processes

Replace "files" in the below code with "imports" or "exports" or "processes" to retrieve the relevant information.

get_id<- GET("https://api.anaplan.com/2/0/models/{modelId}/files", 
             add_headers("Authorization" = paste("AnaplanAuthToken", token)))

fromJSON(content(get_id,"text"))

Upload file as a single chunk

PUT method to be used to upload file. Default response code should be 204.

fileupload<- upload_file("")
PUT("https://api.anaplan.com/2/0/workspaces/{workspaceId}/models/{modelId}/files/{fileId}",body = fileupload,  add_headers("Authorization" = paste("AnaplanAuthToken", token), "Content-Type" = "application/octet-stream"))

Run an import/export/process

Replace "imports" with "exports", "processes" in the below code, to run the relevant type of action.

POST("https://api.anaplan.com/2/0/workspaces/{workspaceId}/models/{modelId}/imports/{importId}/tasks", add_headers("Authorization" = paste("AnaplanAuthToken", token), "Content-Type" = "application/json"), encode = "json", body = list("localeName" = "en_US"))

Monitor import/export/process task status

GET("https://api.anaplan.com/2/0/workspaces/{workspaceId}/models/{modelId}/imports/{importId}/tasks/{taskId}",add_headers("Authorization" = paste("AnaplanAuthToken", token), "Content-Type" = "application/json"))

Get chunks in a file

file_chunks<- GET("https://api.anaplan.com/2/0/workspaces/{workspaceId}/models/{modelId}/files/{fileId}/chunks", add_headers("Authorization" = paste("AnaplanAuthToken", token),  "Content-Type" = "application/json"))
              
fromJSON(content(file_chunks,"text"))

Get data in chunks

get_datachunk<- GET("https://api.anaplan.com/2/0/workspaces/{workspaceId}/models/{modelId}/files/{fileId}/chunks/{chunkId}", add_headers("Authorization" = paste("AnaplanAuthToken", token), "Content-Type" = "application/json"))

fromJSON(readLines(content(get_datachunk, "text")))

Delete files

DELETE method to delete either uploaded or exported file.

DELETE("https://api.anaplan.com/2/0/workspaces/{workspaceId}/models/{modelId}/files/{fileId}",add_headers("Authorization" = paste("AnaplanAuthToken", token),  "Content-Type" = "application/json"))

Conclusion

The rest of the API endpoints can be called in a similar manner. R provides useful ways to do statistical analysis, and I hope this article provided a foundation to use the Anaplan APIs with R.

Comments