Introduction:
Leapwork allows us to easily test the endpoints in the API and explore the results.
The below article will enable us to Export & Import flows from the Leapwork using PowerShell Script using API.
Prerequisites:
We will need to know the following information before proceeding:
Step 1. Controller Hostname - Go to the machine where the controller is installed.
- Open cmd
- Type hostname
- Hit Enter
Step 2. Read/Write access to the path from where flows will be Imported/Exported
Step 3. Controller API Port Number (by default its 9001)
(This can be checked from the C:\Program Files\LEAPWORK\Controller\controller.config)
Step 4. Leapwork Access Key (Can be obtained from Leapwork studio, Settings - API Access keys).

Step 5. TimeoutMinutes - Define the maximum timeout based on the volume of export objects
Step 6. URL of API
- Trial & Enterprise Version
Import - http://localhost:9001/api/v4/import/{teamId}/{timeoutMinutes}/{targetItemId}
- Platform Version
Step 7. Get TeamId from the Leapwork studio:
- Settings
- Team Management
- Right-click on any Team
- Get ID.
Backup Procedures to be followed:
Step 1. Open Windows PowerShell ISE as an administrator.
Step 2. Paste the code in PowerShell, **script code (below).
Step 3. Save the file, the file will be saved with *.ps1 extension.
Step 4. To perform the export of flows, right-click and run with PowerShell file. We will see it perform some queries and then it will export the data.
Note: The amount of time to perform the export may take a while depending on the size/volume of the export objects/flows.
**Script Code
headers = @{}
$headers.Add("AccessKey","768123")
$headers.Add("Accept","application/octet-stream")
Invoke-WebRequest -Uri "http://localhost:9001/api/v4/export/9fb4ef50-6924-471d-8261-052aa8b572c9/60" -ContentType "application/octet-stream" -Headers $headers -Method GET -OutFile "C:\Users\SandeepKumar\Desktop\Backup_$(get-date -f yyyy-MM-dd).zip"
Auto Backup Procedure using the Task Scheduler
Step 1. Click on the windows start button, search and open Task Scheduler
Step 2. Click on Action - Create Task
Step 3. In General, Name the task (ex. Powershell Backup).




Restore Procedure:
Step 1. Open Windows PowerShell ISE as an administrator.
Step 2. Paste the below code in PowerShell
Import Flows
function Import
{
param
(
[string]$Path,
[string]$Hostname,
[int]$ApiPort,
[string]$AccessKey,
[int]$MinutesTimeOut,
[string]$TargetItemId = ""
)
$fileName = [IO.Path]::GetFileName($Path)
$boundary = [guid]::NewGuid().ToString()
$fileBytes = [System.IO.File]::ReadAllBytes($Path)
$fileBody = [System.Text.Encoding]::GetEncoding("iso-8859-1").GetString($fileBytes)
$LF = "`r`n";
$headers = @{}
$headers.Add("AccessKey",$AccessKey)
$headers.Add("Accept","application/json")
$url = "http://localhost:9001/api/v4/import/167cd0f3-e2ff-47b1-9a3c-22d7ebf1e248/180"
$bodyLines = ("--$boundary","Content-Disposition: form-data; name=`"zip file`"; filename=`"import.zip`"","Content-Type: application/x-zip-compressed$LF",$fileBody,"--$boundary--$LF") -join $LF
try {
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -ContentType "multipart/form-data;boundary=`"$boundary`"" -Body $bodyLines
}
catch
{
$ErrorMessage = $_.Exception.Message
$ErrorMessage
}
}
$uploadPath="C:\Users\SandeepKumar\Desktop\a.zip"
$hostName = "localhost"
$apiPort = 9001
$accessKey = "768123"
$timeOut = 180
#Import $uploadPath
Import -Path $uploadPath -Hostname $hostName -ApiPort $apiPort -AccessKey $accessKey -MinutesTimeOut $timeOut
Step 3. Save the file, the file will be saved with *.ps1 extension.
Step 4. To perform the import of flows, right-click and run with the PowerShell file. We will see it perform some queries and then it will import the data.
If you have any questions, contact our Priority Support.
Comments
0 comments
Please sign in to leave a comment.