******************************************************************************************************* * - - + APPLICATION + - - * - - + DOMAIN + - - * - - + STATUS + - - * ******************************************************************************************************* * hello-world-app * default * DEPLOYED * *******************************************************************************************************
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.
-
Package Explorer helps you navigate the files created for each project.
-
The canvas is where you drag elements of your project and connect them, creating a flow.
-
The Mule Palette provides quick access to standard or custom assets such as connectors and modules.
-
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.
-
Open Studio, and then select Anypoint Studio > Settings.
-
In the Preferences window, click Anypoint Studio, and then Authentication.
-
Click Add and enter your Anypoint Platform username and password.
-
Click Sign in.
-
Optionally, click Yes in the Secure Storage - Password Hint Needed dialog.
-
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.
-
In Studio, select File > New > Mule Project.
-
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.
-
Click the green plus sign, and then select From Exchange.
-
Log in to your organization if needed.
-
Type
hello-world
in the search box. -
Click hello-world-api to select it.
-
Click Add to move it to the Selected modules list, and then click Finish.
-
-
-
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:
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:
-
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:
-
Double-click Listener to display its general configuration values.
-
Click the edit icon next to the configuration name, and verify that the host is
0.0.0.0
and the port is8081
. -
Click Test Connection, and then click OK when a successful test connection is reported.
-
Click OK to close the configuration.
You return to the Global Configuration Elements tab.
-
Click Message Flow to return to the canvas.
The general configuration values for the Listener component are still displayed.
-
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:
-
In the Message Flow view, scroll down the canvas until you see the
/greeting
endpoint module: -
Right-click the Transform Message component and delete it.
-
In Mule Palette, select Favorites to display a list of components, services, transformers, and routers.
-
Click Set Payload to select it, and then drag it into the Process space, where you deleted Transform Message.
-
Specify the logic that displays the data sent in a response from the
hello-world-api
API:-
Click the Set Payload transformer you just added to the flow.
-
In the General tab, click the fx (function) button to deselect it. For this tutorial, you are hard-coding the value.
-
In Settings, set the Value to
Today the greeting is Hello.
-
-
In the Anypoint Studio menu, select File > Save All.
-
Click the Configuration XML tab on the canvas to review the XML generated by your work.
-
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:
|
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:
-
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:
-
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 bodyToday the greeting is Hello
. -
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:
-
Log in to Anypoint Platform.
-
Find Management Center on the landing page and click Access Management.
-
In the navigation panel, click Business Groups.
-
Click Create business group and enter
My Top Business Group
in Name. -
Select a business group you are the administrator for from the Parent business group dropdown.
-
In Owner, enter your user name and select it when it is found.
-
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.
-
In Studio, from Package Explorer, right-click the
hello-world-app
project. -
Select Anypoint Platform > Publish to Exchange.
-
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.
-
-
Click Next.
-
You can add documentation, descriptions, and tags to help users find available assets in Exchange.
-
Click Finish.
-
Click Yes when asked to proceed.
After the project is uploaded to Exchange, you are provided with a direct link to the project.
-
Click the link to view the project in Exchange:
-
Click Back to Assets List.
You can now see the Exchange tile for the asset you just published.
-
(Optional) Sign out of Exchange and search for the asset you just created.
-
Click the tile for your hello-world-app.
-
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. +
-
-
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:
-
Transform data with DataWeave: Quickstart, Tutorial.
-
Structure Mule applications using flows, subflows, and asynchronous queues.
-
Define and manage application metadata (schemas): Metadata Editor.
-
Implement key-performance indicators using API Manager analytics.
-
Conditionally route events: choice router, scatter gather router.