Contact Us 1-800-596-4880

Step 3. Develop the API

Now that you’ve created an API specification, use it to create a Mule app.

In Anypoint Platform, developers package an API in an application that is deployed to an instance of Mule runtime engine (Mule). Mule is a lightweight integration engine that runs Mule apps. Instances of Mule are embedded in Studio, and in the environments where you deploy Mule apps and APIs. These environments are called targets. One target is the Mule embedded in Studio, which you use for development and testing. Another target, CloudHub, is managed by MuleSoft and is cloud based.

First, you’ll deploy the app to Mule in Studio and then, you’ll deploy it to Mule in CloudHub.

Studio without any projects

  1. Package Explorer helps you navigate the files created for each project.

  2. The canvas is where you drag elements of your project and connect them, creating a flow.

  3. The Mule Palette provides quick access to standard or custom assets such as connectors and modules.

  4. The tabs in the configuration section provide access to configuration dialogs, the Mule debugger, and other developer dialogs.

Step 3.1: Add Your Anypoint Platform Credentials to Studio

Add your Anypoint Platform credentials in Studio once, and they’ll be available for all the future projects you create.

  1. Open Studio, and then select Anypoint Studio > Settings.

  2. In the Preferences window, click Anypoint Studio, and then Authentication.

  3. Click Add and enter your Anypoint Platform username and password.

  4. Click Sign in.

  5. Optionally, click Yes in the Secure Storage - Password Hint Needed dialog.

  6. Click Apply and Close.

These credentials are automatically used for many operations in Studio.

Step 3.2: Create a New Studio Project with an Imported API Specification

In this step, you create the project.

  1. In Studio, select File > New > Mule Project.

    New project dialog

  2. In the dialog box, provide the following values:

    • Project Name: hello-world-app

    • Runtime: If there is a choice, select the latest version of Mule. This is the Mule runtime engine that is hosted within Studio for testing.

    • Select the Import a published API tab.

      1. Click the green plus sign, and then select From Exchange.

      2. Log in to your organization if needed.

      3. Type hello-world in the search box.

      4. Click hello-world-api to select it.

      5. Click Add to move it to the Selected modules list, and then click Finish.

  3. Verify that the checkbox Scaffold flows from these API specifications is selected, and click Finish.

    This setting ensures that Studio adds default error handling and other helpful items to your project

Studio creates a hello-world.xml XML file and opens it in the canvas Message Flow view, showing the basic elements you need for the API interface:

View in Studio after importing API specification with scaffolding

Scroll down the canvas to see the scaffolding that was added to your app:

  • HTTP Listener is ready to receive requests

  • APIkit Router routes messages

  • Error handlers for different error conditions

  • A console, hello-world-app-console, which you won’t use for this project

  • The GET request to endpoint /greeting, where you do the bulk of your configuration

There are three different views of the canvas:

One canvas and links to all three views

  • Use Message Flow to drag modules, connectors, and other assets on to the canvas and create the relationships between them.

  • Use Global Elements to specify global elements used in multiple projects.

  • Use the Configuration XML to directly edit the XML of a project.

