Deploying and managing your first API

manage your first api
jordan schuetz
Jordan Schuetz
Developer Advocate
20 Min Read
Getting started

Now that we have successfully created our first mule application in our previous tutorial, let’s learn how to manage our API with API Manager. API Manager allows you to add policies such as rate-limiting, IP blacklisting, authentication, and threat protection. These policies can be added after you publish your integration so that way you can monitor and change settings even if your API is live to consumers. First, we will learn how to add API Autodiscovery to our mule application which registers your application to the API gateway to set permissions and SLA’s. Next, we will learn how to publish our mule application to CloudHub, and then manage our API via API Manager. Let’s get started in just a few easy steps, and if you haven’t read the previous two tutorials please go back to the last tutorial by clicking here.

Step 1: Signup for Anypoint Platform

The first step is to Sign up for Anypoint Platform for free. Click the button below to create a free account.

If you have already done this step, make sure you are logged in. If you haven’t read the previous two tutorials, start by creating your first API by clicking here.

Step 2: Configure API Autodiscovery and upload to CloudHub

When your API specification is published on Exchange, you can begin the process of registering the API, setting up security policies, SLA’s and analytics throughout the lifecycle of your API through API Manager. You need to first configure API Manager by logging into Anypoint Platform (click the button in Step 1), and in the Management Center, select API Manager.

All organizations by default have a Sandbox and Design environment, which can have independent configurations, policies, and proxies. The Sandbox environment will typically be limited in available resources and you may need to adjust your deployment target or worker size depending upon the scale of your application. For now, go to API Manager, and click the blue button that says Manage API and select Manage API from Exchange. Select the Contact API that you created on the first tutorial.

Leave the implementation URI blank and make sure your Asset version is the latest version published. Don't forget to check the box that declares you are using Mule 4 and above.

api manager mulesoft

After clicking Save, you will see a new screen that contains an "API Instance" ID. Copy this ID to your clipboard to use it in Anypoint Studio.

In order for your application to register itself to the gateway, we need to add and configure API Autodiscovery in your Anypoint Studio application. To add the API Autodiscovery component:

  1. In Anypoint Studio, click Global Elements to view all the globally accessible elements in your Mule App.
  2. Click the Create button.
  3. Expand the Component configurations dropdown.
  4. Select API Autodiscovery and press OK.
api autodiscovery mulesoft

Next, paste your API ID into the API Autodiscovery Global Element Properties window (you can access that window by clicking on API Autodiscovery in the Global Elements tab, then clicking the Edit button). For your Flow Name selection, make sure to choose: contact-api-main (or whatever you named your main flow). Click OK to save your changes.

api autodiscovery

Before publishing your application, you need to obtain your organization’s credentials to control apps that are registered on your gateway. To do this, you will need to provide a client ID and secret that is used in conjunction with the API ID to provision it to the correct organization, environment, and API version. Log back in to Anypoint Platform, and navigate to Access Management -> Environments. Then click on the environment name that matches the environment from API Manager, which will show a new lightbox. Keep this window open for the next setup.

setup environment anypoint platform
Step 3: How to deploy to CloudHub from Anypoint Studio

Now that you have developed your first Mule application, and setup API Autodiscovery in API Manager, let’s deploy from within Anypoint Studio to CloudHub. CloudHub is the MuleSoft managed platform for deploying your API’s and integrations.

To start, right-click on your project folder called contactapi in the Package Explorer tab, and select Anypoint Platform and click Deploy to CloudHub. Select Sandbox and name your application whatever you wish.

Deploy to Cloudhub

This will open the application deployment window where you can select the environment you'll be publishing to (same as prior steps), set the runtime version, resource allocation, etc.

The deployment name you want to choose may already be taken. In this case, please type in a different name for deployment that is available. Also, you may reach your worker size limit; however, your account may be provisioned enough if you reduce the worker size to “0.1 vCore”.

From the steps above, the application knows which API to associate itself with via API ID, but it doesn't have the authentication parameters needed to register. Instead of baking it into the app itself with a properties file, you'll be placing the client ID and secret from Step 2 in the properties tab of the deployment window in order to allow your app to authenticate itself and update easily should the need arise without having to edit the project (e.g. promoting from sandbox to production). Insert the following properties and replace the fields accordingly from the previous section:

Cloudhub Application Deployed

Click Deploy Application and click Open in Browser to grab the URL of your published API and integration from Runtime Manager. This will open the application deployment window where you can select the environment you'll be publishing to (same as prior steps), set the runtime version, resource allocation, etc.

contact API deployed

When you open up your deployed application in CloudHub, grab the URL that's displayed in Runtime Manager, and insert that URL back into PostMan and try sending the same payload as before. You should notice that the API will return you with the desired values.

postman endpoint

You can also test out the API by following a CURL request (replace the deployed app instance name with your own):

curl -X GET http://{{YOUR ROOT URL}}/api/contacts

Step 4: Setting policies on deployed apps in API Manager

You can now go back to API Manager and see that the "Status" column of your API is now marked as Active with a green dot next to it. This means the app has successfully registered with the gateway and you can manage it. To set a rate limit policy on the api.

  1. Click v1.0.
    • This will take you to the settings for your API.
  2. Click Policies → Apply New Policies → Rate limiting → Configure Policy.
    • You're now at the Rate limit configuration window.
  3. Configure the Rate limit to the following:
    • # of Reqs: 1
    • Time Period: 1
    • Time Unit: Minute

Go back to PostMan, or your CURL request and try sending the request again. You will notice that you will get a 429 Too Many Requests error once rate limiting has been setup! After you test out this feature, feel free to remove this API policy since you will need to use this API in the next tutorial.

Congrats! Now let’s integrate our API with Salesforce

You did it! Your app is now deployed and is able to be analyzed and managed! Your app has registered itself with the gateway and can set policies and even set up SLA tiers.

In our next tutorial, you will learn how to integrate your API with Salesforce! To continue to the next tutorial, click here to learn how to build an integration between Salesforce and your API.

Thanks for reading, your feedback is much appreciated. Please rate this tutorial below.

Try Anypoint Platform for free

Start free trial

Already have an account? Sign in.

Related tutorials