Preface
We at Bytecoin value accessible education and shared knowledge - we have learned a lot of things that have given us a professional edge from open source. In the spirit of sharing we have decided to test out a new format of articles - short, clear and straight to the point descriptions of how things work. We hope these articles prove useful to some people and inspire them to learn more. Let us know what you think via Twitter or Reddit.
In the age of the Internet many users have seen the term API on various web services and questioned what it is. API is an umbrella term for various technology that allows easy access to diverse information and greatly expands the functionality of many software products. It can be considered the backbone of the crypto industry.
API is an acronym for Application Programming Interface, a way for different programs/services to exchange information; in other words, “it is a set of clearly defined methods of communication among various components of software”. API can take many forms, depending on what part of software it interacts with, including libraries/frameworks, operating system APIs, remote APIs and Web APIs.
Kinds of API
API takes different forms, including:
Libraries/frameworks
When building software, software engineers avoid reinventing the wheel by using frameworks and libraries - pieces of already existing code that perform certain tasks and are used interchangeably by many users. Technically speaking, this definition can be applied to many components of programming platforms, but programming languages cannot contain functionality for every possible need, so their built-in functionality is limited to a certain set of must-have commands. Libraries and frameworks serve several important purposes:
they greatly save time. Programmers don’t have to spend time making something that has been implemented earlier. For instance, if a programmer needs to perform complex math calculations or work with audio files, they would likely turn to a library to make their life easier;
they allow for high quality code. The more people use the code, the more reviewed it becomes - thanks to that libraries are becoming faster, easier to use and more reliable.
Establishing the difference between a library and a framework is a common debate among software engineers. According to prevailing opinion, a library is a set of functions that you call in your code, while a framework is a skeleton for a software product that you embed your code into.
Operating system APIs
Operating system APIs define the interface between an application and an operating system. Common examples here are POSIX, a set of APIs for a series of Unix-like operating systems, and Windows API, a Microsoft API for interacting with Windows OSes - almost all applications that run on Windows use WinAPI. It is quite typical for such APIs to be backward compatible, which means that apps that were written for an older version of an OS can run on a newer version of the OS.
Remote APIs
Remote APIs are a quite specific thing: these are APIs that work remotely, but appear local to the developer. They are an object-oriented equivalent of Remote Procedure Calls (RPC). Some examples of remote APIs are the Java remote method invocation or some of Google’s App Engine APIs.
Web APIs
When the term API is brought up, most people think about Web API, rather than APIs in general. A Web API is “a programmatic interface consisting of one or more publicly exposed endpoints to a defined request–response message system”.
In order to use such an endpoint one has to send a properly formatted request to an endpoint’s web address. While a lot of web APIs rely on JSON and XML message formatting, there is a trend in moving to REST-oriented API architecture.
Bytecoin’s API
Bytecoin’s integrators, miners and advanced users rely on Bytecoin’s API to interact with Bytecoin software. Bytecoin’s API is a web API, although it can be used locally as well. In order to run it, we have to send an HTTP request in JSON format to an active Bytecoin daemon. To make that happen, we employ the cross-platform cURL utility in the command line. Here’s what Bytecoin’s get_status request which returns miscellaneous info about the Bytecoin daemon looks like:
curl -s -u user:pass -X POST http://127.0.0.1:8081/json_rpc -H 'Content-Type: application/json-rpc' -d '{
"jsonrpc": "2.0",
"id": "1",
"method": "get_status"
}'
Here:
curl - a call for a widely used command-line utility that allows the sending of data to and from servers and computers in many different formats;
-s, -u, -X, -H, and -d are arguments for the curl command:
-s - silent mode, a setting specific to curl,
-u - allows us to specify user credentials that can be set up in the daemon,
-X - specifies what kind of request we are using (POST),
-H - allows us to specify headers; here we say that we pass data with the JavaScript Object Notation Remote Procedure Call,
-d - is followed by actual data we transmit in JSON format;
http://127.0.0.1:8081/json_rpc - is the IP address and port where we have the Bytecoin daemon running. You can treat the address and port like a house number and apartment number respectively. Experienced users will notice that 127.0.0.1 is an address called “localhost” - basically it means we are addressing the daemon that has been launched on the machine itself.
Immediately we receive a response with various data about blockchain the Bytecoin software has synced:
{
"id": "0",
"jsonrpc": "2.0",
"result": {
"top_block_hash": "c4a67767955cf26ec558c48166847da9386abba7b7a492efd524cd4efad5d339",
"transaction_pool_version": 2,
"outgoing_peer_count": 6,
"incoming_peer_count": 0,
"lower_level_error": "",
"top_block_height": 1780743,
"top_block_difficulty": 36992324923,
"top_block_cumulative_difficulty": 12692341676648833,
"top_block_timestamp": 1556736276,
"top_block_timestamp_median": 1556733459,
"recommended_fee_per_byte": 100,
"next_block_effective_median_size": 98958,
"recommended_max_transaction_size": 98958,
"top_known_block_height": 1785332
}
}
This response is one of many that any software can use to take advantage of Bytecoin’s functionality.
API is not only a technology that facilitates data exchange globally, but also an evolving frontier that by necessity stimulates innovation. We are excited to see it develop and take new forms.