# Leapwork Export/Import Flows using PowerShell Script through API

**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). Please define the scope of the API Access Key by checking the required checkboxes. The same should be done for the Team scope as well. The goal of defining Scope is to restrict the API Access Key usage based on the requirements.

**Step 5.** Timeout Minutes - Define the maximum timeout based on the volume of export objects

**Step 6.** URL of API

- **_Trial & Enterprise Version_**

Export - [http://localhost:9001/api/v4/export/{teamId}/{timeoutMinutes}/{targetItemId}](http://localhost:9001/api/v4/export/%7BteamId%7D/%7BtimeoutMinutes%7D/%7BtargetItemId%7D "http://localhost:9001/api/v4/export/{teamId}/{timeoutMinutes}/{targetItemId}")

Import - [http://localhost:9001/api/v4/import/{teamId}/{timeoutMinutes}/{targetItemId}](http://localhost:9001/api/v4/export/%7BteamId%7D/%7BtimeoutMinutes%7D/%7BtargetItemId%7D "http://localhost:9001/api/v4/export/{teamId}/{timeoutMinutes}/{targetItemId}")

- **_Platform Version_**

Export - [http://localhost:9001/api/v4/export/{timeoutMinutes}/{targetItemId}](http://localhost:9001/api/v4/export/%7BteamId%7D/%7BtimeoutMinutes%7D/%7BtargetItemId%7D "http://localhost:9001/api/v4/export/{teamId}/{timeoutMinutes}/{targetItemId}")

Import - [http://localhost:9001/api/v4/import/{timeoutMinutes}/{targetItemId}](http://localhost:9001/api/v4/export/%7BteamId%7D/%7BtimeoutMinutes%7D/%7BtargetItemId%7D "http://localhost:9001/api/v4/export/{teamId}/{timeoutMinutes}/{targetItemId}")

**Step 7. Get TeamId** from the **Leapwork studio:**

1. Settings
2. Team Management
3. Right-click on any Team
4. 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**

```powershell
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**).

**Step 4.** Click on **Triggers - New** and define the schedule for executing the script.

**Step 5.** Click **Actions-New,** in **Action** choose to start a program, in **Program/Script** add Powershell and in **Add arguments** add **-file “path of the script”**

**Step 6.** This will create a task in **Task Scheduler** which will execute as per the scheduled defined.

**Restore Procedure:**

**Step 1.** Open **Windows PowerShell ISE** as an administrator.

**Step 2.** Paste the below code in **PowerShell**

```powershell
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 = "``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, please reach out to our [Priority Support](mailto:prioritysupport@leapwork.com).