Verify the values set in the Listener component in the canvas:

  1. Double-click Listener to display its general configuration values.

  2. Click the edit icon Pencil on notebook next to the configuration name, and verify that the host is 0.0.0.0 and the port is 8081.

  3. Click Test Connection, and then click OK when a successful test connection is reported.

  4. Click OK to close the configuration.

    You return to the Global Configuration Elements tab.

  5. Click Message Flow to return to the canvas.

    The general configuration values for the Listener component are still displayed.

  6. Verify that Path is set as /api/*. This best practice ensures that all endpoints are formed like base-URI/api/endpoint.

In the next step, you create business logic and configure the elements you need for this API.

Step 3.3: Configure the API

Using the scaffolding provided by Studio, complete the app by configuring the /greeting endpoint module:

  1. In the Message Flow view, scroll down the canvas until you see the /greeting endpoint module:

    View of GET connector as created by scaffolding

  2. Right-click the Transform Message component and delete it.

  3. In Mule Palette, select Favorites to display a list of components, services, transformers, and routers.

  4. Click Set Payload to select it, and then drag it into the Process space, where you deleted Transform Message.

  5. Specify the logic that displays the data sent in a response from the hello-world-api API:

    1. Click the Set Payload transformer you just added to the flow.

      View of canvas and configuration dialog for Set Payload connector

    2. In the General tab, click the fx (function) button to deselect it. For this tutorial, you are hard-coding the value.

    3. In Settings, set the Value to Today the greeting is Hello.

  6. In the Anypoint Studio menu, select File > Save All.

  7. Click the Configuration XML tab on the canvas to review the XML generated by your work.

  8. Click Message Flow to set the canvas for the next step.

You now have a project that contains a simple Mule app that you can deploy to expose your API.

This API has been simplified compared to a typical production-ready API:

  • The payload (what’s delivered by the GET request) is hard coded in the project configuration. In a production-ready API the payload value would typically come from another source or be the function of a variable or DataWeave formula.

  • The best practice of separating the interface and implementation (business logic) into separate XML files is skipped in this tutorial for conciseness.

Step 3.4. Test the API

Run the Studio project that contains your API in the Mule runtime engine (Mule) and then test it with a third-party client:

  1. Right-click anywhere on the canvas and select Run project hello-world-app.

    The console opens within Studio and displays status messages as the app is deployed to Mule. The following message displays when the app containing your API is deployed to Mule:

    *******************************************************************************************************
    *            - - + APPLICATION + - -            *       - - + DOMAIN + - -       * - - + STATUS + - - *
    *******************************************************************************************************
    * hello-world-app                                  * default                        * DEPLOYED           *
    *******************************************************************************************************
  2. To test the app, open a REST client (Advanced Rest Client) and issue a GET request:

    http://localhost:8081/api/greeting

    Your REST client returns a 200 OK message and the body Today the greeting is Hello.

  3. Right-click in the canvas and select Stop project hello-word-app.

Step 3.5. Create a Business Group in Your Organization

Before you can publish your app to Exchange to share it with others, you must create a Business Group in your trial organization. Assets in Exchange must belong to a Business Group that is not the root Business Group.

If you’ve already created a business group in your organization, you can skip this step.

To create a Business Group:

  1. Log in to Anypoint Platform.

  2. Find Management Center on the landing page and click Access Management.

  3. In the navigation panel, click Business Groups.

  4. Click Create business group and enter My Top Business Group in Name.

  5. Select a business group you are the administrator for from the Parent business group dropdown.

  6. In Owner, enter your user name and select it when it is found.

  7. Select the Can create environments checkbox, and click Create.

Your new business group is listed underneath the organization name in the Access Management screen.

Step 3.6. Publish the API to Exchange

Publish the API to Exchange so it can serve as a template or example for others.

  1. In Studio, from Package Explorer, right-click the hello-world-app project.

  2. Select Anypoint Platform > Publish to Exchange.

    Dialog to publish API to Exchange

  3. Enter values for your project:

    • For User, select your username.

    • For Business group, select the My Top Business Group business group you created. You can’t use the root business group, which appears by default in this field. If your credentials expired, click Add account to add them again.

    • Leave Version with the default value.

    • Select Project type > Example.

  4. Click Next.

  5. You can add documentation, descriptions, and tags to help users find available assets in Exchange.

  6. Click Finish.

  7. Click Yes when asked to proceed.

    After the project is uploaded to Exchange, you are provided with a direct link to the project.

    Dialog showing link to API published in Exchange

  8. Click the link to view the project in Exchange:

    Home page of the hello-world-app you deployed

  9. Click Back to Assets List.

    You can now see the Exchange tile for the asset you just published.

  10. (Optional) Sign out of Exchange and search for the asset you just created.

    1. Click the tile for your hello-world-app.

    2. In the Asset Versions section, click the more (…​) menu and select Version detail. The HTTP Connector, Sockets Connector, and APIkit module are listed as dependencies. + Version details of app page as viewed in Exchange

  11. After you explore the published asset on Exchange, return to Studio and click OK to close the open dialog windows.

What’s Next

To finish developing the API, next you will add:

  • Validation

  • Event logging to support debugging

  • Error handling

  • A unit test to your API

You will publish the updated version of the app after it is complete.

Developer Deep Dive

To explore some of the capabilities of Anypoint Platform and Studio: