API Discovery

Ismeet Kaur
Another Integration Blog
5 min readJan 18, 2023

Overview

MuleSoft Auto Discovery is a feature that allows MuleSoft applications to automatically discover its API Manager policies at runtime. This can be useful for creating a dynamic and decentralized architecture for your Mule applications.

It is an essential mechanism of managing an API from the API manager by pairing the deployed application to an API instance created on the Anypoint Platform. API Autodiscovery permits a deployed MuleSoft application to connect with API Manager to download and direct policies and generate analytics data. Mule applications can also be configured to act as their own proxy with autodiscovery.

Prerequisites

Anypoint Studio: This is an IDE provided by MuleSoft and enables the development process. It allows the developers to build the code on top of the API design created in the Design Center. Download the Anypoint Platform here.

Anypoint Platform: Anypoint Platform provides all the necessary tools to complete the API lifecycle. You can sign up for a free trial account here.

REST Client: Download and install any REST Client of your choice.

Create an API Instance

First and foremost, log in to Anypoint Platform and navigate to API Manager.

Once there, select Manage API > Create New API.

Select the appropriate Runtime for your API instance. In this case, we are going with Mule Gateway. You can connect an existing application or create a new proxy application.

You can now select the API you want to manage. It can be imported from Exchange or you can create a new API instance. To create a new API Instance, Add the name of your choice and select the Asset type which best suits your API. For this case, we will move forward with HTTP API. For REST or SOAP API, you must have a RAML or WSDL, respectively. Click Next.

Configure the Endpoint Configuration. These can be left empty as these fields are optional or filled out as per the requirement. Click Next.

Review and Save the API Instance.

The new API instance is now created. An API Autodiscovery ID is now granted to the API instance. You will need this ID to configure in the API implementation in Anypoint Studio.

Configure API implementation to enable Auto Discovery

Create a new property per environment:

It is a best practice to keep our properties in an external file instead of hardcoding. Open your project in Anypoint Studio, open your property.yaml file by navigating to src/main/resources. Add a new api.id property in the file which is the API Autodiscovery ID from API Manager.

Configure Autodiscovery in Global Elements:

Now, go to your global.xml file and switch to the Global Elements view. Click Create button and select API Autodiscovery.

Link the API ID field from the property file to this API Autodiscovery element by using the following syntax: ${api.id}. Select the Flow name as the API-kit router or the flow which has your listener endpoint. Save your application.

Test your application

Before we deploy our application to CloudHub, it is essential that we test and debug our application. Before testing, we must add two important credentials to our application. Client ID and Client Secret, for each environment, can be found in Access Management in Anypoint Platform. Navigate to Access Management > Environments > Sandbox or any other environment. and copy the values. We will include them in the Deployment Properties and in Anypoint Studio.

Go to Anypoint Studio’s preferences/settings and navigate to Anypoint Studio > API Manager. Paste the credentials here and click Validate. The organization name should match what you have on the top-right corner of your Anypoint Platform account.

Once validates, click on Apply and Close and run your Mule app. Ensure there are no errors and the application is Deployed.

Go to your REST Client and send a request to your local application. You should get back a 200 OK status. Stop the application

Deploy to CoudHub with secure Credentials

In your Anpoint Studio, navigate to the mule-artifact.json file and add the following properties to secure the Environment credentials. This will hide our properties in Runtime Manager for security reasons.

Save this file and right-click on your project. Select Anypoint Platform > Deploy to CloudHub.

Confirm that you select a previously deployed application and select Overwrite Existing Application option. Go to the Runtime Properties tab and enter your Client ID and Client Secret.

Click Deploy Application. After the application completed deployment endure that the Client ID and Client Secret in the properties are secured and the status of your API in API Manager is set as active.

Next Steps

The Mule application is now ready to enforce policies directly from API Manager without needing to redeploy your Mule application.

--

--

Ismeet Kaur
Another Integration Blog

I want to inspire women to not be afraid and take on tech. I encourage all Women who Mule to stand stronger together.