How to create Custom Actions using Swagger Files
IBM® webMethods Integration supports 250+ connectors to help you create smart integrations faster. Further, it allows you to create custom actions/triggers using the Connector Builder or by importing your existing Swagger files.
In this tutorial, we will understand how to create a custom action using a Swagger file.
In order to create a custom action, install the IBM webMethods CLI Connector Builder
Installing Connector Builder
To install the Connector Builder, follow the instructions given below:
-
Install node module by running the following command:
npm i @webmethodsio/wmiocli -g
-
Login to IBM webMethods Integration by using the following command:
wmio login
You will be prompted to enter your tenant URL, email address associated with the tenant, and the tenant developer key. Successful login indicates that the Connector Builder is installed on your system.
-
Create a new Connector by running the following command:
wmio init
You will be prompted to provide a name for the connector. Once this is done, an action scaffolding will be automatically created in your current directory.
Importing Swagger file
Once you have installed the Connector Builder, the next step is to create a custom action for the application of your choice. To do so, follow the instructions given below:
-
Navigate to the newly created scaffolding and run the following command:
wmio swagger <swagger_file>
This command is responsible for importing all API calls from the specified swagger file and then creating actions.
Let’s now understand how to do this with the help of an example.
Suppose we have a swagger file -
https://api.apis.guru/v2/specs/box.com/2.0/openapi.json
- and, we want to create actions by importing all API calls from this file.So, we will now run the below command to achieve the required result:
wmio swagger https://api.apis.guru/v2/specs/box.com/2.0/openapi.json
-
Next, create authorization for the custom action.
If the service for which you are trying to create an action has an authorization type set to OAUTH, you will be prompted to confirm the OAuth provider.
In our example, the authorization type is set to OAUTH and hence, we get the following message on the command window screen:
- Enter ‘N’ if you want to proceed without creating authorization for the action. On clicking ‘N’, the relevant action will be created automatically.
- Enter ‘Y’ to confirm the OAuth provider. If the exact match of the OAuth provider is found, your custom action will be created automatically.
- In case, the exact match of the OAuth provider is not found, you will be prompted to either select the required OAuth provider from the list of available providers or proceed without creating an authorization.
On selecting ‘Other’, a list of available OAuth providers will be displayed on the screen. Select the required OAuth provider from the given list.
To proceed without creating an authorization, select 'Skip'. the required actions will be created automatically.
- Once you have selected the relevant OAuth provider, your actions will be created automatically in the /action directory.
wmio auth
To learn how to add an authorization using this command, Authentications
Testing the action
Testing the custom action is an optional step. However, it is recommended to perform this step to ensure that the code you have written is working as expected.
We will now understand how to test the action with the help of an example. Suppose we want to test an action that is created using the example swagger file, __folders__folder_id__itemsget. To do this, follow the steps given below:
-
Navigate to /action/v1/__folders__folder_id__itemsget file.
-
You will now see the code structure of the action.
To test the action, add mock_input object below the output object as shown below:
With this, the action is ready to be tested. Now you can run the command - wmio
test
- to test the action.
A file named auth.conf will be automatically created at the root level of the current directory. This file contains an object with Authorization details (as shown below) of the action that needs to be tested. The Authorization details will be used by CLI while testing the action.
Ensure that the index.json file contains only those actions in the array that you would want to test.
In case the index.json file contains an empty array of actions, then only the passed access token will be validated. The validation will be performed using the validate function present in the authentication.js file.
Deploying the action
Once you have tested your action, the next step is to deploy it. To do so, run the following command:
wmio deploy
With this, your action will be available for you to use locally. Refresh the browser to see the deployed action added in the Connectors panel under the Services tab.
Error Description
Here is a list of some common errors you might face while deploying the action to your tenant.
-
Title key missing from the input schema properties
This will throw the following error:
-
Type key is missing from the input schema properties
This will throw the following error:
-
Array items missing from the input schema properties
This will throw the following error:
Executing the action
The process of executing the action remains the same as that of any other action on IBM webMethods Integration.
Prerequisites for executing the action
Before you execute any newly created action, ensure that:
-
The base URL is correct.
To verify the base URL, navigate to the config.js file present inside the V1 folder of the action directory.
-
You have to set the query, header, and params required by the service provider in the authhelper.js file present inside the action directory.
Editing an existing custom action
You can edit an existing custom action created through a Swagger file. To do so, follow the steps given below:
-
Navigate to the custom action file you want to edit inside the app folder. Let’s say we want to edit the ‘authorizeget’ custom action that we created using the example swagger file. So, navigate to /action/v1/_authorizeget.js file. This JSON file contains the code of the action you want to edit.
-
Make changes to this code based on your requirements and save the file.
Deleting an existing custom action
You can delete an existing custom action created using a Swagger file. To do so, follow the steps given below:
-
To delete an action, run the following command:
wmio pull
-
Select whether you want to remove an action or a trigger.
-
On selecting ‘action’, a list of actions will be displayed on the screen. Select the action(s) you want to delete.
-
The specified action(s) will be deleted from the connector. Simultaneously, the name of this action will also be deleted from the index.json file.
Help
Given below is the list of all commands used to create a custom action using a Swagger file:
- npm i @webmethodsio/wmiocli -g: Installs node module to create and deploy connectors on IBM webMethods Integration
- wmio login: Configures the deploy key and logs you into IBM webMethods Integration
- wmio init: Initializes a new IBM webMethods Integration app in your directory
- wmio swagger <swagger_file>: Imports all APIcalls from the specified swagger file as actions in the connector
- wmio auth: Creates an authentication scaffolding in the app directory
- wmio test: Tests and runs the created custom connectors
- wmio deploy: Builds app and deploys it to IBM webMethods Integration
- wmio pull: Deletes an existing custom connector