API vs CLI
Tags: api, programming, devops, cloud
Published: 2022-07-28 15:58:00

In this article, we will explain why we need API and CLI automation tools and how to use these tools using [Serverspace](https://serverspace.io/) as an example.

### What is CLI?

CLI (Command Line Interface) is a tool for the user to work with the program using commands. 

### What is an API?

API (Application Programming Interface) is a way for one program to interact with another program when elements of one application are used inside another application.

From the definitions, it is difficult to understand what to do next with these tools, so let's take a closer look at each tool.

<a href="https://serverspace.io/ref/344960"><img src="https://static.serverspace.io/referral/banners/Serverspace_600h90.png"></a>

### [CLI](https://serverspace.io/services/cli/)

Before the advent of graphical interfaces, the only way to interact with a computer was through command language. Despite the advent of graphical interfaces, today the command line is still an important tool, as the command line gives quick access to the necessary functions of a computer / server.

### How the CLI works

The user enters text commands into the interface line and waits for a response from the computer. At this time, the tool translates requests into functions understandable to the operating system and then issues a response to the user. Commands in the CLI can be either one word or multiple lines, known as scripts.

For example, using the Serverspace command line interface, you can find out the volume of the created disk:

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">  >s2ctl server get-volume l1s12345 --volume-id 20210 </span>
</code></pre>
</div>
</div>
*<strong>s2ctl</strong> is the name of the Serverspace utility that allows you to control your infrastructure in the terminal.

As a result, you will get detailed information about the server:

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">  id: 20210 
  name: boot </span>
<span class="nb">  server_id:</span> <span class="s1">l1s12345 </span>
<span class="nb">  size_mb:</span> <span class="s1"> 25600 </span>
<span class="nb">  created:</span> <span class="s1">'1970-01-01T0:00:00.0000000Z'</span>
</code></pre>
</div>
</div>

The CLI tool is useful for automating routine tasks. You or your programming team can write a list of commands that the computer will execute at a time or situation you choose.

The CLI allows you to do everything that is available in the Serverspace control panel GUI with quick commands and the input line.

### [API](https://serverspace.io/services/api/)

The API acts as an intermediary between two programs and allows them to interact with each other using a certain set of protocols. Developers don't need to know how a third-party API is implemented; programmers can use the interface to communicate with other products and services.

An API can be thought of as a contract between two services that defines how the services work with each other. The contract is documentation that contains information about how developers should structure requests and responses. For example, you can read the  [Serverspace API](https://serverspace.io/services/api/) documentation, where you will find a description of the API, request and response structures, error codes, and examples of operations.

To get started, you need to access the API using authorization data (API key, password, Secret key). For example, to work with the Serverspace API, the user must create an API key for the project, which must then be passed with each request in the X-API-KEY header:

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">-H "X-API-KEY: lmGwbvllpIqIrKROOCLgE5Z941MKP5EYf...gfXUTpuYRpNQkCqShmm6r"</span>
</code></pre>
</div>
</div>

### Principle of operation

[Mulesoft](https://www.mulesoft.com/resources/api/what-is-an-api) specialists give a good example of how the API works - this tool is compared to a restaurant. Imagine you are sitting at a table in a restaurant and choosing a dish. The restaurant kitchen is part of the "system" that will prepare the dishes you ordered. But there is not enough link to pass your order to the kitchen and deliver the food to your table. This is where the waiter comes in, (in our case the API), who takes your request or order and tells the kitchen (i.e. the system) what to do. Then he gives you the answer in the form of delicious food.

For example, you want to get detailed information about a [server](https://serverspace.io/services/api/). You make an "order" with a request:

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">curl -X GET \
 https://api.serverspace.io/api/v1/servers/{server_id} \
  -H 'content-type: application/json' \
  -H 'x-api-key: lmGwbvllpIqIrKROOCLgE5Z941MKP5EYf...gfXUTpuYRpNQkCqShmm6r'</span>
</code></pre>
</div>
</div>

The Serverspace API accepts this request, refers it to the "kitchen" (sends the request to the system) and returns a response in the form of information about the server.

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">  {
  "servers": [
    {</span>
      <span class="nb">"id":</span> <span class="nx"> "l1s2400"</span>,
      <span class="nb">"location_id":</span> <span class="nx">"am2"</span> <span class="nb">,
     "cpu":</span> <span class="s1">1</span> <span class="nb">,
      "ram_mb":</span> <span class="s1">1024</span> <span class="nb">,
      "volumes": [
        {
          "id":</span> <span class="s1">2977</span> <span class="nb">,
          "name":</span> <span class="nx">"boot"</span><span class="nb">,
          "size_mb":</span> <span class="s1">25600</span><span class="nb">,
          "created":</span> <span class="nx">"2020-11-12T09:09:30.46252"</span><span class="nb">
        },
        {
          "id":</span> <span class="s1">2978</span><span class="nb">,
          "name":</span> <span class="nx">"additional"</span><span class="nb">,
          "size_mb":</span> <span class="s1">30720</span><span class="nb">,
          "created":</span> <span class="nx">"2020-11-12T09:36:34.376165"</span><span class="nb">
        }
      ],
      "nics": [
        {
          "id":</span> <span class="s1">3024</span><span class="nb">,
          "network_id":</span> <span class="nx">"l1n1"</span><span class="nb">,
          "mac":</span> <span class="nx">"ca:05:27:ff:56:89"</span><span class="nb">,
          "ip_address":</span> <span class="nx">"45.14.48.218"</span><span class="nb">,
          "mask":</span> <span class="s1">28</span><span class="nb">,
          "bandwidth_mbps":</span> <span class="s1">50</span><span class="nb">
        },
        {
          "id":</span> <span class="s1">3025</span><span class="nb">,
          "network_id":</span> <span class="nx">"l1n368"</span><span class="nb">,
          "mac":</span> <span class="nx">"ca:05:1a:50:f6:07"</span><span class="nb">,
          "ip_address":</span> <span class="nb">"10.0.0.1"</span><span class="nb">,
          "mask":</span> <span class="s1">24</span><span class="nb">,
          "bandwidth_mbps":</span> <span class="s1">1024</span><span class="nb">
        }
      ],
      "image_id":</span> <span class="nx">"CentOS-7.7-X64"</span><span class="nb">,
      "is_power_on":</span> <span class="o">true</span><span class="nb">,
      "name":</span> <span class="nx">"public-api"</span><span class="nb">,
      "login":</span> <span class="nx">"root"</span>,
      "password":</span> <span class="nx">"EuvzlqK6pv"</span><span class="nb">,
      "ssh_key_ids": [</span>
        <span class="s1">223</span>,
        <span class="s1">224</span>
      ],
      "state":<span class="nx">"Active"</span><span class="nb">,
      "created":</span> <span class="nx">"2020-11-12T09:09:54.6478655Z"</span><span class="nb">,
      "tags": [</span>
        <span class="nx">"production",
        "elastic"</span>
      ]
    },
    {
      "id": <span class="nx">"l1s2401"</span><span class="nb">,
      "location_id":</span> <span class="nx">"am2"</span><span class="nb">,
      "cpu":</span> <span class="s1">2</span><span class="nb">,
      "ram_mb":</span> <span class="s1">3072</span><span class="nb">,
      "volumes": [
        {
          "id":</span> <span class="s1">2979</span><span class="nb">,
          "name":</span> <span class="nx">"boot"</span><span class="nb">,
          "size_mb":</span> <span class="s1">25600</span><span class="nb">,
          "created":</span> <span class="nx">"2020-11-12T09:43:43.257493"</span><span class="nb">
        }
      ],
      "nics": [
        {
          "id":</span> <span class="s1">3026</span><span class="nb">,
          "network_id":</span> <span class="nx">"l1n1"</span><span class="nb">,
          "mac":</span> <span class="nx">"ca:05:5e:6e:4d:36"</span><span class="nb">,
          "ip_address":</span> <span class="nx">"45.14.48.219"</span><span class="nb">,
          "mask":</span> <span class="s1">28</span><span class="nb">,
          "bandwidth_mbps":</span> <span class="s1">100</span><span class="nb">
        }
      ],
      "image_id":</span> <span class="nx">"CentOS-7.7-X64"</span><span class="nb">,
      "is_power_on":</span> <span class="nx">false</span><span class="nb">,
      "name":</span> <span class="nx">"example"</span><span class="nb">,
      "login":</span> <span class="nx">"root"</span><span class="nb">,
      "password":</span> <span class="nx">"TzJYPpAV9P9"</span><span class="nb">,
      "ssh_key_ids": [],
      "state":</span> <span class="nx">"Active"</span><span class="nb">,
      "created":</span> <span class="nx">"2020-11-12T09:44:05.0545552Z"</span><span class="nb">,
      "tags": [</span><span class="nx">
        "production"</span><span class="nb">,</span><span class="nx">
        "gitlab"</span><span class="nb">
      ],
      "application_ids": [</span><span class="nx">
        "nginx"</span>,<span class="nx">
        "gitlab"</span>,<span class="nx">
        "wordpress"</span><span class="nb">
      ]
    }
  ]
}</span>
</code></pre>
</div>
</div>
As a result, the task ID is returned, by which you can track the process of changing the VPS settings and its ID.
<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">{
"task_id":</span> <span class="nx">"lt17499"</span>
}
</code></pre>
</div>
</div>
*Using the APIs of several services, sending requests and parsing responses, you can set up automatic application interaction. Automating the use of APIs can make software development processes more flexible, allowing programmers to focus on more important tasks.

Today, every second application uses the API from an application for finding out the calendar date and weather to an application for setting up stock trading. An interesting fact, if one day all the APIs in the world are removed, then almost all services and most applications will stop working!

Both tools (CLI and API) allow you to automate the process of working with Serverspace virtual machines and get quick responses to requests without going to the control panel. Here is what you can do with these tools:
* manage virtual machines
* view server details and manage server power
* scale virtual machine configuration
* connect networks
* manage SSH keys
* manage snapshots
* create and delete border gateways, configure NAT and Firewall rules
* view project details in the control panel
* create and delete a domain name