This guide provides detailed instructions and code examples on how to download data load scripts from Qlik Sense as QVS files using the Qlik API. We will cover multiple methods including utilizing the Qlik Engine API, REST API, and utilizing CLI or scripting alternatives. The guide includes code examples written in Python, C#, and PowerShell. This detailed explanation is intended for developers and analysts looking to automate the extraction of load scripts to facilitate version control, backup, or further processing.
Qlik Sense offers several options to interact with its applications programmatically via APIs such as the Engine API and the REST API. The APIs allow for various operations including retrieving the data load script—commonly stored in the "script" property of the Qlik Sense application. The downloaded script can be saved in a ".qvs" file format. This QVS file format is widely used among Qlik users for managing and maintaining reusable load scripts.
Downloading load scripts as QVS files simplifies several key tasks:
The subsequent sections provide code samples that interact with the Qlik API to download data load scripts, then save them as QVS files. The primary example is implemented in Python using the Qlik Engine API, but additional examples in C# and PowerShell are also discussed.
The Python example provided below demonstrates how to connect to a Qlik Sense application through the Qlik Engine API, retrieve the load script using the GetScript()
method, and save the script as a QVS file.
The following Python code illustrates:
GetScript()
API method.import requests
import json
# Configuration: Replace placeholders with your Qlik Cloud details and credentials
qlik_url = 'https://your-qlik-cloud-url/'
app_id = 'your-app-id'
access_token = 'your-access-token'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}'
}
def download_load_script():
"""
Retrieve the data load script using the Qlik API and save it as a .qvs file.
"""
# Construct the API endpoint using Qlik's Engine API
script_url = f'{qlik_url}/v1/apps/{app_id}/script'
response = requests.get(script_url, headers=headers)
if response.status_code == 200:
# Extract the script from the JSON payload
script = response.json().get('script')
if script:
with open('app_load_script.qvs', 'w', encoding='utf-8') as file:
file.write(script)
print("Load script downloaded successfully as app_load_script.qvs")
else:
print("No script found in the response.")
else:
print(f"Error accessing the script: {response.status_code} - {response.text}")
if __name__ == "__main__":
download_load_script()
Ensure to install the Python requests
module if it is not already available using pip install requests
. Also, make sure your access token has the necessary permissions to call the API.
If your Qlik Sense environment uses basic authentication or another method, you might need to obtain an authentication token first. The example below demonstrates a two-step process: authenticating and then retrieving the load script.
import requests
from requests.auth import HTTPBasicAuth
# Qlik Sense server URL and credentials
qlik_server_url = 'https://your-qlik-sense-server.com'
username = 'your-username'
password = 'your-password'
app_id = 'your-app-id'
# Authenticate with Qlik Sense
auth_response = requests.post(f'{qlik_server_url}/login', auth=HTTPBasicAuth(username, password))
if auth_response.status_code == 200:
auth_token = auth_response.json().get('Token')
print("Authentication successful. Token received.")
else:
print(f"Authentication failed: {auth_response.status_code} - {auth_response.text}")
exit()
# Now retrieve the load script
headers = {
'X-Qlik-Authentication': auth_token
}
script_response = requests.get(f'{qlik_server_url}/apps/{app_id}/script', headers=headers)
if script_response.status_code == 200:
load_script = script_response.text
with open("load_script.qvs", "w", encoding="utf-8") as f:
f.write(load_script)
print("Load script successfully saved as load_script.qvs")
else:
print(f"Failed to retrieve the script: {script_response.status_code} - {script_response.text}")
This code uses the HTTPBasicAuth
class to authenticate against the Qlik Sense server. After successful authentication, the load script is retrieved and saved as a .qvs file.
For developers working in the .NET ecosystem, the example below demonstrates how to use the Qlik Engine API with C#. This code connects to the Qlik Sense server, obtains the script from the specified application, and writes the contents to a QVS file.
// Using Qlik.Engine and System.IO namespaces
using Qlik.Engine;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Set your Qlik Sense server details: URL and application ID
var url = "https://your-qlik-sense-server.com";
var appId = "your-app-id";
// Initialize an engine connection. Authentication may be required here.
var engine = new Engine(url);
// Retrieve the app instance
var app = engine.App(appId);
// Use the GetScript method to retrieve the data load script
var script = app.GetScript();
// Write the script content to a QVS file
File.WriteAllText("script.qvs", script);
Console.WriteLine("Script saved as script.qvs");
}
}
This C# example highlights the usage of the Qlik Engine API for direct interaction with applications. If additional authentication is necessary, modify the connection initialization accordingly.
While the Qlik CLI is generally more oriented towards setting scripts and automating other tasks, it is nevertheless a useful part of a broader automation strategy. The CLI may not directly download scripts, but it can interact with the Qlik API. Here’s a simple PowerShell snippet that demonstrates similar automation:
# PowerShell script to download the Qlik load script
# Note: This example assumes you have a module such as Qlik-Cli-Windows installed if required.
$qlikServerUrl = "https://your-qlik-sense-server.com"
$appId = "your-app-id"
$username = "your-username"
$password = "your-password"
# Create the authentication header (this depends on your Qlik setup)
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
# Authenticate and get the token if needed (this step might vary)
$response = Invoke-RestMethod -Method Post -Uri "$qlikServerUrl/login" -Credential $credential
$authToken = $response.Token
# Define headers with the obtained token
$headers = @{
"X-Qlik-Authentication" = $authToken
}
# Retrieve the load script
$scriptResponse = Invoke-RestMethod -Method Get -Uri "$qlikServerUrl/apps/$appId/script" -Headers $headers
# Save the script to a file with .qvs extension
$scriptResponse | Out-File -FilePath "load_script.qvs" -Encoding utf8
Write-Output "Load script saved as load_script.qvs"
In this PowerShell snippet, we utilize Invoke-RestMethod
for HTTP requests. The process involves authentication, script retrieval, and saving the output to a file.
API / Tool | Programming Language | Key Steps | Description |
---|---|---|---|
Engine API | Python |
|
Retrieve and save load script using HTTP GET requests. |
REST API | Python |
|
Alternative method using direct REST calls combined with authentication. |
Engine API | C# |
|
Utilizes the .NET framework for extracting and saving the load script. |
CLI / PowerShell | PowerShell |
|
Incorporates CLI commands in scripting for automation tasks. |
Before attempting to retrieve any data load script, ensure that you have the proper authentication and authorization set up. Qlik Sense typically requires an access token or Basic Authentication parameters, depending on the server configuration. Verify that:
When writing the script to a QVS file, always consider file encoding. It is advisable to use UTF-8 encoding to ensure that any special characters within the load script are correctly recorded. Additionally, handling I/O exceptions is important to prevent data loss or file corruption.
Encapsulating the download logic into functions or classes makes the code more modular and reusable. This ease of maintenance is especially beneficial when integrating the functionality into larger automation pipelines or data management systems.
Once the load scripts are safely saved as QVS files, it is advisable to integrate them with a version control system like Git. This integration helps track changes over time, collaborate with team members, and quickly restore previous versions if needed.