Title: | Easily Send Rewards and Incentives with 'Tremendous' from R |
---|---|
Description: | A slightly-opinionated R interface for the 'Tremendous' API (<https://www.tremendous.com/>). In addition to supporting GET and POST requests, 'tremendousr' has, dare I say, tremendously intuitive functions for sending digital rewards and incentives directly from R. |
Authors: | Jonathan Trattner [aut, cre] , Scott Chamberlain [ctb] (Utility functions `err_catcher()` and `trem_ua()` from chimpr package.), R Computing Lab at Wake Forest University [fnd] |
Maintainer: | Jonathan Trattner <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0.9000 |
Built: | 2024-11-05 03:41:54 UTC |
Source: | https://github.com/jdtrat/tremendousr |
Create a new Tremendous API Client
trem_client_new(api_key = NULL, sandbox = TRUE)
trem_client_new(api_key = NULL, sandbox = TRUE)
api_key |
API key from
tremendous.com. Can either pass in
here as a character string or set for repeated use with
|
sandbox |
Logical: |
An object of class 'tremClient' that contains the API Key and Environment information for easily performing Tremendous API requests.
## Not run: # Create a client for testing API calls within the Sandbox environment. test_client <- trem_client_new(api_key = "TEST_YOUR-KEY-HERE", sandbox = TRUE) # Create a client for performing API calls within a production environment. # This client will be able to send actual money! prod_client <- trem_client_new(api_key = "PROD_YOUR-KEY-HERE", sandbox = FALSE) ## End(Not run)
## Not run: # Create a client for testing API calls within the Sandbox environment. test_client <- trem_client_new(api_key = "TEST_YOUR-KEY-HERE", sandbox = TRUE) # Create a client for performing API calls within a production environment. # This client will be able to send actual money! prod_client <- trem_client_new(api_key = "PROD_YOUR-KEY-HERE", sandbox = FALSE) ## End(Not run)
Tremendous only supports DELETE requests for one endpoint – deleting an invoice. Per their documentation, this request "removes an invoice. This has no further consequences but is a rather cosmetic operation." See the examples for a walk-through.
trem_delete( client, path, query = list(), body = NULL, disk = NULL, stream = NULL, encode = "json", parse = TRUE )
trem_delete( client, path, query = list(), body = NULL, disk = NULL, stream = NULL, encode = "json", parse = TRUE )
client |
A Tremendous API Client object, created with
|
path |
The URL path, appended to the base URL, for GET requests such as listing available payment types, funding sources, account members, and more. See the Tremendous API Documentation for examples. |
query |
Query terms as a named list. See crul::HttpClient for more details. |
body |
Request body for Tremendous API, as an R List. |
disk |
A path to write to. |
stream |
An R function to determine how to stream data. |
encode |
"json" by default based on Tremendous API Request format. See crul::HttpClient for more options. |
parse |
Logical: Should the API Response results be parsed into a data frame? |
If parse = TRUE
(default), a list containing the response from the
API request. Otherwise, the R6 HttpResponse object containing API request
data.
## Not run: # Create a new Tremendous API Client test_client <- trem_client_new(api_key = "TEST_YOUR-API-KEY-HERE", sandbox = TRUE) # Perform a POST request for an invoice. # `po_number` is Reference to the purchase order number within your organization # `amount` is in USD trem_post(test_client, path = "invoices", body = list(po_number = "unique-invoice-id", amount = 50) ) # Perform a GET request for listing all current (non-deleted) invoices. current_invoices <- trem_get(test_client, "invoices") # Get index for the correct ID unique_id_index <- which(current_invoices$invoices$po_number == "unique-invoice-id") # Get the invoice ID for 'unique-invoice-id' to delete my_invoice_id <- current_invoices$invoices[unique_id_index, "id"] # Perform a DELETE request for the specific invoice. trem_delete(test_client, paste0("invoices/", my_invoice_id)) # Perform a GET request for listing all current (non-deleted) invoices. # The one with id po_number 'unique-invoice-id' should no longer be here. trem_get(test_client, "invoices") ## End(Not run)
## Not run: # Create a new Tremendous API Client test_client <- trem_client_new(api_key = "TEST_YOUR-API-KEY-HERE", sandbox = TRUE) # Perform a POST request for an invoice. # `po_number` is Reference to the purchase order number within your organization # `amount` is in USD trem_post(test_client, path = "invoices", body = list(po_number = "unique-invoice-id", amount = 50) ) # Perform a GET request for listing all current (non-deleted) invoices. current_invoices <- trem_get(test_client, "invoices") # Get index for the correct ID unique_id_index <- which(current_invoices$invoices$po_number == "unique-invoice-id") # Get the invoice ID for 'unique-invoice-id' to delete my_invoice_id <- current_invoices$invoices[unique_id_index, "id"] # Perform a DELETE request for the specific invoice. trem_delete(test_client, paste0("invoices/", my_invoice_id)) # Perform a GET request for listing all current (non-deleted) invoices. # The one with id po_number 'unique-invoice-id' should no longer be here. trem_get(test_client, "invoices") ## End(Not run)
This function provides lower-level access to perform GET requests via Tremendous API. Available endpoints can be found on the official Tremendous API documentation.
trem_get( client, path, query = list(), disk = NULL, stream = NULL, parse = TRUE )
trem_get( client, path, query = list(), disk = NULL, stream = NULL, parse = TRUE )
client |
A Tremendous API Client object, created with
|
path |
The URL path, appended to the base URL, for GET requests such as listing available payment types, funding sources, account members, and more. See the Tremendous API Documentation for examples. |
query |
Query terms as a named list. See crul::HttpClient for more details. |
disk |
A path to write to. |
stream |
An R function to determine how to stream data. |
parse |
Logical: Should the API Response results be parsed into a data frame? |
If parse = TRUE
(default), a list containing the
response from the API request. Otherwise, the R6 HttpResponse object
containing API request data.
## Not run: # Create a new Tremendous API Client test_client <- trem_client_new(api_key = "TEST_YOUR-API-KEY-HERE", sandbox = TRUE) # Sandbox environment so no actual money is sent # Perform a GET request to list funding sources available in your Tremendous # Account. Documentation: # https://developers.tremendous.com/reference/core-funding-source-index trem_get(trem_client, "funding_sources") # Perform a GET request to list all invoices on your Tremendous Account. # Documentation: # https://developers.tremendous.com/reference/core-invoices-index trem_get(trem_client, "invoices") # Perform a GET request to list all orders (payment history) on your Tremendous # Account. Documentation: # https://developers.tremendous.com/reference/core-orders-index trem_get(trem_client, "orders") # Perform a GET request to list a specific order's information (payment history) # from your Tremendous Account. Documentation: # https://developers.tremendous.com/reference/core-orders-show trem_get(trem_client, "orders/YOUR-ORDER-ID") ## End(Not run)
## Not run: # Create a new Tremendous API Client test_client <- trem_client_new(api_key = "TEST_YOUR-API-KEY-HERE", sandbox = TRUE) # Sandbox environment so no actual money is sent # Perform a GET request to list funding sources available in your Tremendous # Account. Documentation: # https://developers.tremendous.com/reference/core-funding-source-index trem_get(trem_client, "funding_sources") # Perform a GET request to list all invoices on your Tremendous Account. # Documentation: # https://developers.tremendous.com/reference/core-invoices-index trem_get(trem_client, "invoices") # Perform a GET request to list all orders (payment history) on your Tremendous # Account. Documentation: # https://developers.tremendous.com/reference/core-orders-index trem_get(trem_client, "orders") # Perform a GET request to list a specific order's information (payment history) # from your Tremendous Account. Documentation: # https://developers.tremendous.com/reference/core-orders-show trem_get(trem_client, "orders/YOUR-ORDER-ID") ## End(Not run)
This function provides lower-level access to perform POST requests via Tremendous API. Available endpoints can be found on the official Tremendous API documentation.
For sending payments, I would recommend using trem_send_reward
as
it's more intuitive to use. However, this can be done using the
trem_post()
function (see examples).
trem_post( client, path, query = list(), body = NULL, disk = NULL, stream = NULL, encode = "json", parse = TRUE )
trem_post( client, path, query = list(), body = NULL, disk = NULL, stream = NULL, encode = "json", parse = TRUE )
client |
A Tremendous API Client object, created with
|
path |
The URL path, appended to the base URL, for GET requests such as listing available payment types, funding sources, account members, and more. See the Tremendous API Documentation for examples. |
query |
Query terms as a named list. See crul::HttpClient for more details. |
body |
Request body for Tremendous API, as an R List. |
disk |
A path to write to. |
stream |
An R function to determine how to stream data. |
encode |
"json" by default based on Tremendous API Request format. See crul::HttpClient for more options. |
parse |
Logical: Should the API Response results be parsed into a data frame? |
If parse = TRUE
(default), a list containing the response from the
API request. Otherwise, the R6 HttpResponse object containing API request
data.
## Not run: # Create a new Tremendous API Client test_client <- trem_client_new(api_key = "TEST_YOUR-API-KEY-HERE", sandbox = TRUE) # Perform a POST request to invite new members to your Tremendous Account. # Documentation: https://developers.tremendous.com/reference/post_members trem_post(trem_client, path = "members", body = list(email = "[email protected]", name = "Example Person", role = "MEMBER")) # Perform a POST send payments -- I find it ~tremendously~ easier to use the `trem_send_reward()` function. # Documentation: https://developers.tremendous.com/reference/core-orders-create trem_post(trem_client, path = "orders", body = list( external_id = "manual-payment-post", # This is a payment description id payment = list( funding_source_id = "your-funding-id-from-tremendous" ), rewards = list( value = list( denomination = 10, currency_code = "USD" ), delivery = list( method = "EMAIL" # "EMAIL", "LINK", or "PHONE", ), recipient = list( name = "first last", email = "[email protected]" ), # IDs for Applebee's Gift Card and Amazon Gift Card products = c("2JFKPXBWDC1K", "VW9JLMPRL9N7") ) )) ## End(Not run)
## Not run: # Create a new Tremendous API Client test_client <- trem_client_new(api_key = "TEST_YOUR-API-KEY-HERE", sandbox = TRUE) # Perform a POST request to invite new members to your Tremendous Account. # Documentation: https://developers.tremendous.com/reference/post_members trem_post(trem_client, path = "members", body = list(email = "[email protected]", name = "Example Person", role = "MEMBER")) # Perform a POST send payments -- I find it ~tremendously~ easier to use the `trem_send_reward()` function. # Documentation: https://developers.tremendous.com/reference/core-orders-create trem_post(trem_client, path = "orders", body = list( external_id = "manual-payment-post", # This is a payment description id payment = list( funding_source_id = "your-funding-id-from-tremendous" ), rewards = list( value = list( denomination = 10, currency_code = "USD" ), delivery = list( method = "EMAIL" # "EMAIL", "LINK", or "PHONE", ), recipient = list( name = "first last", email = "[email protected]" ), # IDs for Applebee's Gift Card and Amazon Gift Card products = c("2JFKPXBWDC1K", "VW9JLMPRL9N7") ) )) ## End(Not run)
The most likely reason to use the tremendousr package is to send rewards
This function, trem_send_reward()
, provides an easy interface to do so. See the
examples for more details.
trem_send_reward( client, name, email = NULL, phone = NULL, reward_amount, currency_code = "USD", delivery_method = "EMAIL", payment_description_id, funding_source_id, reward_types, parse = TRUE )
trem_send_reward( client, name, email = NULL, phone = NULL, reward_amount, currency_code = "USD", delivery_method = "EMAIL", payment_description_id, funding_source_id, reward_types, parse = TRUE )
client |
A Tremendous API Client object, created with
|
name |
Name of the recipient. |
email |
Email address of the recipient. |
phone |
Phone number of the recipient (US phone numbers only). |
reward_amount |
Amount of the reward (numeric). |
currency_code |
Currency of the reward (default to "USD"). |
delivery_method |
Default to "EMAIL", for sending the reward to the recipient via email. Alternatively, reward can be delivered via a link ("LINK") or text message ("PHONE"). |
payment_description_id |
Unique ID for specific order. This will appear
as |
funding_source_id |
ID of the funding source linked to your account, to
draw funds from for this order. One of the IDs from
|
reward_types |
A character vector of product ids – reward options – for the recipient to choose from. Available options can be found here. |
parse |
Logical: Should the API Response results be parsed into a data frame? |
If parse = TRUE
(default), a list containing the response
from payment API request. Otherwise, the R6 HttpResponse object containing
API request data.
## Not run: # Create a Tremendous Client test_client <- trem_client_new(api_key = "TEST_YOUR-KEY-HERE", sandbox = TRUE) # Sandbox environment so no actual money is sent # To send a payment, you can simply pass in the client # and specify the necessary fields. payment1 <- trem_send_reward(client = test_client, name = "first last", email = "[email protected]", reward_amount = 10, currency_code = "USD", delivery_method = "EMAIL", payment_description_id = "payment-from-tremendousr-examples", funding_source_id = "your-funding-id-from-tremendous", reward_types = "Q24BD9EZ332JT", # ID for virtual visa gift card parse = TRUE # Return a parsed API response ) ## End(Not run)
## Not run: # Create a Tremendous Client test_client <- trem_client_new(api_key = "TEST_YOUR-KEY-HERE", sandbox = TRUE) # Sandbox environment so no actual money is sent # To send a payment, you can simply pass in the client # and specify the necessary fields. payment1 <- trem_send_reward(client = test_client, name = "first last", email = "[email protected]", reward_amount = 10, currency_code = "USD", delivery_method = "EMAIL", payment_description_id = "payment-from-tremendousr-examples", funding_source_id = "your-funding-id-from-tremendous", reward_types = "Q24BD9EZ332JT", # ID for virtual visa gift card parse = TRUE # Return a parsed API response ) ## End(Not run)
Set Tremendous API Key in .Renviron
trem_set_api_key(api_key)
trem_set_api_key(api_key)
api_key |
API key from tremendous.com. |
NA; Used to set the Tremendous API key for persistent use in '.Renviron'.
if (interactive()) { # For Sandbox Environment, your API key should look like: trem_set_api_key("TEST_YOUR-API-KEY") # For Production Environment, your API key should look like: trem_set_api_key("PROD_YOUR-API-KEY") }
if (interactive()) { # For Sandbox Environment, your API key should look like: trem_set_api_key("TEST_YOUR-API-KEY") # For Production Environment, your API key should look like: trem_set_api_key("PROD_YOUR-API-KEY") }
Exported for use with trem_client_new
to provide useragent info
as a curl option for the API Request. This function was Adapted from
chimpr.
trem_ua()
trem_ua()
useragent info for tremendousr API Package
# Get useragent info for the tremendous API package trem_ua()
# Get useragent info for the tremendous API package trem_ua()