# ============================================================================= # Examples of Using the Central Bank of Chile REST API (R) # ============================================================================= # This script shows how to directly use the REST API to query # series from the Central Bank's Statistical Database, building # and using URLs explicitly. # # Main functions: # - SearchSeries: Search series by frequency # - GetSeries: Get data for a specific series # ============================================================================= library("rjson") library(httr) # --- API Key Token (replace with your own) ------------------------------- token <- "your_token" # ============================================================================= # EXAMPLE 1: QUERY OBSERVED DOLLAR # ============================================================================= cat("\n=== EXAMPLE 1: QUERY OBSERVED DOLLAR ===\n") # Step 1: Search for the series cat("\nStep 1: Search for series related to 'observed dollar'\n") cat("Building full URL for SearchSeries with frequency=DAILY...\n") # Full URL to search daily series url_search_dollar <- paste0("https://si3.bcentral.cl/SieteRestWS/SieteRestWS.ashx?token=", token, "&function=SearchSeries&frequency=DAILY") cat("URL used:\n", url_search_dollar, "\n\n") # Run the query json_data_dolar <- rjson::fromJSON(file = url_search_dollar) # Process SeriesInfos from JSON daily_series <- as.data.frame(do.call(rbind, lapply(json_data_dolar$SeriesInfos, as.vector))) daily_series$spanishTitle <- as.character(daily_series$spanishTitle) # Filter series containing "dólar observado" idx_dolar <- grep("dólar observado", daily_series$spanishTitle, ignore.case = TRUE) dollar_series <- daily_series[idx_dolar, ] cat("Found", nrow(dollar_series), "series related to 'dólar observado'\n") cat("\nFirst matches:\n") print(head(dollar_series, 3)) # Step 2: Query the data cat("\nStep 2: Query data for observed dollar (last year)\n") cat("We will use the code: F073.TCO.PRE.Z.D (daily observed dollar)\n") cat("Building full URL for GetSeries...\n") # Full URL to retrieve series data url_dollar_data <- paste0("https://si3.bcentral.cl/SieteRestWS/SieteRestWS.ashx?token=", token, "&function=GetSeries×eries=F073.TCO.PRE.Z.D&firstdate=2024-09-01&lastdate=2025-09-30") cat("URL utilizada:\n", url_dollar_data, "\n\n") # Run the query json_data_serie <- rjson::fromJSON(file = url_dollar_data) # Process observations # We use Obs from the JSON to access the value of the observations observations <- as.data.frame(do.call(rbind, lapply(json_data_serie$Series$Obs, as.vector))) observations$value <- as.numeric(observations$value) observations$indexDateString <- as.character(observations$indexDateString) cat("Data obtained:", nrow(observations), "observations\n") cat("Period:", min(observations$indexDateString), "to", max(observations$indexDateString), "\n") cat("\nLast 10 observations:\n") print(tail(observations, 10)) # Step 3: Calculate basic statistics cat("\nStep 3: Basic statistics for observed dollar\n") valid_values <- observations$value[!is.na(observations$value)] cat("Minimum value: $", round(min(valid_values), 2), "\n") cat("Maximum value: $", round(max(valid_values), 2), "\n") cat("Average value: $", round(mean(valid_values), 2), "\n") cat("Last observation: $", round(tail(valid_values, 1), 2), "\n") # ============================================================================= # Example 2: GDP AND IMACEC WITH DIFFERENT FREQUENCIES # ============================================================================= cat("\n=== EXAMPLE 2: GDP AND IMACEC WITH DIFFERENT FREQUENCIES ===\n") cat("In this example, we will work with two economic indicators that have\n") cat("different frequencies: IMACEC (monthly) and GDP (quarterly).\n") # Step 1: Search for the series cat("\nStep 1: Search for the series\n") cat("First, we search for both series separately according to their frequency.\n") # Search for IMACEC (monthly) cat("\nSearching for IMACEC (frequency MONTHLY):\n") url_search_imacec <- paste0("https://si3.bcentral.cl/SieteRestWS/SieteRestWS.ashx?token=", token, "&function=SearchSeries&frequency=MONTHLY") json_data_imacec <- rjson::fromJSON(file = url_search_imacec) monthly_series <- as.data.frame(do.call(rbind, lapply(json_data_imacec$SeriesInfos, as.vector))) monthly_series$spanishTitle <- as.character(monthly_series$spanishTitle) idx_imacec <- grep("imacec empalmado", monthly_series$spanishTitle, ignore.case = TRUE) imacec_series <- monthly_series[idx_imacec, ] cat("Series found for IMACEC:", nrow(imacec_series), "\n") cat("\nFirst matches:\n") print(head(imacec_series, 5)) # Search for GDP (quarterly) cat("\nSearching for GDP (frequency QUARTERLY):\n") url_search_gdp <- paste0("https://si3.bcentral.cl/SieteRestWS/SieteRestWS.ashx?token=", token, "&function=SearchSeries&frequency=QUARTERLY") json_data_gdp <- rjson::fromJSON(file = url_search_gdp) quarterly_series <- as.data.frame(do.call(rbind, lapply(json_data_gdp$SeriesInfos, as.vector))) quarterly_series$spanishTitle <- as.character(quarterly_series$spanishTitle) idx_gdp <- grep("PIB, volumen a precios del año anterior encadenado", quarterly_series$spanishTitle, ignore.case = TRUE) series_gdp <- quarterly_series[idx_gdp, ] cat("Series found for GDP:", nrow(series_gdp), "\n") cat("\nLast matches:\n") print(tail(series_gdp, 10)) # Step 2: Query each series separately cat("\nStep 2: Query each series individually\n") cat("From the previous results, we will use:\n") cat("- IMACEC: F032.IMC.IND.Z.Z.EP18.Z.Z.0.M (monthly frequency)\n") cat("- GDP: F032.PIB.FLU.R.CLP.EP18.Z.Z.0.T (quarterly frequency)\n") # Query IMACEC cat("\nQuerying IMACEC (monthly):\n") url_imacec_data <- paste0("https://si3.bcentral.cl/SieteRestWS/SieteRestWS.ashx?token=", token, "&function=GetSeries×eries=F032.IMC.IND.Z.Z.EP18.Z.Z.0.M&firstdate=2024-01-01&lastdate=2025-09-30") json_imacec_data <- rjson::fromJSON(file = url_imacec_data) obs_imacec <- as.data.frame(do.call(rbind, lapply(json_imacec_data$Series$Obs, as.vector))) obs_imacec$value <- as.numeric(obs_imacec$value) obs_imacec$indexDateString <- as.character(obs_imacec$indexDateString) colnames(obs_imacec)[colnames(obs_imacec) == "value"] <- "IMACEC" cat("IMACEC:", nrow(obs_imacec), "monthly observations\n") print(tail(obs_imacec, 5)) # Query GDP cat("\nQuerying GDP (quarterly):\n") url_gdp_data <- paste0("https://si3.bcentral.cl/SieteRestWS/SieteRestWS.ashx?token=", token, "&function=GetSeries×eries=F032.PIB.FLU.R.CLP.EP18.Z.Z.0.T&firstdate=2024-01-01&lastdate=2025-09-30") json_gdp_data <- rjson::fromJSON(file = url_gdp_data) obs_gdp <- as.data.frame(do.call(rbind, lapply(json_gdp_data$Series$Obs, as.vector))) obs_gdp$value <- as.numeric(obs_gdp$value) obs_gdp$indexDateString <- as.character(obs_gdp$indexDateString) colnames(obs_gdp)[colnames(obs_gdp) == "value"] <- "GDP" cat("GDP:", nrow(obs_gdp), "quarterly observations\n") print(obs_gdp) # Step 3: Combine both series into a DataFrame cat("\nStep 3: Combine both series with different frequencies\n") cat("The REST API allows querying series with different frequencies.\n") cat("We create a combined DataFrame to show both series:\n") # Prepare dates for merging obs_imacec$fecha_date <- as.Date(obs_imacec$indexDateString, format = "%d-%m-%Y") obs_gdp$fecha_date <- as.Date(obs_gdp$indexDateString, format = "%d-%m-%Y") # Create a sequence of monthly dates for the entire period all_dates <- seq(from = as.Date("2024-01-01"), to = as.Date("2025-08-01"), by = "month") # Base data frame with all dates combined_data <- data.frame( fecha = format(all_dates, "%d-%m-%Y"), fecha_date = all_dates, stringsAsFactors = FALSE ) # Add IMACEC (monthly - will have values for all dates) combined_data <- merge(combined_data, obs_imacec[, c("fecha_date", "IMACEC")], by = "fecha_date", all.x = TRUE) # Add GDP (quarterly - only in January, April, July, October) combined_data <- merge(combined_data, obs_gdp[, c("fecha_date", "GDP")], by = "fecha_date", all.x = TRUE) # Select final columns combined_data <- combined_data[, c("fecha", "IMACEC", "GDP")] cat("Combined data (original frequency):\n") cat("IMACEC has monthly values, while GDP only has values\n") cat("quarterly (January, April, July, October). That's why NA appear in the\n") cat("intermediate dates for GDP.\n\n") print(head(combined_data, 8))