---
updatedAt: 2025-08-14T17:02:33.000Z
agentTools:
  siteIndex: https://developer.n-able.com/llms.txt
  projectIndex: https://developer.n-able.com/n-central/llms.txt
---

# Azure Keyvault REST API Code Example

```powershell PowerShell
########## VARIABLES BEGIN ##########
$Modules = @("Az.Accounts", "Az.Keyvault")
$serverName = "n-central.acme.com" # your N-central server
$Azure = @{
    "Tenant"     = "" # Your Azure Tenant GUID
    "VaultName"  = "" # Your Azure Keyvault Name
    "SecretName" = "" # Your Azure Keyvault Secret Name
}
########## VARIABLES END ##########

$NuGet = Get-PackageProvider -Name "NuGet"
If (($null -eq $NuGet) -or ($Nuget.version -lt "2.8.5.208")) {
    # Only install if NuGet isn't version 2.8.5.208 or newer (doesn't need to be the latest version)
    Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force -Scope CurrentUser -ea 0
}

foreach ($Module in $Modules) {
    $ModuleInstalledVersion = (Get-InstalledModule -Name $Module -ea 0).Version
    if ($null -ne $ModuleInstalledVersion) {
        $ModuleAvailableVersion = (Find-Module -Name $Module -ea 0).Version
        if (($null -ne $ModuleAvailableVersion) -and ($ModuleInstalledVersion -ne $ModuleAvailableVersion)) {
            # Available online but the incorrect version localy
            Install-Module -Name $Module -Scope AllUsers -Force -ea 0
        }
    }
    else {
        # Not installed localy
        Install-Module -Name $Module -Scope AllUsers -Force -ea 0
    }
}

if ($null -eq $apiToken) { 
    Connect-AzAccount -Tenant $Azure.Tenant -wa 0 | Out-Null # To suppress connecting message & Warning about multiple active subscriptions
    $apiToken = (Get-AzKeyVaultSecret -vaultname $Azure.VaultName -name $Azure.SecretName -AsPlainText)
    Disconnect-AzAccount | Out-Null # To suppres a closing message
}

if ($null -eq $apiToken) { exit 1 }

function Get-BearerToken {
    $authURL = "https://$($serverName)/api/auth/authenticate"
    $authHeaders = @{
        "accept"        = "*/*"
        "Authorization" = "Bearer $apiToken"
    }
    $authResponse = Invoke-RestMethod -Uri $authURL -Method Post -Headers $authHeaders
    return $authResponse.tokens
}

function Update-BearerToken {
    $validateURL = "https://$($serverName)/api/auth/validate"
    $ValidateHeader = @{
        "accept"        = "application/json"
        "Authorization" = "Bearer $($bearerToken.access.token)"
    }
    $ValidateResponse = Invoke-RestMethod -Uri $validateURL -Method Get -Headers $ValidateHeader -ErrorAction SilentlyContinue
    if ($ValidateResponse.message -ne "The token is valid.") {
        try {
            $RefreshURL = "https://$($serverName)/api/auth/refresh"
            $RefreshHeaders = @{
                "accept"        = "*/*"
                "Authorization" = "Bearer $($bearerToken.access.token)"
                "Content-Type"  = "text/plain"
            }
            $RefreshResponse = Invoke-RestMethod -Uri $RefreshURL -Method Post -Headers $RefreshHeaders -Body $bearerToken.refresh.token
            $bearerToken = $RefreshResponse.tokens
        }
        catch {
            Throw "Refresh Token expired, start a new session"
        }
    }
}

function Get-APIResponse([parameter(mandatory)][String]$Endpoint, [string]$SortBy, [switch]$Descending, [Hashtable]$Parameters) {
    Update-BearerToken # First we check if we need to refresh the bearer token
    if ($null -eq $Parameters) { $Parameters = @{} } # If no parameters Hashtable is given, create one
    if ($null -eq $Parameters.pageSize) { $Parameters.pageSize = 1000 } # If no pageSize is mentioned in the parameters, set it to 1000
    $Parameters.pageNumber = 1 # Always start on pageNumber one, no matter what is in the parameters
    if ($null -ne $SortBy) {
        # The options SortBy and Descending overrule what is in the parameters hashtable
        $Parameters.sortBy = $SortBy
        $Parameters.sortOrder = if ($Descending) { "descending" } else { "ascending" }
    }
    $RestResponses = [System.Collections.ArrayList]@() # Create an arraylist to put in all responses (faster and less memory usage)
    $RestURL = "https://$($serverName)/api/$($Endpoint)" # Generate the url for the API endpoint we want to use
    $RestHeader = @{ # Create the header
        "accept"        = "*/*"
        "Authorization" = "Bearer $($bearerToken.access.token)"
    }

    do {
        $RestResponse = [System.Collections.ArrayList]@(Invoke-RestMethod -Uri $RestURL -Method Get -Headers $RestHeader -Body $Parameters) # Execute the API call
        if ($RestResponse.pageNumber -ne $RestResponse.totalPages) { $Parameters.pageNumber++ } # Are there new pages, then add 1 to pagenumber
        $RestResponses.AddRange([System.Collections.ArrayList]@($RestResponse.data)) # Add the (max 1000) data points returned from the API call to the Arraylist
    } while ($RestResponse.pageNumber -ne $RestResponse.totalPages) # Do this until there are no more pages

    return $RestResponses # Return your result
}

$bearerToken = Get-BearerToken


```

```json Response Example
{"success":true}
```

# Azure API Connection