Import/Export APIs in WSO2 API Manager 2.6.0 with Configurations

Sumudu Sahan Weerasuriya
6 min readDec 11, 2020

--

Hello Geeks!!! ๐Ÿ˜‡๐Ÿ˜‡๐Ÿ˜‡

Today, Iโ€™ll explain how to use the WSO2 API Manager Command Line Interface (APIMCLI) with WSO2 API Manager 2.6.0 in Linux Operating System.

WSO2 API Manager is the worldโ€™s leading open-source, enterprise-grade API management platform for on-premises, cloud, and hybrid architectures.

WSO2 API Manager Command Line Interface allows you to manage multiple environments which are running in a same WSO2 API Manager instance. This allows you to manage APIs between your environments by import and export APIs.

Sounds pretty cool??? Ok, Letโ€™s start. ๐Ÿ˜‡๐Ÿ˜‡๐Ÿ˜‡

Getting started with API Manager

  1. Download the api-import-export war file which is compatible with API Manager 2.6.0 from [1]
  2. Go to the API Manager Home path.
  3. Navigate to the webapps directory (<APIM_HOME>/repository/deployment/server/webapps)
  4. Paste the downloaded war file.
  5. Start the API Manager 2.6.0 by executing below command.
sh wso2server.sh

By default, management console, dev portal, publisher UI and admin portal will access via 9443 port and you can invoke published APIs via 8243 port. If you apply a portOffset for the API Manager, this would be change according to the offset value.

