Discover our knowledge. Read our blogs!

Learn more

We build all our solutions with WSO2 and we are proud that we are WSO2 Premier Certified Integration Partner and Value-Added Reseller.

Learn more

Working with the command line WSO2 API Manager tool

8 min read

Working with the command line WSO2 API Manager toolThe latest version of WSO2’s API manager, version 2.5.0, has an additional tool to download that will make it easier for you to copy an API or an application from one environment to the other. People who are knowledgeable about the API manager can say: hang on, don’t we have that already? And they’re partly right. There is a tool that will allow you to import or export an API that has been created on the API manager. It is called API – import – export and is actually a war file that is deployed on the API manager itself in the webapps directory. My colleague Daniel Stevanović wrote a blog about it way back in 2015. 

The command line tool is more than that. To be precise it actually uses the latest version, tied to the version of the WSO2 API manager, of that very same war file. The addition is that you now have a more elaborate solution where you can define environments that you would like to copy to. In this blog, we are going to look at how we can define two environments, let’s call them development and test and using the command line tool from one environment to the other.

Setting up the environment

So, let’s define the environment that were going to use. It consists of two API managers on the same server which means that you need a port offset for instance of one the second API manager to let them coexist on the same server.

We are not going to make any other changes the out-of-the-box API managers and the Port offset that we just described. On the first API manager, the one with the zero-port offset, we are going to deploy the standard API: the Pizzashack. Reason for this is that it’s packaged with the application requires no other installation, just click the button for the deployment of the sample API.

Make sure that you actually deploy the WSO2 API Manager 2.5.0 Import / Export WAR file. You can  download it from this location.

We are not going to do a very detailed explanation of how you download and install the API manager. If you’re looking for information about that please see our blog on installing WSO2 products.

I’ve taken the API manager using WUM an updated it to the latest version available. It made to directories:

  1. APIM-DEV
  2. APIM-TEST

and make sure that the second API manager has a port offset of one.

I’ve downloaded the apimcli tool in the same directory where I created the two API subdirectories. I’ve executed the tool once to create the yaml configuration files.

Lets add the two environments using the command-line. This is for a localhost deployment on Linux:

./apimcli add-env -n APIM-TEST --registration https://localhost:9444/client-registration/v0.13/register --apim https://localhost:9444 --token https://localhost:8244/token --import-export https://localhost:9444/api-import-export-2.5.0-v1 --admin https://localhost:9444/api/am/admin/v0.13     --api_list https://localhost:9444/api/am/publisher/v0.13/apis --app_list https://localhost:9444/api/am/store/v0.13/applications./apimcli add-env -n APIM-DEV --registration https://localhost:9443/client-registration/v0.13/register --apim https://localhost:9443 --token https://localhost:8243/token --import-export https://localhost:9443/api-import-export-2.5.0-v1 --admin https://localhost:9443/api/am/admin/v0.13     --api_list https://localhost:9443/api/am/publisher/v0.13/apis --app_list https://localhost:9443/api/am/store/v0.13/applications


For Windows the  ./ is not necessary.

The file looks like this

wso2apimcli - working with the command line in api manager

Start both instances using the shell of bat file and login to the APIM-DEV. Deploy the sample PizzaShack API.

API Publisher-working with the command line api manager tool

API Publisher listing-working with the command line in api manager tool

WSO2 API publisher - all API's

Let’s list the environments. As you can see the two environments are present.

environments - working with the command line API manager tool

We will now export the API using the apimcli tool:

apimcli export-api -n PizzaShackAPI -v 1.0.0 -r admin -e APIM-DEV -u admin -p admin


We however encounter an issue with regards to the self-signed certificate. A response is given that the certificate is signed by unknown authority. We now make the tool using the -k or --insecure setting. The --verbose setting gives more information about the actions of the tool. 

apimcli export-api --verbose –-insecure -n PizzaShackAPI -v 1.0.0 -r admin -e APIM-DEV -u admin -p admin 


Successfully exported API!

If you have a CA that is not trusted yet, use the following two commands to add the CA to your OS. This is the command for Centos7:

sudo cp cacert.pem /etc/pki/ca-trust/source/anchors/ 
sudo update-ca-trust


Find the exported API at:

.wso2apimcli\exported\apis\APIM-DEV\PizzaShackAPI_1.0.0.zip in windows.


Linux has a different directory where is stored and created the exported APIs.

/opt/wso2/.wso2apimcli/exported/apis/APIM-DEV/PizzaShackAPI_1.0.0.zip

 

Let’s verify the export. There are two directories under exported:

  1. apis
  2. apps

And indeed, we find a zip file with the exported PizzaShackAPI.

zip PizzaShackAPI

Import the API

We are going to import the API into APIM-TEST.

For Windows this is the command from the directory where

apimcli import-api --verbose –-insecure -f APIM-DEV\PizzaShackAPI_1.0.0.zip -e APIM-TEST -u admin -p admin

 
Linux uses this command, note that this includes the addition of the CA cert.

./apimcli import-api --verbose -f /opt/wso2/.wso2apimcli/exported/apis/APIM-DEV/PizzaShackAPI_1.0.0.zip -e APIM-TEST -u admin -p admin

 

Questions regarding this blog? Don't hesitate to leave a comment below.

 

Care to share?
   
Picture of Rob Blaauboer
Published March 7, 2019

Rob Blaauboer

Rob is a Senior Business Consultant and Solution Architect with more than twenty years experience. In addition to his work he is an active blogger working on a number of articles on the ‘Internet of Things’ and a WSO2 ‘Getting Started with …’ series (WSO2 tutorial) in which he talks about WSO2 components and their purpose especially aimed at non technical readers. Rob is a WSO2 expert and official WSO2 trainer.

Responses

Stay up to date with the latest articles