Supernatix
Supernatix

Reputation: 503

Export all API names Azure APIM

I am trying to export all the url endpoints of my APIs which are stored in Azure APIM.

I need them listed in the format [POST]: https://apim-resource-name.azure-api.net/api-name/api-function, or something similar, so I am trying to enumerate all the endpoints first and the VERB that can be used much like what SwaggerUI or one of the OpenAPI docs might show.

I have this code so far but cannot get the output I would like, am I on the right track or is there any easier way to do this even via UI? I have also tried exporting the template and parsing the script, but it seemed overly complicated for this task.

#az login

$token = az account get-access-token | ConvertFrom-Json
$token = $token.accessToken -replace "`n","" -replace "`r",""
$headers = @{Authorization="Bearer $token.accessToken"}

$subscriptionId = 'subscriptionid'
$resourceGroupName = 'resourcegroupname'
$resourceName = 'resourcename'
$apiName = 'apiname'

$url_getapiexport = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.ApiManagement/service/$resourceName/apis/$apiName`?format=swagger-link&export=true&api-version=2021-08-01" #GET
$url_getapiexport
#Invoke-RestMethod -Uri $url_getapiexport -Method GET -Headers @{Authorization="Bearer $token"} -ContentType "application/json"

$url_getapibytags = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.ApiManagement/service/$resourceName/apisByTags?&includeNotTaggedApis=true&api-version=2021-08-01" #GET
$url_getapibytags 
$api_tags = Invoke-RestMethod -Uri $url_getapibytags -Method GET -Headers @{Authorization="Bearer $token"} -ContentType "application/json"
$api_tags
foreach ($api in $api_tags) {
    write-host API: $api.value.api.name
}

Upvotes: 0

Views: 1082

Answers (1)

Baked Inhalf
Baked Inhalf

Reputation: 3735

So if you want to export all APIM APIs and their service URLs you can do like this (replace xxx with correct values). Example in PHP

<?php
echo "[INFO] API Names and Service Urls \n\n";

$apiList = shell_exec('az apim api list --resource-group "xxx" --service-name "xxx" ');
$apis = json_decode($apiList, true);
$cli = "az rest --method get --url";

foreach ($apis as $api) 
{
    $name = $api["name"];       
    $apiRequest = "$cli " . "\"https://management.azure.com/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.ApiManagement/service/xxx/apis/$name?api-version=2021-08-01\"";
    
    $json = shell_exec($apiRequest);
    $apiInfo = json_decode($json, true);
    
    $displayName = $apiInfo["properties"]["displayName"];
    $serviceUrl = $apiInfo["properties"]["serviceUrl"];

    printf("%s: %s \n", $displayName, $serviceUrl); 
}

?>

Upvotes: 1

Related Questions