Qlik Community

Knowledge

Search or browse our knowledge base to find answers to your questions ranging from account questions to troubleshooting error messages. The content is curated and updated by our global Support team

Announcements
IMPORTANT security patches for GeoAnalytics Server available to download: READ DETAILS

Call the Qlik Sense QRS API with Python

cancel
Showing results for 
Search instead for 
Did you mean: 
Damien_Villaret
Support
Support

Call the Qlik Sense QRS API with Python

This article is a guide on how to call the Qlik Sense Repository API using Python. 


Environments:

  • Qlik Sense any version 
  • Python 3.x

 

 

Prerequisites to host the app on a different server

 

  • Export the certificates from the QMC, provide a password and select platform independent PEM-format in the  Export file format for certificates option.
  • Copy the client.pem and client_key.pem certificates to the external server where the application is hosted.


The Qlik Sense Repository (QRS) API is a RESTful API, so in order to call it from Python, you would need to use a module such as requests

This can be simply installed using pip:

pip install requests

 

Option 1: Connecting using certificates

Create a python file test.py with the following contents (strings in red should be replaced to match your environment):

import requests

requests.packages.urllib3.disable_warnings()

#Set up necessary headers comma separated
xrf = 'iX83QmNlvu87yyAB'
headers = {'X-Qlik-xrfkey': xrf,
"Content-Type": "application/json",
"X-Qlik-User":"UserDirectory=DOMAIN;UserId=Administrator"}

#Set up the certificate path
cert = 'C:\certificates\clientandkey.pem'

#Set the endpoint URL
url = 'https://qlikserver1.domain.local:4242/qrs/app/full?xrfkey={}'.format(xrf)

#Call the endpoint to get the list of Qlik Sense apps
resp = requests.get(url, headers=headers, verify=False, cert=cert)

if resp.status_code != 200:
    # Returns an error if something went wrong.
    raise ApiError('GET /qrs/app/full {}'.format(resp.status_code))
for app in resp.json():
    print('{} {}'.format(app['id'], app['name']))


The clientandkey.pem file should look like below and contains both private key and certificate in pem format (Certificate and private key can be exported from QMC and it can be merged manually using a text editor)

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAigl7uyMr9zdiyZ4UU99IA15qaR6YisrkAxOEDh9aC5xX8cKX
mS++v6JjoIJCItLaAII19ubKylSLQZMfiYNMqKrKQIKH8VqgK8G5H/pAYkvpWuz+
MIp5cT4Xxg8sGNFFygH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WYVKKZg36ryR+h1omrSGo3ic+HowFAYIKwYBBQUHDQMECAQGQ2xpZW50MA0G
CSqGSIb3DQEBCwUAA4IBAQA2gVHTFyXOsjs2Vr1/EqNvx//a5QM+xcBSUvTQXfzZ
zoNofN0YCR5gc5SfS7ihf5R95MkrYXfKdhgCriqXVYExWiA0uPOLWsuMj9iaDJys
0494kUMA9UqiS+8AIfidCqkn4G1QpqtjEwPQMp3M2U3GkTkabQp6BB0Lf6srai6a
ASk33xatdm8c8mf8sTemm3Iu2VDR02eX6gNeDdo4S1pmil1HsIPJKqDEwXvRY0nF
kxedriyHdsXAz6Cb3stSl0szAeRuQ/B2UF3nW7VGMAjLiMFuDTLXai0539bzii0A
5rujJu/mt+wyljie0JUBfK1dvDCSSQUbHl4IEFGTX08b
-----END CERTIFICATE-----

If you get an error involving certificates change the following line in the code
cert = 'C:\certificates\clientandkey.pem'
by
cert = ('C:/certificates/client.pem', 'C:/certificates/client_key.pem')

 

Option 2: Connecting using Windows authentication

You will need to also install the following module to handle NTLM authentication:

pip install requests_ntlm


Create a python file test.py with the following contents (strings in red should be replaced to match your environment):

import requests
from requests_ntlm import HttpNtlmAuth

requests.packages.urllib3.disable_warnings()

#Set up necessary headers comma separated
xrf = 'iX83QmNlvu87yyAB'
headers = {'X-Qlik-xrfkey': xrf,
"Content-Type": "application/json",
"User-Agent":"Windows"}

#Set up user credentials
user_auth = HttpNtlmAuth('domain\\user1','MyPassword')

#add xrfkey to URL
url = 'https://qlikserver1.domain.local/qrs/app/full?xrfkey={}'.format(xrf)

#Call the endpoint to get the list of Qlik Sense apps
resp = requests.get(url,headers = headers,verify=False,auth=user_auth)

if resp.status_code != 200:
    # Returns an error if something went wrong.
    raise ApiError('GET /qrs/app/full {}'.format(resp.status_code))
for app in resp.json():
    print('{1} {0}'.format(app['id'], app['name']))





Example of result obtained:

PS C:\certificates> python test.py
9b428869-0fba-4ba5-9f94-901ae2fdf041 test1
72e183c7-f838-4a8e-8e16-1014aa80acb4 License Monitor
38476273-ae47-475b-bee0-68cffa384ae1 Operations Monitor
9cdad10c-a230-4c78-b4f7-0d97ca30a48a testuser2
9fa859ed-b59c-4574-bf26-2620e09f1289 test1(1)
d3c96fe7-2b3b-457a-82d7-2b32edbf4190 License Monitor_22.0.4.0
df32a902-bccb-4187-8c64-4223f6694f7c Operations Monitor_22.0.4.0


  ! The information in this article is provided as-is and to be used at own discretion. Ongoing support on the solution is not provided by Qlik Support.

Labels (1)
Comments
cpra_qlik
Contributor
Contributor

Hi @Damien_Villaret  , 

this is an amazing article, which I have been able to use and works as expected. I have a question though:

We are using a third party server provider for our ssl certificate.

The question I have is using the PEM files, as per above i.e. Qlik default ones is the connection secure? 

Or do we need to generate equivalent PEM files from our ssl certificate ?

 

Damien_Villaret
Support
Support

Hello @cpra_qlik 

In the current product design, it's not possible to replace the QlikClient certificate by a 3rd party generated certificate. The API can only be called by the QlikClient certificate generated by Qlik Sense itself.

DavidFosterVF
Contributor III
Contributor III

@Damien_Villaret will this approach work via the proxy server rather than direct to port 4242?

Damien_Villaret
Support
Support

@DavidFosterVF 

Yes, that's what option 2 is explaining if you scroll down in the article.

DavidFosterVF
Contributor III
Contributor III

@Damien_Villaret  got it working now. Thanks

Version history
Last update:
a month ago
Updated by:
Contributors