DEV Community

Cover image for CLI vs API: What's the Difference?
Dean Jones for Jamaica Homes

Posted on • Updated on

CLI vs API: What's the Difference?

****In this article, we will discuss why API and CLI automation tools are required, as well as how to use them using HostRooster as an example.

What is Command Line Interface (CLI)?
API (Application Programming Interface) is a method for one program to interact with another when elements from one application are used within another.

It's difficult to know what to do next with these tools based on their definitions, so let's take a closer look at each one in a detail.

1. Command Lind Interface
Before graphical user interfaces we're created, the only way to interact with a computer was via command language which was second nature to most people at that point. How many of you remember using ms-dos. Does not seem that long ago at all, but that was more than 20 years ago and despite the advent of graphical interfaces over the last couple of decades, the command line still remains an important tool today. But why? Well, i believe it is because it provides quick and direct access to the necessary functions of a computer and or server over and above the new graphical interfaces.

2. How the Command Lind Interface CLI's Operates
A users first needs to enter text commands into the interface command line and then sit and wait for the computer to respond. The tool then translates user requests into operating-system-friendly functions and responds to the user. Proving you have entered something the the computer can understand. You'll get back a positive response or command line error. The Command Line displays errors for incorrect command syntax, commands that are not valid for the active window, or commands that are lacking required parameter or have incorrect parameters. Commands in the CLI can be either one word or multiple lines, known as scripts.

For example, you can determine the volume of the created disk using the hostrooster command line interface as follows:

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

hostrooster tool *s2ctl allows you to control your infrastructure from the terminal.

As a result, you will receive detailed server information as foolows:

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

The Command Line Inferface tool can be used to automate routine tasks. You or your programming team can create a set of commands that the computer will execute at a specific time or when a specific situation occurs.

With quick commands and the input line, you can do everything that is available in the hostrooster control panel GUI.

What is API?
Well the wikipedia definition is a programming interface that allows two or more computer programs to communicate with one another. It is a type of software interface that provides a service to other programs. An API specification is a document or standard that describes how to build or use such a connection or interface.

The API essentially acts as a bridge between two programs, allowing them to communicate with each another via a set of protocols. Most developers do not need to understand how a third-party API work; but instead use the interface to communicate with other products and services.

An API can also be viewed as standing contract between two services that defines how the services interact with one another. The contract is written documentation that describes how developers should structure requests and responses. You can, for example, read the hostrooster API documentation, which includes a description of the API, request and response structures, error codes, and operation examples.

To begin, you must use authorization data to gain access to the API (API key, password, Secret key). To use the hostrooster API, for example, the user must first generate an API key for the project, which must then be passed with each request in the X-API-KEY header (exmaple):

-H "X-API-KEY: lmGwbvllpIqIrKROOCefefefeffKP5EYf...gfXUTpuYRefefefefShmm6r"

The operating principle
Mulesoft experts provide a good example of how the API works by comparing this tool to a restaurant. Consider sitting at a restaurant table and selecting a dish. The restaurant kitchen is a component of the "system" that will prepare the dishes you requested. However, there is insufficient link to send your order to the kitchen and deliver the food to your table. This is where the waiter (in our case, the API) comes in, taking your request or order and informing the kitchen (i.e. the system) of what to do. Then he provides you with the answer in the form of delectable food (example).

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

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

{
"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, allowing you to track the process of changing the VPS settings as well as its ID.

You can automate application interaction by using the APIs of various services, sending requests and parsing responses. The use of APIs can be automated to make software development processes more flexible, allowing programmers to concentrate on more important tasks.

Today, mosts applications makes use of the API. Web applications use APIs to connect user-facing front ends with all-important back end functionality and data. Streaming services like Spotify and Netflix use APIs to distribute content. Automotive companies like Tesla send software updates via APIs. Others use APIs to unlock car data for third-parties. An interesting fact is that if all APIs in the world were to be removed one day, almost all services and most applications would cease to function!

Both tools (CLI and API) enable you to automate the process of working with hostrooster virtual machines and receive quick responses to requests without having to navigate to the control panel. What you can do with these tools is as follows and more:

  • control virtual machines
  • scale virtual machine configuration
  • domain name creation and deletion
  • connect networks
  • configure NAT and Firewall rules, and create and delete border gateways
  • view project details in the control panel
  • view server information and control server power
  • manage SSH keys
  • control snapshots

Top comments (0)