API key management script
- Last Updated: May 12, 2026
- 3 minute read
- Progress Data Cloud
- Documentation
This sample PowerShell script demonstrates how to automate API key authentication and renewal for PDC services. You may use this script as a basis for an API integration that ensures your API key stays current and authentication is properly established.
Whenever your application or automation process needs to interact with PDC APIs, implement this logic at the start. This approach validates and updates your API key as needed, then sets up the authentication headers for all subsequent API calls. This ensures reliable, uninterrupted access to PDC services.
Script overview
The API key management PowerShell script is provided below. You may use this script to automate API key authentication and renewal for Progress Data Cloud services. The script follows this workflow:
- Loads the API key from an
apikey.txtfile. - Retrieves an access token.
- Checks API key expiry date.
- Renews the key and token if less than 24 hours remain until the expiry date.
- Provides a ready-to-use authorization header containing a valid bearer token.
Setup instructions
Prerequisite:
Before using the script, one-time setup is required. You must create an apikey.txt file to be consumed by the script. Take the following steps to create the apikey.txt file.
- Generate an initial API key through the UI or API.
- Save the API key in an
apikey.txtfile. This file must be located in the same directory as the script.
Take the following steps to run the script:
- Copy the script below into a new file with a
.ps1extension (for example,pdc-auth.ps1). The script must be located in the same directory as theapikey.txtfile. - Update the
$baseUrlvariable to match your Progress Data Cloud tenancy URL. - Run the script:
.\pdc-auth.ps1 -Verbose(optional -Verbose flag shows progress). - After the script completes, use the
$headervariable for authenticated API calls.
Important:
For enhanced security, consider storing your API key in a secure keystore rather than in a plain text file. You can modify this script to retrieve the key from your preferred secure storage solution (such as Azure Key Vault, HashiCorp Vault, or Windows Credential Manager) instead of reading from apikey.txt.
PowerShell script
[CmdletBinding()]
param ()
# General variables
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$baseUrl = "https://your-tenant.marklogic.cloud"
$apikeyFile = "apikey.txt"
if (-Not (Test-Path $apikeyFile)) {
throw "API key file '$apikeyFile' not found."
}
# Utility function
function Get-AccessToken {
param (
[string]$apiKey
)
$tokenRequest = @{ grantType = "apikey"; key = $apiKey.trim() }
try {
$tokenResponse = Invoke-RestMethod -Method Post -ContentType "application/json" -Uri "$baseUrl/token" -Body $tokenRequest -ErrorAction Stop
} catch {
throw "Token request failed: $($_.Exception.Message)"
}
if (-Not $tokenResponse.access_token) {
throw "Access token not found in response."
}
return $tokenResponse.access_token
}
#============================
#Step 1 - Get an access token
#============================
Write-Verbose "Get access token using saved API key..."
$apiKey = Get-Content -Path $apikeyFile -Raw
$accessToken = Get-AccessToken -apiKey $apiKey
$header = @{ Authorization = "bearer $accessToken" }
#============================
#Step 2 - If expiry date of API key is in the next 24 hours, renew it, save it and generate new session token
#============================
try {
$apikeyDetails = Invoke-RestMethod -Headers $header -Method Get -ContentType "application/json" -Uri "$baseUrl/api/account/apikey" -ErrorAction Stop
} catch {
$errorMessage = $_.Exception.Message
throw "Session token request failed: $errorMessage"
}
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$expiryDate = [datetime]::ParseExact($apikeyDetails.expiryDate, "MM/dd/yyyy HH:mm:ss", $culture)
$now = Get-Date
$timeRemaining = $expiryDate - $now
if ($timeRemaining.TotalHours -lt 24) {
Write-Verbose "API key is approaching expiry, renewing..."
# Reset API key
try {
$apikeyResponse = Invoke-RestMethod -Headers $header -Method Put -ContentType "application/json" -Uri "$baseUrl/api/account/apikey" -ErrorAction Stop
} catch {
$errorMessage = $_.Exception.Message
throw "API key generation request failed: $errorMessage"
}
$apiKey = $apikeyResponse.apiKey
# Get and use new session token
$accessToken = Get-AccessToken -apiKey $apiKey
$header = @{ Authorization = "bearer $accessToken" }
# Save new API key (after access token generation just in case that fails)
Set-Content -Path $apikeyFile -Value $apiKey
}
#============================
#Step 3 - Continue processing (use $header for any PDC API requests)
#============================
Write-Verbose "API key validation and token generation complete..."
# Add your Progress Data Cloud API calls below this line
# Example: $response = Invoke-RestMethod -Headers $header -Method Get -Uri "$baseUrl/api/your-endpoint"
Usage example
After running this script, you can make authenticated API calls using the $header variable:
# Example API call after authentication
$services = Invoke-RestMethod -Headers $header -Method Get -Uri "$baseUrl/api/services"
$users = Invoke-RestMethod -Headers $header -Method Get -Uri "$baseUrl/api/users"