9

We have some Azure Functions exposed through Api Management? Can Api Management expose a /swagger endpoint automatically, the same way the Swashbuckle package does for api's in Asp.Net.

Mathias Rönnlund
  • 4,078
  • 7
  • 43
  • 96
  • Hi Do you mean, how to import API using swagger, https://learn.microsoft.com/en-us/azure/api-management/import-and-publish – Inzi May 08 '19 at 01:38
  • In order to automate this, we can use Azure ARM APIs, https://learn.microsoft.com/en-us/rest/api/apimanagement/apis/createorupdate#apimanagementcreateapiusingswaggerimport – Inzi May 08 '19 at 01:40
  • 1
    Mostly I though about exposing the same functionality as the Swashbuckle package generates swagger pages for Asp.Net. Added this in the question. – Mathias Rönnlund May 08 '19 at 06:09

2 Answers2

6

Azure API management cannot automatically generate the swagger page. Azure API management only can provide you the API definition file. Then you can use other tools (such as Swagger UI) with the definition file to generate the page you need.

Besides, Azure API management has provided you the UI(https://youapimanagementname.portal.azure-api.net) to tell you how to use all the APIs.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • 1
    Good idea, but I'm not sure we can expose it that way to api users from our partners. – Mathias Rönnlund May 08 '19 at 07:23
  • APIM has a second optional management endpoint: https://learn.microsoft.com/en-us/rest/api/apimanagement/apimanagementrest/api-management-rest Once enabled it can be used same way as ARM to export API definitions. IT uses different authentication mechanism so it's more fitting for users that do not have ARM identity. – Vitaliy Kurokhtin May 08 '19 at 17:34
  • You can create a SAS token for your users. Then they can call the rest API with it. For more details, please refer to https://learn.microsoft.com/en-us/rest/api/apimanagement/apimanagementrest/azure-api-management-rest-api-authentication. – Md Farid Uddin Kiron May 09 '19 at 02:53
3

You can expose your openapi documentation through the API itself. The documentation of an API can be requested on

https://management.azure.com/subscriptions/[subscriptionid]/resourceGroups/[resourcegroupname]/Microsoft.ApiManagement/service/[servicename]/apis/[apiid]?export=true&format=openapi&api-version=2021-01-01-preview

Just create an additional operation (ex. openapi.yaml) on your API, call the url above through a custom policy and return the result. You can use the following policy

<policies>
    <inbound>
        <base />
        <send-request mode="new" response-variable-name="result" timeout="300" ignore-error="false">
            <set-url>@("https://management.azure.com/subscriptions/{{azure-subscriptionid}}/resourceGroups/{{azure-resourcegroup}}/providers/Microsoft.ApiManagement/service/" + context.Deployment.ServiceName + "/apis/" + context.Api.Id + "?export=true&format=openapi&api-version=2021-01-01-preview")</set-url>
            <set-method>GET</set-method>
            <authentication-managed-identity resource="https://management.azure.com/" />
        </send-request>
        <return-response>
            <set-status code="200" reason="OK" />
            <set-header name="Content-Type" exists-action="override">
                <value>application/yaml</value>
            </set-header>
            <set-body>@((string)(((IResponse)context.Variables["result"]).Body.As<JObject>()["value"]))</set-body>
        </return-response>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

More info can be found on https://www.devprotocol.com/2021/07/20/expose-openapi-documentation-on-azure-api-management.html

jtourlamain
  • 139
  • 8
  • awesome solution! One thing I noticed and had to adopt: `context.Deployment.ServiceName` for me included `azure-api.net` which I had to remove then. Was that working for you out of the box? – silent Apr 13 '22 at 11:17
  • @silent What do you mean "included azure-api.net"? What did you do to fix the problem? – Evil August May 04 '22 at 18:25
  • I'm saying that `context.Deployment.ServiceName` is something like `myapim.azure-api.net`, but the script above needs only the `myapim` – silent May 05 '22 at 11:28