WSO2 API Cloud projects initialization and deployment using the CLI Tool
Tags: api, cloud, devops, beginners
Published: 2021-01-23 07:41:32

**WSO2 API Cloud delivers an enterprise-ready solution for creating, publishing, and managing all aspects of an API and its lifecycle. To register and get started with WSO2 API Cloud, go to** http://wso2.com/cloud/api-cloud/

You can take a look at the [WSO2 API Cloud Documentation](https://cloud.docs.wso2.com/en/latest/) to quickly try out designing , creating and publishing APIs **from the Web UI**, subscribing to it **from the Web UI** of the WSO2 API Cloud

#### In this article, we are focusing on Designing, Initializing and deploying APIs **from the command line** using the **APIMCLI tool**. 

👉 **Pre-requisites**

**1.**Download API Controller based on your preferred platform (i.e., Mac, Windows, Linux).

* [For MacOS](https://docs.wso2.com/download/attachments/103334679/apimcli-2.0.13-macosx-x64.tar.gz)
* [For Linux 32-bit](https://docs.wso2.com/download/attachments/103334679/apimcli-2.0.13-linux-i586.tar.gz)
* [For Linux 64-bit](https://docs.wso2.com/download/attachments/103334679/apimcli-2.0.13-linux-x64.tar.gz)
* [For Windows 32-bit](https://docs.wso2.com/download/attachments/103334679/apimcli-2.0.13-windows-i586.zip)
* [For Windows 64-bit](https://docs.wso2.com/download/attachments/103334679/apimcli-2.0.13-windows-x64.zip)

**2.**Extract the downloaded archive containing the CLI Tool to a preferred location and navigate to it from the terminal (using cd ).

**3.**Navigate to the working directory where the executable CLI Tool resides.

**4.**Execute the following command to start the CLI tool.

```
./apimcli
```

**5.**Add the location of the extracted folder to your system's $PATH variable to be able to access the executable from anywhere.

```
apimcli
```

<hr>

#### 1. Adding the cloud gateway environment

```sh

apimcli add-env -n wso2apicloud \
                      --registration https://gateway.api.cloud.wso2.com/client-registration/register \
                      --apim https://gateway.api.cloud.wso2.com/pulisher \
                      --token https://gateway.api.cloud.wso2.com/token \
                      --import-export https://gateway.api.cloud.wso2.com/api-import-export \
                      --admin https://gateway.api.cloud.wso2.com/api/am/admin/ \
                      --api_list https://gateway.api.cloud.wso2.com/api/am/publisher/apis \
                      --app_list https://gateway.api.cloud.wso2.com/api/am/store/applications


```

######Make sure the environment is configured by this command :
```
apimcli list envs
```
#### 2. Create a new API project

Usage: 

You can follow one of these example methods to initialize a project.                                                                                                                

`apimcli init [project path] [flags]`
                                                                                                                                                                                                           Examples:                                                                                                               


```sh
# Initialize with default swagger.yaml file and default API definition (You have to replace the generated API definition and Swagger with your actual values)
apimcli init SampleStore
# Initialize with your actual swagger file path
apimcli init SampleStore --oas petstore.yaml
# Initialize with your actual swagger file URL
apimcli init SampleStore --oas https://petstore.swagger.io/v2/swagger.json
# Initialize with your actual swagger file and API Definition file
apimcli init SampleStore --oas ./swagger.yaml -d definition.yaml 

```

#####A project folder with the following structure will be created in the given directory.

├── api_params.yaml
├── Docs
│   └── FileContents
├── Image
├── Meta-information
│   ├── api.yaml
│   └── swagger.yaml
├── README.md
└── Sequences
│    ├── fault-sequence
│    ├── in-sequence
│    └── out-sequence


I have attached 2 sample files for **api.yaml** and **swagger.json** for your reference. 

You can get the hands-on experience by replacing below files with generated Meta-information in your directory and move forward with the article.

* [api.yaml](https://gist.github.com/mihinduranasinghe/6285853c3f186b107e9bddc4d550e16d)
* [swagger.json](https://gist.github.com/mihinduranasinghe/0b083375f1fea5bcc03f7737804dc933)

If you want to learn more on api.yaml configuration, here you can find out [gist](https://gist.github.com/kasvith/01e704611b6c301f470ab0e3b5cb0607)

<hr>

#### 3. Deploying the API project to a targeted tenant in API Cloud 

* First of all you need to log into your targeted tenant in API Cloud by providing targeted tenant username & password .

```sh
#apimcli login <Environment> -u <Username> -p <Password> 
apimcli login wso2apicloud -u mihindu@wso2.com@development -p PASS54687 -k

```
Also you can login like this :

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8ve1hk5rf89b9o72tubg.png)

* After logging in to the targeted tenant, you can import the API using the following command.

```sh

apimcli import-api -f ./path/SampleStore -e wso2apicloud --preserve-provider=false --update --verbose -k


```
#####Make sure the API project is deployed successfully by this command :

```sh
# apimcli list apis -e <environment> -k
apimcli list apis -e wso2apicloud -k
```

#####Here the deployed APIs are listed:


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2hvorase7b82ym5qe8j4.png)

##### You can log into the WSO2 API cloud portal and see the project is deployed successfully.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6agzdd1b569mroryssh5.PNG)

<hr>

### **How to export the API Project from the logged in tenant from the CLI** ?

```sh

# # apimcli export-api -n <API-name> -v <version> -r <provider> -e <environment> -u <username> -p <password> -k
# # apimcli export-api --name <API-name> --version <version> --provider <provider> --environment <environment> --username <username> --password <password> --insecure
apimcli export-api -n SampleStore -v 1.0.0 -r mihindu@wso2.com@development -e wso2apicloud -k


```
##### Once you exported successfully you will receive a response with the exported directory in your local machine.
 
Response :

```
Successfully exported API!
Find the exported API at /Users/kim/.wso2apimcli/exported/apis/dev/SampleStore_1.0.0.zip
```


### **DO YOU KNOW** 
* How to update an API Cloud Project **Thumbnail** from the **CLI** ?
* How to add **Mediation Sequences** for the API Cloud Project from the **CLI** ?
* How to add **API Scopes** from the **CLI** ?
* How to add **Documents** for the API Cloud Project from the **CLI** ?
* How to **invoke an API** in WSO2 API Cloud with a generated API Token, from the **CLI** ?

<hr>

👉 What's Next?

  * [These sections will be covered in-detail in the next article.](https://dev.to/mihinduranasinghe/wso2-api-cloud-adding-mediation-sequences-documents-and-api-thumbnails-from-cli-tool-3l5h)

### **Thank You**

##### ***Hope you all enjoyed and learned something from this. Let me know your comments and suggestions in the discussion section.***

👉 Visit me - [https://mihinduranasinghe.com/](https://mihinduranasinghe.com/)