You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何导出Azure APIM中所有API的端点及请求方法?

导出Azure APIM所有API端点的方法

一、UI操作方式(简便快速)

如果不需要自动化流程,直接通过Azure门户就能快速获取或导出:

  • 登录Azure门户,进入目标APIM服务
  • 在左侧菜单选择「API」,选中需要导出的API(支持多选)
  • 点击顶部工具栏的「导出」,选择「OpenAPI 3.0」或「Swagger 2.0」格式下载
  • 用任意OpenAPI查看工具(如本地Swagger UI)打开下载文件,即可看到所有「VERB: 端点URL」形式的接口,也可直接在文件中提取内容

若需批量查看所有API端点,还可通过APIM开发者门户:

  • 进入APIM服务的「开发者门户」(左侧菜单直接点击)
  • 在门户内浏览所有API的操作,每个操作都会明确显示请求方法和完整URL

二、PowerShell自动化实现(修正你的代码)

你的原代码存在几个关键问题:

  • 授权头拼接错误:$headers里的$token.accessToken是冗余的,$token本身就是获取到的accessToken值
  • 遍历结构错误:apisByTags返回的有效数据是$api_tags.value数组,而非直接遍历$api_tags
  • 缺少操作获取步骤:apisByTags仅返回API基本信息,无法拿到具体请求方法和端点,需额外调用接口获取每个API的操作

以下是修正后的完整代码,运行后直接输出你需要的格式:

# 登录Azure(未登录时执行)
az login

# 获取访问令牌
$token = (az account get-access-token | ConvertFrom-Json).accessToken
$headers = @{Authorization = "Bearer $token"}

# 配置参数
$subscriptionId = '你的订阅ID'
$resourceGroupName = '你的资源组名称'
$resourceName = '你的APIM服务名称'
$apiVersion = '2021-08-01'

# 获取所有当前版本的API
$apiListUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.ApiManagement/service/$resourceName/apis?api-version=$apiVersion&`$filter=isCurrent eq true"
$apiList = (Invoke-RestMethod -Uri $apiListUrl -Method GET -Headers $headers).value

# 遍历每个API,获取操作并输出指定格式
foreach ($api in $apiList) {
    # 跳过系统内置API(如management,不需要可删除此判断)
    if ($api.name -in ('management', 'graphql', 'websocket')) {
        continue
    }

    # 获取当前API的所有操作
    $operationsUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.ApiManagement/service/$resourceName/apis/$($api.name)/operations?api-version=$apiVersion"
    $operations = (Invoke-RestMethod -Uri $operationsUrl -Method GET -Headers $headers).value

    # 拼接完整端点URL并输出
    foreach ($op in $operations) {
        $fullUrl = "https://$resourceName.azure-api.net$($op.properties.urlTemplate)"
        Write-Host "[$($op.properties.method)]: $fullUrl"
    }
}

代码说明:

  • 过滤了系统内置API,若需保留可删除if判断逻辑
  • urlTemplate是API操作的路径模板,直接拼接APIM域名即可得到完整端点
  • 输出格式完全匹配你要求的「[POST]: https://xxx」形式

三、Azure CLI快速实现

如果你熟悉Azure CLI,也可以用CLI配合命令行工具快速提取:

az apim api list --resource-group <资源组名> --service-name <APIM服务名> --query "[].name" -o tsv | while read api; do
    az apim api operation list --resource-group <资源组名> --service-name <APIM服务名> --api-id $api --query "[].{method:properties.method, url:properties.urlTemplate}" -o tsv | while read method url; do
        echo "[$method]: https://<APIM服务名>.azure-api.net$url"
    done
done

内容的提问来源于stack exchange,提问作者Supernatix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 06:20:33