How to Publish RAML-Based APIs to Anypoint Exchange
Anypoint Exchange can be connected with Anypoint Studio to download and install connectors, consume APIs, and publish RAML-based APIs.
Join the DZone community and get the full member experience.
Join For FreeAnypoint Exchange is home to a complete listing of your connectors, APIs, templates, and examples. Anypoint Exchange is a hub where you can find connectors, APIs, examples, and much more. You can connect to Exchange with Anypoint Studio to download and install connectors, consume APIs, etc.
Publish APIs to Anypoint Exchange
Sign into Anypoint Mulesoft Platform and go to Exchange. Make sure you have access to Exchange. This can be verified under Access Management > Users.
Now, click Add Item and select REST API from the drop-down list. It will navigate you to another page.
Provide details like Item Name and Item ID (auto-generated). Other fields are optional. Click on Add Version at the end of the page. It will ask you to provide details like REST API Spec Version, API Version, API Portal, and REST API URL. You can get the API Portal and Rest API URL from your Live Portal. Click Done.
Finally, click Save new item. This will navigate you to a new page from there you can publish Rest API to Exchange.
Now, click Request to publish and it will publish your API to Exchange.
This is how you can publish your API and any other item to Exchange.
Consume REST APIs Using Anypoint APIKit
Go to Anypoint Studio and go to File > New > Mule Project and it will open a pop-up window. Provide the Project Name and check Add APIKit Component.
Click the ellipsis button near API Definition. It will show a drop-down list. Select Anypoint Platform. It will open new pop-up window.
Now, click Add Credentials and sign in with your Anypoint Platform account.
Once you will sign in, you can see a list of all APIs associated with your account. Select the API which you want to consume and Press OK. Finally, click on Finish.
This will generate various flows like API Main, API Console, Exception Strategy and various flows depending on HTTP method defined in your API.
API Main and Console Flow
The main flow performs the below function:
Exposes API using HTTP or Jetty.
Routes request between interfaces and backend flows depending on the HTTP request.
References exception strategies that produce HTTP-status-code responses.
Backend Flows
APIKit generates the backend flows for each request-action pairing in RAML.
Exception Strategy
APIKit generates the Exception Strategy flow for various response codes.
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/apikit http://www.mulesoft.org/schema/mule/apikit/current/mule-apikit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<http:listener-config name="api-httpListenerConfig" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<apikit:config name="api-config" raml="api.raml" consoleEnabled="false" doc:name="Router"/>
<flow name="api-main">
<http:listener config-ref="api-httpListenerConfig" path="/api/*" doc:name="HTTP"/>
<apikit:router config-ref="api-config" doc:name="APIkit Router"/>
<exception-strategy ref="api-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
</flow>
<flow name="api-console">
<http:listener config-ref="api-httpListenerConfig" path="/console/*" doc:name="HTTP"/>
<apikit:console config-ref="api-config" doc:name="APIkit Console"/>
</flow>
<flow name="put:/users/books:api-config">
<set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
</flow>
<flow name="put:/users/books/{bookTitle}:api-config">
<set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
</flow>
<flow name="delete:/users/books/{bookTitle}:api-config">
<set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
</flow>
<flow name="get:/users/authors:api-config">
<set-payload value="{
"authorID":1,
"authorName":"Michael Lynn",
"DOB":"21/04/1986",
"Age":30
}" doc:name="Set Payload"/>
</flow>
<flow name="get:/users/authors/{authorID}:api-config">
<set-payload value="{
"TotalBooks":30,
"Subject":"Maths,SCience",
"Publication":"Nirvana"
}" doc:name="Set Payload"/>
</flow>
<flow name="get:/users/books:api-config">
<set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
</flow>
<flow name="get:/users/books/{bookTitle}:api-config">
<set-payload value="{
"id": "SbBGk",
"title": "Stiff: The Curious Lives of Human Cadavers",
"description": null,
"datetime": 1341533193,
"genre": "science",
"author": "Mary Roach",
"link": "http://e-bookmobile.com/books/Stiff"
}" doc:name="Set Payload"/>
</flow>
<flow name="get:/users/books/{bookTitle}/author:api-config">
<set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
</flow>
<flow name="get:/users/books/{bookTitle}/publisher:api-config">
<set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
</flow>
<flow name="post:/users/authors:api-config">
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{
"message":"Author updated {but not really"
}" doc:name="Set Payload"/>
</flow>
<flow name="post:/users/books:api-config">
<set-payload value="#[NullPayload.getInstance()]" doc:name="Set Payload"/>
</flow>
<apikit:mapping-exception-strategy name="api-apiKitGlobalExceptionMapping">
<apikit:mapping statusCode="404">
<apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Resource not found" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="405">
<apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Method not allowed" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="415">
<apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Unsupported media type" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="406">
<apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Not acceptable" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="400">
<apikit:exception value="org.mule.module.apikit.exception.BadRequestException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Bad request" }" doc:name="Set Payload"/>
</apikit:mapping>
</apikit:mapping-exception-strategy>
</mule>
You can go through my articles about designing RAML-based APIs with API Manager and creating API portals for more information!
Now, you know how to publish APIs to Anypoint Exchange and consume them using APIKit!
Opinions expressed by DZone contributors are their own.
Comments