Skip to content

Recharge API Integration

What It Is

Recharge is our subscription platform — the source of truth for who subscribed, when, for how long, and at what frequency. The Recharge API gives us access to real-time customer data, subscription behavior, and retention outcomes.
In R, we query this data using the httr and jsonlite packages.

Why It Matters

We train neural networks on historical data. Recharge provides that data — especially labels like:
Which customers subscribed
How long they stayed
When they churned
What they purchased and how often

Without Recharge, we can’t learn from actual subscription behavior.

Step-by-Step Setup

✅ 1. Get Your Recharge API Key

Login to
Go to Apps & IntegrationsAPI tokens
Create a Read Access Token
Copy the token (starts with sk_...)
or
Ask Aaron for the Token

✅ 2. Set Up Helper Functions in R

Run this code in R
library(httr)
library(jsonlite)

base_url <- "https://api.rechargeapps.com"
access_token <- "" # Replace this

# Helper: Generic GET with headers
recharge_get <- function(endpoint, query = list()) {
url <- paste0(base_url, endpoint)
res <- GET(url,
add_headers("X-Recharge-Access-Token" = access_token),
query = query)
cat("Status:", status_code(res), "\n")
if (status_code(res) != 200) {
print(content(res, "text", encoding = "UTF-8"))
stop("❌ API request failed.")
}
content(res, "parsed", simplifyDataFrame = TRUE)
}

✅ 3. Pull Data From Recharge

# Get Customers
customers_df <- recharge_get("/customers", query = list(limit = 250))$customers

# Get Subscriptions (paginated)
get_recharge_data <- function(endpoint, limit = 250) {
page <- 1
all_data <- list()
repeat {
cat("Fetching page:", page, "\n")
res <- GET(
url = paste0(base_url, "/", endpoint),
query = list(page = page, limit = limit),
add_headers("X-Recharge-Access-Token" = access_token)
)
if (status_code(res) != 200) {
print(content(res, "text", encoding = "UTF-8"))
stop("❌ Failed to retrieve data from Recharge.")
}
parsed <- content(res, "parsed", simplifyDataFrame = TRUE)
items <- parsed[[endpoint]]
if (length(items) == 0) break
all_data[[page]] <- items
page <- page + 1
}
return(dplyr::bind_rows(all_data))
}

subs_df <- get_recharge_data("subscriptions")

🧠 Notes for Interns

Date handling: Use lubridate::ymd_hms() to convert created_at and cancelled_at columns to timestamps. ​To convert date strings (like created_at) into timestamps R can understand, use:
library(lubridate)

# Convert to datetime format
customers_df$created_at <- ymd_hms(customers_df$created_at)
subs_df$created_at <- ymd_hms(subs_df$created_at)
subs_df$cancelled_at <- ymd_hms(subs_df$cancelled_at)
This lets us calculate time between events — like how long someone stayed subscribed.

Avoid API rate limits: Never call inside a for loop without a delay.
Recharge may block you if you send too many requests too fast.
If you ever need to loop through a list of API calls, always add a short pause:
for (page in 1:5) {
Sys.sleep(1) # Pause 1 second between calls
response <- recharge_get("/subscriptions", query = list(page = page))
print(paste("Page", page, "fetched."))
}

You’ll use this to create training labels: subscribed_within_30_days, churned_within_90_days, etc.
We’ll eventually train models using a label like: Did this customer subscribe within 30 days?
Here’s how to create that kind of logic using Recharge’s data:
# Join customer and subscription data by customer_id
library(dplyr)

merged_df <- customers_df %>%
inner_join(subs_df, by = c("id" = "customer_id"))

# Convert dates if not already done
merged_df$created_at.x <- ymd_hms(merged_df$created_at.x) # from customers
merged_df$created_at.y <- ymd_hms(merged_df$created_at.y) # from subscriptions

# Create a new label column
merged_df$subscribed_within_30_days <- ifelse(
difftime(merged_df$created_at.y, merged_df$created_at.x, units = "days") <= 30,
1, 0
)
This label (1 for yes, 0 for no) will later be used as our target variable when training the model.

Load content from share.geckoboard.com?
Loading external content may reveal information to 3rd parties. Learn more
Allow
Load content from share.geckoboard.com?
Loading external content may reveal information to 3rd parties. Learn more
Allow
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.