Alphacast's Integrations: Interacting with the API on R


Introduction and prerequisites

Getting data from Alphacast with R is really easy. You need the Alphacast API Key and some common R packages. With a few simple steps, you can get entire dataframes, dataset indexes, repository names and repository content in any format, ready for processing and analysis. To work correctly with the Alphacast API from R we recommend installing and loading the following libraries:

install.packages(c("dplyr", "httr", "reshape2"))
library(dplyr)
library(httr)
library(reshape2)

To make your API workflow easier, we recommend creating an object named "alphacast_api_key" with your own key. This will make working with your Alphacast credentials faster. Remember that you can get the API credentials from the Alphacast Settings menu. For example:

alphacast_api_key <- "YOUR_API KEY"

Getting all available datasets in Alphacast

Before starting to work with the API, you may find it useful to have an index with all the datasets available on the platform with your user level. With a few lines of code to achieve this is possible in a very simple way by using the mentioned libraries. First, we indicate to R the link to the Alphacast website that will bring the index in JSON format. Authentication will be completed with the authenticate() function. It should be remembered that the Alphacast API does not need a user and password but works with a single API Key.

datasets <- GET("https://api.alphacast.io/datasets", 
authenticate(user = alphacast_api_key, password = "")) 

To clean up the dataset and make it useful, use the bind_rows() command from the dplyr package in conjunction with the content() function from the httr package to get the response in dataframe format.

datasets <-  bind_rows(content(datasets))[ ,-5]
head(datasets)
idnamedatabase
5208High Frequency CPI - Argentina - Wide - WeeklyAlphacast Basics: Argentina High Frequency CPI
5225High Frequency CPI - Argentina - WeeklyAlphacast Basics: Argentina High Frequency CPI
5226High Frequency CPI - Argentina - SEIDO vs INDEC - WeeklyAlphacast Basics: Argentina High Frequency CPI
5231Public Opinion - Latin AmericaSEIDO: Latin American Public Opinion
5236Public Opinion - ArgentinaSEIDO: Latin American Public Opinion
5241Public Opinion - Argentina - COVID-19SEIDO: Latin American Public Opinion

Getting dataframes from Alphacast

To obtain a dataframe it is necessary to call the GET function (from the HTTR library) with the number of dataset you want and your API key. For example, if you want to get the data from dataset 6659 (Apple Mobility Report):

dataset_id <- 6659
apple_mob <- GET(paste("https://api.alphacast.io/datasets/", 
dataset_id,".csv", sep=""), authenticate(user = alphacast_api_key, password = "")) 
apple_mob <- readr::read_csv(content(apple_mob, as ="text"), guess_max = 100000)
head(apple_mob)
EntityYeardrivingwalkingdriving - 7d_running_avwalking - 7d_running_avtransittransit - 7d_running_av
Albania2020-01-13100.00100.00NANANANA
Albania2020-01-1495.30100.68NANANANA
Albania2020-01-15101.4398.93NANANANA
Albania2020-01-1697.2098.46NANANANA
Albania2020-01-17103.55100.85NANANANA
Albania2020-01-18112.67100.13NANANANA

The previous code allows to save the dataframe of the "Apple Mobility Report" in the object "apple_mob". From here, you can do whatever you want with it: graph, analyze, export as csv or JSON, among other things. It is also easy to transform the dataframe to LONG format using the reshape2 package, since all Alphacast datasets contain the "Year" and "Entity" columns.

apple_mob_long <- melt(apple_mob, id.vars = c("Entity", "Year"))

Getting repositories and its datasets

You can get all available repositories from Alphacast with your level of access.

repos <- GET("https://api.alphacast.io/repositories",
authenticate(user = alphacast_api_key, password = ""))
repos <- bind_rows(content(repos))

You can also access the index of the datasets of a given repo. In this case, you can get all the datasets from the repo "Argentina's daily financial data" through the following functions:

repo_id <- 21
repos_datasets <- GET("https://api.alphacast.io/datasets", query = list(repo_id = repo_id), 
authenticate(user = alphacast_api_key, password = ""))
repos_datasets <- bind_rows(content(repos_datasets))
head(repos_datasets)
idnamecreatedAtupdatedAtrepositoryId
5266Base FCI - Renta Variable2020-10-22T22:38:212020-10-22T22:38:2121
5273Base FCI - Renta Fija2020-10-27T16:43:042020-10-27T16:43:0421
5288Financial - Argentina - FX premiums - Daily2020-11-01T17:32:022020-11-01T17:32:0221
5289Financial - Argentina - FX premiums - Daily_Long2020-11-01T17:33:032020-11-01T17:33:0321
5341Financial - Argentina - Sovereign Bonds2020-11-12T12:30:032020-11-12T12:30:0321
5357Financial - Argentina - Sovereign Bonds - Last Price - Daily2020-11-19T16:30:032020-11-19T16:30:0321

Creating repositories in Alphacast

You can create your own repository to later upload the dataset. First, you have to set some variables in your R Environment.

url <- "https://api.alphacast.io/repositories"`

form <- list(
  "name" = "Repo's Name",
  "description" = "Test Repo - description",
  "privacy" = "Private",
  "slug" = "test-rrr-repo")

And then, you post in the Alphacast server through the function POST.

r <- POST(url = url, body = form, config = authenticate(user = alphacast_api_key, password = ""))

content(r)
idnamedescriptionprivacyslug
610Repo's NameTest Repo - descriptionPrivatetest-rrr-repo

In this way, the "610" repo is created and can be checked from your admin on the Alphacast web.

Uploading data to your repo

Once the repo is created, it is necessary to create the slot for the dataset that you want to upload. The system will automatically generate the id of the dataset.

url <- "https://api.alphacast.io/datasets"
form <- list(
    "name" = "test_datasets",
    "repositoryId" = 610)`
r <- POST(url = url, body = form, config = authenticate(user = alphacast_api_key, password = ""))
content(r)

$id 6822

In this example, id number 6822 was assigned for the dataset. The next thing is to create the PUT function to upload the CSV to Alphacast and make it appear in the repo.

dataset_id <- 6822
url <- paste("https://api.alphacast.io/datasets/", dataset_id, 
"/data?deleteMissingFromDB=True&onConflictUpdateDB=True", sep = "")

Finally, you can upload the dataset in CSV format, with columns named Entity (for countries) and Year (for dates, in YYYY-MM-DD format). In this case, the "tcn.csv" file is uploaded from the indicated path (located in the root folder of the R project).

r <- PUT(url, body = list(data= upload_file("tcn.csv")), 
config = authenticate(user = alphacast_api_key, 
password = ""))
content(r)
idstatuscreatedAtdatasetId
614Requested2021-07-26T20:59:21.4941346822

And in this way the file is uploaded to its own repository, generating the possibility of sharing it, transforming it or graphing it.


Previous

Next

Related insights