Enable linting and analysis for API governance in your API center

This article shows how to enable linting to analyze API definitions in your organization's API center for conformance with your organizations's API style rules. Linting generates an analysis report that you can access in your API center. Use API linting and analysis to detect common errors and inconsistencies in your API definitions.

Scenario overview

In this scenario, you analyze API definitions in your API center by using the Spectral open source linting engine. An Azure Functions app runs the linting engine in response to events in your API center. Spectral checks that the APIs defined in a JSON or YAML specification document conform to the rules in a customizable API style guide. A report of API compliance is generated that you can view in your API center.

The following diagram shows the steps to enable linting and analysis in your API center.

Diagram showing how API linting works in Azure API Center.

  1. Deploy an Azure Functions app that runs the Spectral linting engine on an API definition.

  2. Configure an event subscription in an Azure API center to trigger the function app.

  3. An event is triggered by adding or replacing an API definition in the API center.

  4. On receiving the event, the function app invokes the Spectral linting engine.

  5. The linting engine checks that the APIs defined in the definition conform to the organization's API style guide and generates a report.

  6. View the analysis report in the API center.

Options to deploy the linting engine and event subscription

This article provides two options to deploy the linting engine and event subscription in your API center:

  • Automated deployment - Use the Azure developer CLI (azd) for one-step deployment of linting infrastructure. This option is recommended for a streamlined deployment process.

  • Manual deployment - Follow step-by-step guidance to deploy the Azure Functions app and configure the event subscription. This option is recommended if you prefer to deploy and manage the resources manually.

Limitations

  • Linting currently supports only JSON or YAML specification files, such as OpenAPI or AsyncAPI specification documents.
  • By default, the linting engine uses the built-in spectral:oas ruleset. To extend the ruleset or create custom API style guides, see the Spectral GitHub repo.
  • The Azure function app that invokes linting is charged separately, and you manage and maintain it.

Prerequisites

  • An API center in your Azure subscription. If you haven't created one already, see Quickstart: Create your API center.

  • The Event Grid resource provider registered in your subscription. If you need to register the Event Grid resource provider, see Subscribe to events published by a partner with Azure Event Grid.

  • For Azure CLI:

    Note

    az apic commands require the apic-extension Azure CLI extension. If you haven't used az apic commands, the extension is installed dynamically when you run your first az apic command. Learn more about Azure CLI extensions.

    Note

    Azure CLI command examples in this article can run in PowerShell or a bash shell. Where needed because of different variable syntax, separate command examples are provided for the two shells.

azd deployment of Azure Functions app and event subscription

This section provides automated steps using the Azure Developer CLI to configure the Azure Functions app and event subscription that enable linting and analysis in your API center. You can also configure the resources manually.

Other prerequisites for this option

Run the sample using azd

  1. Clone the GitHub repository and open it in Visual Studio Code.

  2. Change directory to the APICenter-Analyzer folder in the repository.

  3. In the resources/rulesets folder, you can find an oas.yaml file. This file reflects your current API style guide and can be modified based on your organizational needs and requirements.

  4. Authenticate with the Azure Developer CLI and the Azure CLI using the following commands:

    azd auth login
    
    az login
    
  5. Run the following command to deploy the linting infrastructure to your Azure subscription.

    azd up
    
  6. Follow the prompts to provide the required deployment information and settings, such as the environment name and API center name. For details, see Running the sample using the Azure Developer CLI (azd).

    Note

    The deployment might take a few minutes.

  7. After the deployment is complete, navigate to your API center in the Azure portal. In the left menu, select Events > Event subscriptions to view the event subscription that was created.

You can now upload an API definition file to your API center to trigger the event subscription and run the linting engine.

Manual steps to configure Azure Functions app and event subscription

This section provides the manual deployment steps to configure the Azure Functions app and event subscription to enable linting and analysis in your API center. You can also use the Azure Developer CLI for automated deployment.

Other prerequisites for this option

Step 1. Deploy your Azure Functions app

