APIs are one of the most significant parts of software development, every developer must be well versed with them. I have been working as a golang backend engineer for about a year now, have constructed plenty of APIs for various projects.
Recently, I came through an interesting session on APIs at Crio: Learn by Doing and am sharing its notes with DevCommunity. Let's get it started!
API stands for Application Programming Interface, is a set of functions that allows applications to access data and interact with external software components, operating systems, or microservices.
In simple words, an API is like a waiter in a restaurant. You don’t go into a cafe and walk straight into the kitchen to tell the chef what you wanna eat. The waiter does that for you, and that’s exactly what an API is - with the client being you, the customer, and any resource that can send data, being the chef.
Security: While using an API, client-side and server-side application works independently, and they communicate through API, so if anyone tries to hack into the client-side app, the server stays unaffected and secured.
Flexibility: APIs allow content to be embedded from any site or application more easily. For e.g. an API written in GO can be used by Node.js/Django.
Efficiency: When access is provided to an API, the content generated can be published automatically and is available for every channel. It allows it to be shared and distributed more easily.
Innovation: With an API, an application layer can be created which can be used to distribute information and services to new audiences which can be personalized to create custom user experiences.
REST stands for Representational State Transfer, it is the most popular web API architecture. They follow a client-server model where one software program sends a request and the other response with some data.
These APIs generally receive a response in JSON format, it's a standard format that is easily "understandable" by applications and can be handled well in most languages. For e.g. Ruby app can easily use JSON response from Java sever.
There are several other forms of APIs (SOAP, XML-RPC, JSON-RPC, etc.), we'll limit our discussion to REST only. For more info, Click here
A lot of people prefer to compare HTTP with REST. REST and HTTP are not same.
In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs). The resources are acted upon by using a set of simple, well-defined operations. The clients and servers exchange representations of resources by using a standardized interface and protocol – typically HTTP.
Resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others. Metadata about the resource is available and used, for example, to control caching, detect transmission errors, negotiate the appropriate representation format, and perform authentication or access control. And most importantly, every interaction with a resource is stateless.
REST API request generally consists of Method, Body(auth and other parameters), and Host. Here is a sample request that I have been working on recently.
As stated earlier, REST API responses generally in JSON format. Here is a sample response to the above API requests.
Note: To get a similar response from the previous request, you'll have to add an API key: and API secret.
For more information, goto reastfulapi.net
I hope you have a good understanding of APIs now. If this article helped you, support it, and share it among your peers.