Now you have a running instance of WSO2 API Manager 2.6.0 with the capability of import and export APIs by using CLI tool. Letโ€™s create an API.

  1. Go to the publisher portal (https://localhost:9443/publisher). You can log in by providing default username(admin) and password(admin)
  2. Click on New API
Create New API โ€” WSO2 API Manager 2.6.0

3. Select Design a New REST API and click on Start Creating.

Create REST API โ€” WSO2 API Manager 2.6.0

3. You will navigate to the Design tab.

  • Inside the General Details section, you can provide a name for the API, context, version, Description etcโ€ฆ
  • Inside the API Definition section, You can define API Resources.

I applied below values and create an API resource as below.

Design Tab โ€” WSO2 API Manager 2.6.0

In next page(Implement Tab), You can manage endpoint configurations, message mediation policies and CORS configurations.

I selected HTTP/REST Endpoint type and defined a production endpoint as below. (You can add advanced endpoint configurations to the endpoint by click on the settings icon near to the endpoint Test button)

Implementation Tab โ€” WSO2 API Manager 2.6.0

By default, advanced endpoint configurations shown as below.

No need to configure right now, we can do it by using APIMCLI Tool ๐Ÿ˜

Advanced Endpoint Configurations โ€” WSO2 API Manager 2.6.0

In Manage Tab, You can add API configurations, Throttling Settings,API gateways etcโ€ฆ For now, we just put a tick to a Subscription Tiers and click on Save & Publish.

Manage Tab โ€” WSO2 API Manager 2.6.0
  • For more information about Create and Publish an API, Please go through the documentation[2]

GREAT!!! Now you have published an API. ๐Ÿ˜‡๐Ÿ˜‡๐Ÿ˜‡ Lets move with the API Controller.

Getting started with API Controller

  1. Go to the official documentation link[3] and download the API Controller. for the Linux environment, I have downloaded the Linux 64-bit API Controller version.
  2. Extract the downloaded archive to a desired location.
  3. Navigate to the extracted location and start the CLI tool.
apimcli
APIMCLI Check

Now, we can manage APIs in different environments via apimcli. First of all, we can list all the environments that available by executing below command.

apimcli list envs

We can add a specific environment by executing below command. Please go through the documentation [4] to manage environments via apimcli.

apimcli add-env -n test_environment1 \
--registration https://localhost:9443/client-registration/v0.14/register \
--apim https://localhost:9443 \
--token https://localhost:8243/token \
--import-export https://localhost:9443/api-import-export-2.6.0-v17
Add Environment via APIMCLI
  • I have added 2 environments named test_environment1 and test_environment2

Export the API

Now we came to the exciting part of this article. As described above, we can manage APIs in different environments by using API Controller. We have already created an API via publisher portal. Lets start!!!

  1. Log into the environment.
apimcli login <environment_name> -k
Log into test_environment1

Now we have logged into the test_environment1 by providing default credentials.

2. Export the API.

You can export the API by executing below command. Please go through the documentation [5]

apimcli export-api -n <API-name> -v <version> -r <provider> -e <environment> -k
Export API

As you can see, I have exported the API from test_environment1 and that has been exported as a zip file. Inside the zip file, you can see the API information in api.json and api definition in swagger.json file.

3. Import the API.

You can import the exported API by executing below command. Please refer to the documentation [6].

  • We have exported the API from test_environment1. We can import that API into test_environment2.
apimcli import-api -f <file> -e <environment> --update -k
Import API
 --update - this flag is used if the API is available,this will update with new artifact. If not, then create a new API and import configurations automatically.

Now go to the publisher portal and then you can see, the exported API has been in the publisher portal successfully with defined configurations.

Now we can move with some advanced import statements. ๐Ÿ˜

3. Import the API with parameter file.

We can import the exported API artifact and override configurations of the API. Please refer to the documentation [7] for more information.

apimcli import-api -f Test_API_1.0.0.zip -e test_environment2 --params custom_params.yaml --update -k
Import API with parameter file

Below content included into the parameter.yaml file.

environments:
- name: test_environment2
endpointType: rest
endpointRoutingPolicy: load_balanced
loadBalanceEndpoints:
production:
- url: http://newendpoint.com
config:
factor: 1
retryTimeOut: 10000
retryDelay: 20000
retryErroCode: 101507
actionSelect: discard
actionDuration: 75000
suspendErrorCode: 101504
suspendMaxDuration: 25000
suspendDuration: 45000

And the result is,

  1. Endpoint Configurations.
Endpoint Configurations

2. Advanced Endpoint Configurations.

Advanced endpoint configurations

Parsing Advanced Endpoint Configurations.

  • apimcli 2.0.11 is supported advanced endpoint configurations as mentioned below.
factor
retryTimeOut
retryDelay
retryErroCode - this should be a valid error code
actionSelect - this value can be either discard or fault
actionDuration
suspendErrorCode - this should be a valid error code
suspendMaxDuration
suspendDuration
  • apictl (Latest API Tool) is supported only for below mentioned advanced endpoint configurations.
factor
retryTimeOut
retryDelay
  • All the error codes mentioned in the below image.

Thatโ€™s it!!!

Congratulations!!! Now You can export an API, import an API with/without paramters by using APIMCLI. ๐Ÿ˜‡๐Ÿ˜‡๐Ÿ˜‡

Happy coding!!!

[1] https://docs.wso2.com/download/attachments/28718536/api-import-export-2.6.0-v17.war?version=1&modificationDate=1600688632000&api=v2

[2] https://docs.wso2.com/display/AM260/Create+and+Publish+an+API

[3] https://docs.wso2.com/display/AM260/Initializing+projects+using+the+CLI+Tool#InitializingprojectsusingtheCLITool-Step3-RuntheCLItool

[4] https://docs.wso2.com/display/AM260/Initializing+projects+using+the+CLI+Tool#InitializingprojectsusingtheCLITool-Addinganenvironment

[5] https://docs.wso2.com/display/AM260/Migrating+the+APIs+and+Applications+to+a+Different+Environment#MigratingtheAPIsandApplicationstoaDifferentEnvironment-ExportinganAPI

[6] https://docs.wso2.com/display/AM260/Migrating+the+APIs+and+Applications+to+a+Different+Environment#MigratingtheAPIsandApplicationstoaDifferentEnvironment-ImportinganAPI

[7] https://docs.wso2.com/display/AM260/Building+CICD+Pipeline#BuildingCICDPipeline-SettingUpAdvancedEndpointConfigurations

--

--

Sumudu Sahan Weerasuriya
Sumudu Sahan Weerasuriya

Written by Sumudu Sahan Weerasuriya

Associate Technical Lead @ WSO2 | 2nd Runner-Up of WSO2 Certified Employee of the Year โ€” 2021 | 10X WSO2 Certified | BIT(UCSC) | DiHN | OCPJP

No responses yet