DEV Community

Cover image for API vs CLI
Serverspace.us
Serverspace.us

Posted on • Updated on

API vs CLI

In this article, we will explain why we need API and CLI automation tools and how to use these tools using Serverspace 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.

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:

  >s2ctl server get-volume l1s12345 --volume-id 20210 


*s2ctl 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:

  id: 20210 
  name: boot 
  server_id: l1s12345 
  size_mb:  25600 
  created: '1970-01-01T0:00:00.0000000Z'

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

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 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:

-H "X-API-KEY: lmGwbvllpIqIrKROOCLgE5Z941MKP5EYf...gfXUTpuYRpNQkCqShmm6r"

Principle of operation

Mulesoft 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. You make an "order" with a request:

curl -X GET \
 https://api.serverspace.io/api/v1/servers/{server_id} \
  -H 'content-type: application/json' \
  -H 'x-api-key: lmGwbvllpIqIrKROOCLgE5Z941MKP5EYf...gfXUTpuYRpNQkCqShmm6r'

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.

  {
  "servers": [
    {
      "id":  "l1s2400",
      "location_id": "am2" ,
     "cpu": 1 ,
      "ram_mb": 1024 ,
      "volumes": [
        {
          "id": 2977 ,
          "name": "boot",
          "size_mb": 25600,
          "created": "2020-11-12T09:09:30.46252"
        },
        {
          "id": 2978,
          "name": "additional",
          "size_mb": 30720,
          "created": "2020-11-12T09:36:34.376165"
        }
      ],
      "nics": [
        {
          "id": 3024,
          "network_id": "l1n1",
          "mac": "ca:05:27:ff:56:89",
          "ip_address": "45.14.48.218",
          "mask": 28,
          "bandwidth_mbps": 50
        },
        {
          "id": 3025,
          "network_id": "l1n368",
          "mac": "ca:05:1a:50:f6:07",
          "ip_address": "10.0.0.1",
          "mask": 24,
          "bandwidth_mbps": 1024
        }
      ],
      "image_id": "CentOS-7.7-X64",
      "is_power_on": true,
      "name": "public-api",
      "login": "root",
      "password": "EuvzlqK6pv",
      "ssh_key_ids": [
        223,
        224
      ],
      "state":"Active",
      "created": "2020-11-12T09:09:54.6478655Z",
      "tags": [
        "production",
        "elastic"
      ]
    },
    {
      "id": "l1s2401",
      "location_id": "am2",
      "cpu": 2,
      "ram_mb": 3072,
      "volumes": [
        {
          "id": 2979,
          "name": "boot",
          "size_mb": 25600,
          "created": "2020-11-12T09:43:43.257493"
        }
      ],
      "nics": [
        {
          "id": 3026,
          "network_id": "l1n1",
          "mac": "ca:05:5e:6e:4d:36",
          "ip_address": "45.14.48.219",
          "mask": 28,
          "bandwidth_mbps": 100
        }
      ],
      "image_id": "CentOS-7.7-X64",
      "is_power_on": false,
      "name": "example",
      "login": "root",
      "password": "TzJYPpAV9P9",
      "ssh_key_ids": [],
      "state": "Active",
      "created": "2020-11-12T09:44:05.0545552Z",
      "tags": [
        "production",
        "gitlab"
      ],
      "application_ids": [
        "nginx",
        "gitlab",
        "wordpress"
      ]
    }
  ]
}


As a result, the task ID is returned, by which you can track the process of changing the VPS settings and its ID.

{
"task_id": "lt17499"
}


*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

Top comments (4)

Collapse
 
devsimc profile image
Simc Dev

Nice Explanation...
Keep posting this type content

Collapse
 
serverspace profile image
Serverspace.us

Thanks, we will🙌

Collapse
 
devsimc profile image
Simc Dev

Cheers

Collapse
 
samuelrivaldo profile image
Samuelrivaldo

Thanks 🙏