Implement API using Swagger or OAS in MuleSoft
Since MuleSoft has enabled the support for Swagger 3.0 (now called as Open API specification or OAS) in Design time and runtime, it is now easy to build APIs using Swagger. Swagger has much wider support and acceptance than RAML in API world. Hence it is better to create your API specification using swagger now.
In this tutorial we will implement first API using swagger in MuleSoft.
You can use https://editor.swagger.io/ also to create the swagger specifications of your API.
We will not be explaining much about OAS/Swagger in this tutorial. Please refer to https://swagger.io/specification/ to know more about swagger/OAS.
Here is how you can build your first API using Swagger in MuleSoft
- Create the API specification
Go to Design Center> New API Spec and select OAS 3.0 YAML. We will make use of YAML in this tutorial.
Create a simple swagger API spec as below. it has just one resource mulesy with one method POST with request and response defined as YAML
- Publish the API to Exchange
Once done with creating API specification, it is time to publish the API to exchange now.
- Implement the API
Once the API is published, we will implement the API using Anypoint studio.
Create a new project in Anypoint Studio. Use the Import from exchange to import the API specification as below
Anypoint Studio will generate the skeleton code for you from OAS as below. Update the implementation to include a response as per the OAS
- Deploy the API
Run the project to deploy the API locally. it should start without any issue as below
- Test the API
Test the API using postman or any other tool as below
We just learnt to implement APIs using Swagger. Please do let us know if you face any issue in implementing OAS for your APIs. Happy Learning and Sharing!
Attachment: API specification
Can’t copy yaml from image. Thanks
What is – “Update the implementation to include a response as per the OAS” Thanks