API - Download a file

So far, you've learned how to authenticate and list files within a specific folder. Now, let's take it a step further and explore broader file management capabilities. In this section, you'll learn how to:

  • Check the status of a file

  • Download a file

  • List all accessible files

List files

First let list files. We already discussed listing files in single folder. Now we'll list files in general. for that we can use GET /vault_rest/files/{type}/{start}/{count} endpoint.

  • type: Accepts either "my" or "all".

    • my returns only your own files.

    • all includes all file you can access that can include your files as well as any files shared with you...

  • start and count: These parameters allow you to paginate results by specifying where to begin and how many files to retrieve.

Lets see an example

# Get your files
try {
$response = Invoke-RestMethod -Uri "$baseUrl/files/my/0/20" -Headers $headers -Method Get

# Write-Host ( $response | ConvertTo-Json | Out-String)
Write-Output $response.items | Select-Object file_id, name, size, creation_date | Format-Table -AutoSize
} catch {
Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "Error Message: $($_.Exception.Message)"
return
}
{…}

Now lets look at one single file

File status

We can get status of single file with GET /vault_rest/file/status/{file_id} endpoint. File status will have data on access rights, file type and security scanning.

# Get file status
try {
$response = Invoke-RestMethod -Uri "$baseUrl/file/status/e3de1b4640cb40af8d386f81ad3f2d79" -Headers $headers -Method Get

Write-Host ( $response | ConvertTo-Json | Out-String)
} catch {
Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "Error Message: $($_.Exception.Message)"
return
}
{
"message": null,
"ui_message_key": null,
"administrator_approval_state": "ApprovalDisabled",
"availability_state": "Available",
"availability_state_key": "FILE_STATE_AVAILABLE",
"can_access_file_approval_history": false,
"can_access_scan_results": true,
"can_be_archived": true,
"can_be_deleted": true,
"can_be_downloaded": true,
"can_be_moved": true,
"can_be_permanently_deleted": true,
"can_be_previewed": false,
"can_be_renamed": true,
"can_be_rescanned": false,
"can_be_synced_to_mft": false,
"can_edit_file_tags": true,
"can_update_file_password": true,
"can_update_sharing": true,
"cdr_result_key": "Sanitization not configured",
"current_approval_count": 0,

Download file

Now that we listed files, checked file status, lets downloaded it. For that well use GET /vault_rest/file/{file_id} endpoint. In our example we had file_id and from that we got file name. You can get file name and id by listing your files.

# Download file
# First we need file_name and name
$file_id = "e3de1b4640cb40af8d386f81ad3f2d79"

# Step 1: Get file name
try {
$file = Invoke-RestMethod -Uri "$baseUrl/file/status/$file_id" -Headers $headers -Method Get

Write-Host ( $file | ConvertTo-Json | Out-String)
} catch {
Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "Error Message: $($_.Exception.Message)"
return
}

# Selecting detination where to save our file.
$outputFile = "C:\Users\user\Documents\Test\$($file.name)"

# Step 2: Get ID of root folder
try {
# Downloading file
Invoke-RestMethod -Uri "$baseUrl/file/$($file.file_id)" -Headers $headers -Method Get -OutFile $outputFile
# Testing success