To deploy the Azure Functions app that runs the linting function on API definitions:

  1. Clone the GitHub repository and open it in Visual Studio Code.

  2. In the resources/rulesets folder, you can find an oas.yaml file. This file reflects your current API style guide and can be modified based on your organizational needs and requirements.

  3. Optionally, run the function app locally to test it. For details, see the README file in the repository.

  4. Deploy the function app to Azure. For steps, see Quickstart: Create a function in Azure with TypeScript using Visual Studio Code.

    Note

    Deploying the function app might take a few minutes.

  5. Sign in to the Azure portal, and go to the function app.

  6. On the Overview page, check the following details:

    • Confirm that the Status of the function app is Running.
    • Under Functions, confirm that the Status of the apicenter-analyzer function is Enabled.

    Screenshot of the function app in the portal.

Step 2. Configure managed identity in your function app

To enable the function app to access the API center, configure a managed identity for the function app. The following steps show how to enable and configure a system-assigned managed identity for the function app using the Azure portal or the Azure CLI.

  1. In the Azure portal, navigate to your function app and select Identity under the Settings section.
  2. On the System assigned tab, set the Status to On and then select Save.

Now that the managed identity is enabled, assign it the Azure API Center Compliance Manager role to access the API center.

  1. In the Azure portal, navigate to your API center and select Access control (IAM).
  2. Select + Add > Add role assignment.
  3. Select Job function roles and then select Azure API Center Compliance Manager. Select Next.
  4. On the Members page, in Assign access to, select Managed identity > + Select members.
  5. On the Select managed identities page, search for and select the managed identity of the function app. Click Select and then Next.
  6. Review the role assignment, and select Review + assign.

Step 3. Configure event subscription in your API center

Now create an event subscription in your API center to trigger the function app when an API definition file is uploaded or updated. The following steps show how to create the event subscription using the Azure portal or the Azure CLI.

  1. In the Azure portal, navigate to your API center and select Events.

  2. On the Get started tab, select Azure Function.

  3. On the Create Event Subscription page, do the following:

    1. Enter a descriptive Name for the event subscription, and select Event Grid Schema.

    2. In Topic details, enter a System topic name of your choice.

    3. In Event Types, select the following events:

      • API definition added
      • API definition updated
    4. In Endpoint Details, select Azure Function > Configure an endpoint.

    5. On the Select Azure Function page, select the function app and the apicenter-linter function that you configured. Click Confirm selection.

    6. Select Create.

      Screenshot of creating the event subscription in the portal.

  4. Select the Event subscriptions tab and select Refresh. Confirm that the Provisioning state of the event subscription is Succeeded.

    Screenshot of the state of the event subscription in the portal.

Note

It might take a short time for the event subscription to propagate to the function app.

Trigger event in your API center

To test the event subscription, try uploading or updating an API definition file associated with an API version in your API center. For example, upload an OpenAPI or AsyncAPI document. After the event subscription is triggered, the function app invokes the API linting engine to analyze the API definition.

To confirm that the event subscription was triggered:

  1. Navigate to your API center, and select Events in the left menu.

  2. Select the Event Subscriptions tab and select the event subscription for your function app.

  3. Review the metrics to confirm that the event subscription was triggered and that linting was invoked successfully.

    Screenshot of the metrics for the event subscription in the portal.

    Note

    It might take a few minutes for the metrics to appear.

After analyzing the API definition, the linting engine generates a report based on the configured API style guide.

View API analysis reports

You can view the analysis report for your API definition in the Azure portal. After an API definition is analyzed, the report lists errors, warnings, and information based on the configured API style guide.

In the portal, you can also view a summary of analysis reports for all API definitions in your API center.

Analysis report for an API definition

To view the analysis report for an API definition in your API center:

  1. In the portal, navigate to the API version in your API center where you added or updated an API definition.
  2. In the left menu, under Details, select Definitions.
  3. Select the API definition that you uploaded or updated.
  4. Select the Analysis tab. Screenshot of Analysis tab for API definition in the portal.

The API Analysis Report opens, and it displays the API definition and errors, warnings, and information based on the configured API style guide. The following screenshot shows an example of an API analysis report.

Screenshot of an API analysis report in the portal.

API analysis summary

To view a summary of analysis reports for all API definitions in your API center:

  1. In the portal, navigate to your API center.

  2. In the left-hand menu, under Governance, select API Analysis. The summary appears.

    Screenshot of the API analysis summary in the portal.

Learn more about Event Grid: