Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bradygaster/7caacd363af3f7739b443bac4699f404 to your computer and use it in GitHub Desktop.
Save bradygaster/7caacd363af3f7739b443bac4699f404 to your computer and use it in GitHub Desktop.
Import a REST API described by a Open API Specification (the artist formerly known as Swagger) file using the PowerShell cmdlets for Azure.
# Variables
Set-Variable -Name ResourceGroupName "SomeApi"
Set-Variable -Name ApimInstance "my-app-apis"
Set-Variable -Name SwaggerFilePath "C:\Users\brady\source\repos\brady\SomeApi\bin\Debug\netcoreapp3.1\swagger.json"
Set-Variable -Name ServiceUrl "https://some-api-123.azurewebsites.net"
Set-Variable -Name ApiId "SomeApi"
Set-Variable -Name ApiVersion "v1"
Set-Variable -Name AzureSubscriptionId ""
# select the subscription
Set-AzContext -SubscriptionId $AzureSubscriptionId
# import the api
Write-Output "Setting API Management Context"
$ApiMgmtContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ApimInstance
# remove the old api
Write-Output "Removing the old API"
Remove-AzApiManagementApi -Context $ApiMgmtContext -ApiId $ApiId
# import the new api
Write-Output "Importing Swagger into a new API"
Import-AzApiManagementApi -Context $ApiMgmtContext -ApiId $ApiId -ServiceUrl $ServiceUrl -SpecificationFormat "Swagger" -SpecificationPath $SwaggerFilePath -Path $ApiVersion
# get the api
$api = Get-AzApiManagementApi -Context $ApiMgmtContext -ApiId $ApiId
# disable the subscription requirement
Write-Output "Disabling the subscription header requirement"
$api.SubscriptionRequired = $false
Set-AzApiManagementApi -InputObject $api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment