Parametrizing Custom Rest API Calls in Python

Parametrizing Custom Rest API calls in Rivery Python

In this article we will discuss how to properly create and parametrize requests made using python libraries in the Rivery Python module in a Logic River.

Note that Rivery Python is only available to Professional and Enterprise tier accounts.

Variables

Rivery Python modules support the saving importation and editing or various kinds of variables, these variables are:

Environment Variables - These are variables that are assigned to the rivery environment within the account, while they can be imported into python modules, they are “read-only” and cannot be modified from within the module.

Logic River Variables - These are variables that can be defined within a logic river and can be imported and edited in and out of python scripts.

Encrypted Variables - These are special types of logic river variables that can be securely encrypted. Note that once these variables are encrypted, they cannot be edited.

Multi-Valued Variables - These are a type of logic river variable that can hold multiple values (up to 13mb).

Connection Variables -

These are variables that can be created with a Custom REST API Connection and encrypted (if needed):

pasted image 0 (4)

Connection variables can only be used in Action Rivers.

Action Variables - These are variables that are used in API calls made with Action Rivers (single or multi).

Best Practices for parameterizing REST api calls in python.

To build api calls in python, we recommend using the python requests library, which comes preinstalled in every Rivery python module’s virtual environment.

The requests library is also integrated with popular api testing tools like postman which allow you to build api calls and translate them into code samples in python using certain libraries.

The standard syntax for importing variables into a python module is as follows:

To import environment variables:

from rivery_environment_variables import my_environment_variable1,my_environment_variable2,…

To import logic river variables (either encrypted, multi-value, or standard):

from rivery_variables import my_variable1, my_variable2, my_variable3,…

Here are some tips you can use to construct your api calls securely and maintainably when using Rivery python.

  1. Encrypt any variables tied to authentication parameters. - Most api’s use some form of authentication such as a client_id, client_secret, auth_token, or bearer token and encrypting this information in the logic river variable settings is required for a secure flow.

import requests

import json

from rivery_variables import access_token

url = “https://api.hubapi.com/crm/v3/objects/2-13920127?archived=false&limit=100

payload={}

headers = {

‘Content-Type’: ‘application/json’,

‘Authorization’: ‘Bearer {access_token}’

}

response = requests.request(“GET”, url, headers=headers, data=payload)

print(response.text)

  1. Ensure the formatting of a variable is what is expected by the api call, if it needs to be string, float or integer, make sure to explicitly typecast it in the python script after importation for use in the flow

EX: my_variable1 = int(str(my_variable1))

  1. If the API calls will dynamic, that is, if nested inside of a logic river loop where some parameters will take a different value based on iteration of the loop, parametrize those variables, like so:

  1. Include print() statements in code when you want to verify formatting of the variables and wherever else you may want to verify inputs/outputs of API calls.

pasted image 0 (8)

  1. Save the outputs of the api call to a pandas dataframe object (called my_rest_output, in this example), after which you can save to a rivery dataframe object using the command:

dataframe.save(my_rest_output)

With these helpful tips in mind, building and maintaining custom api calls using the Rivery python module should be a piece-of-cake!