If you have been following along with this two-part series, then you are getting very close to making your very first SAS Viya API call. In Part 1 of this guide series, we discussed how to Register a Client, which provides us with several important pieces of information that we need prior to making an API call. Now, this guide will walk you through making your first API call to the SAS Viya REST API from with Python.
Pre-requisites:
The only required python library needed for this code is the requests library
import requests
Assumption:This guide assumes the python developer has appropriate credentials to access to the SAS Viya platform i.e. username and password. These credentials are the ones that are used to access, for example SAS Visual Analytics, and are typically managed with some combination of Active Directory and the SAS Environment Manager.
To get going, we are going to need a few bits of information from the prep-work we did in Part 1 of this guide, specifically the Application Name and the Secret (password). The username and password needed for this API call is one that the developer would use to login to the SAS Viya platform, for example SAS Visual Analytics.
After running the code below, we are going to get an access token, also called a Bearer Token. This token is valid for 12 hours assuming the validity was set as such in Part 1 during the registration. After 12 hours, this token will expire and a new one will be required. This is NOT to say that you have re-register again every 12 hours. In essence, you can treat the function below as a log in that has an expiration. The token itself is what assures the API that we have logged in and we are authorized to make an API call.
hostname = “example.com”
def obtainAccessTokenPythonUser(appName, secret, username, password):
url = "https://"+ str(hostname) +"/SASLogon/oauth/token"
headers = {"Content-Type":"application/x-www-form-urlencoded"}
data = "grant_type=password&username=" + str(username)+ "&password=" + str(password)
auth = (str(appName), str(secret))
r = requests.post(url,headers=headers, data=data, timeout=30, auth = auth, verify=False)
print(r)
print(r.json())
if r.status_code == 200:
return r.json()['access_token']
Sample Output:
Now lets make our first API call ! As part of the this call, the access token (Bearer Token) obtained in the previous step needs to be passed in to this request as part of the headers of the request.
The output of many of the API calls is going to be a JSON string containing the response of your request.
def samplePythonReuqest(pythonAccessToken):
headers = {'Accept': 'application/vnd.sas.collection+json',
'Authorization': 'Bearer ' +str(pythonAccessToken),
'Accept-Item': 'application/vnd.sas.summary+json',
'Accept-Language': 'string'}
r = requests.get('https://' +str(hostname)+ '/reports/reports#reports',
params={}, headers=headers, verify=False)
print(r.json())
return r.json()
For the list of all available API calls, please reference the full API guide available at https://developer.sas.com/apis/rest/
Sample Output (JSON):
This example was created in a Viya 3.4. All code in this article is intended to be submitted in Python 3.6. Client registration, as show in Part 1 has to be successfully completed before attempting to make an API call as demonstrated in this guide.
The attachments for this post are:
Run the file on the head Viya Server by sending the following command:
python viya_api_client_call.py
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.