Skip to content

Python Example

Below are some samples of how to call the BTQ API using the requests library in Python. Please review the Overview for general information about the code samples provided.

Objects

For these samples, we will be using the JSON parser built in to the requests library. This library will return a dictionary when it deserializes the JSON responses we receive.

Functions

Below are a series of functions that reach out to the BTQ API to perform the actions we want to do. Note that we are making use of the requests library to make the calls and to parse the responses.

Login

The following method will perform a log in to the BTQ API using a provided username and password.

def login(email, password):
    login_uri = "https://arc-aegis.billtrust.com/authentication/v1/login"
    login_headers = {"Accept":"application/json", "Content-Type":"application/json"}
    login_request = {"email":email, "password":password}
    login_response = requests.post(login_uri, json=login_request, headers=login_headers)
    login_response.raise_for_status()
    login_resp_json = login_response.json()
    return login_resp_json["accessToken"]

Get Users For Tenant

The following method will query the list of users for a specific tenant ID and return the list.

def get_users_for_tenant(access_token, tenant_id):
    users_uri = f"https://arc-aegis.billtrust.com/user/v1/tenants/{tenant_id}/users"
    users_headers = {"Accept":"application/json", "X-Billtrust-Auth":access_token}
    users_response = requests.get(users_uri, headers=users_headers)
    users_response.raise_for_status()
    return users_response.json()

Logout

The following method will perform a log out for the provided access token.

def logout(access_token):
    logout_uri = "https://arc-aegis.billtrust.com/authentication/v1/logout"
    logout_headers = {"Accept":"application/json", "Content-Type":"application/json"}
    logout_request = {"accessToken":access_token}
    logout_response = requests.post(logout_uri, json=logout_request, headers=logout_headers)
    logout_response.raise_for_status()
    return

Complete Program

Below is a combination of the sample code above into a single runnable sample console application. This code can be run using Python 3.6 and later. It does require the requests library to be installed in order to run.

import requests
import sys

def login(email, password):
    login_uri = "https://arc-aegis.billtrust.com/authentication/v1/login"
    login_headers = {"Accept":"application/json", "Content-Type":"application/json"}
    login_request = {"email":email, "password":password}
    login_response = requests.post(login_uri, json=login_request, headers=login_headers)
    login_response.raise_for_status()
    login_resp_json = login_response.json()
    return login_resp_json["accessToken"]

def get_users_for_tenant(access_token, tenant_id):
    users_uri = f"https://arc-aegis.billtrust.com/user/v1/tenants/{tenant_id}/users"
    users_headers = {"Accept":"application/json", "X-Billtrust-Auth":access_token}
    users_response = requests.get(users_uri, headers=users_headers)
    users_response.raise_for_status()
    return users_response.json()

def logout(access_token):
    logout_uri = "https://arc-aegis.billtrust.com/authentication/v1/logout"
    logout_headers = {"Accept":"application/json", "Content-Type":"application/json"}
    logout_request = {'accessToken':access_token}
    logout_response = requests.post(logout_uri, json=logout_request, headers=logout_headers)
    logout_response.raise_for_status()
    return

if len(sys.argv) != 4:
    print("Expected usage: python BtqApiSample.py <tenantId> <email> <password>")
    sys.exit()

try:
    tenant_id = sys.argv[1]
    email = sys.argv[2]
    password = sys.argv[3]

    access_token = login(email, password)

    users_list = get_users_for_tenant(access_token, tenant_id)
    print(f"{len(users_list)} users have access to tenant {tenant_id}:")
    for user in users_list:
        first_name = user["firstName"]
        last_name = user["lastName"]
        print(f"{first_name} {last_name}")

    logout(access_token)

except Exception as error:
    print(error)

sys.exit()