DEV Community

Ayako yk
Ayako yk

Posted on

What is API?

An API is used everywhere, making our everyday life so much easier. Thanks to an API, Google Maps not only displays a map but navigates us to our destinations. When we book a flight or a hotel, it provides us with the latest information about the availability or the cheapest options. When we log in a website, we don’t have to worry about setting up a new password. We can use our Facebook, Twitter or Google accounts. And there’s much, much more.

An API stands for Application Programming Interface, so it literally means it connects applications. An API works as a gateway. It accepts and returns a request from users, and on the server-side it limits the exposure of an application created by a third-party or simplifies their complex programming so that developers can make use of it without understanding everything occurring behind the scene. In addition, with APIs developers can develop applications more efficiently because we don’t have to create a function from scratch. Servers, too, have their own benefits. Since they share their information only partially, they can still hide their internal details. Users, as mentioned earlier, have lots of benefits available.

Here’s how an API works. A user initiates an API call to retrieve information, or to put it simply, a user sends a request to an API. Then, the API makes a call to a web server. The web server sends a response with the requested information. And then, the API sends the data back to the user.

More technically speaking, there are three types of APIs: Private APIs, Partner APIs, and Public APIs. Private APIs are used within an organization, and Partner APIs are openly published but shared with known business partners. Public APIs are mostly designed for end-users, and they can be freely used to create applications. But we should bear it in mind that there are two types - open (free of charge) and commercial ones.

APIs are classified according to the systems. The most common one is a web API, which provides interaction between web-based systems. Web APIs that conform to the constraints of REST are called RESTful web APIs. REST stands for REpresentational State Transfer and is an architectural style or a guide for providing standards between computer systems on the web.

RESTful web APIs use Hyper-Text Transfer Protocol, or HTTP, requests with URL and the use of JSON or XML to transmit data to retrieve or modify data on the server. A request consists of an HTTP verb, a header, a path to a resource, and an optional message body containing data. The four basic HTTP verbs are GET, POST, PUT and DELETE. GET is used to retrieve a resource, POST to create a new resource, PUT to edit or update an existing resource, and DELETE to delete a resource. In the header of the request, a user also specifies the type of content, which is called the Accept field, where it ensures the server sends data that the user understands or processes. The options for types of content are MIME Types, or Multipurpose Internet Mail Extensions. They consist of a type and a subtype separated by a slash. So, the header the user sends for a request looks like this:

GET https://xxx.com/yyy
Accept: text/html

POST https://xxx.com/yyy/zzz
Body:
Enter fullscreen mode Exit fullscreen mode

{
“user”: {
“name” = “xxx”,
“email” = “yyy@zzz.com
“id” = 000
}
}

When the server sends data back to the user, the server must include a content-type, just as they are in the accept field. The content-type should be one of the options that the user requested in the accept field. The header from the server also needs to include status codes to inform the user of the success of the operation. The most common status codes are 200 (OK), 201 (CREATED), 204 (NO CONTENT), 400 (BAD REQUEST), 403 (FORBIDDEN), 404 (NOT FOUND), and 500 (INTERNAL SERVER ERROR). Here’s the examples of the headers the server-side sends back:

Status Code: 200 (OK)
Content-type: text/html

201 (CREATED)
Content-type: application/json
Enter fullscreen mode Exit fullscreen mode

Both XML and JSON are used with APIs. XML, or Extensible Markup Language was designed to carry data. It is a set of rules for encoding documents in a format that is both human-readable and machine-readable, and is a W3C recommendation. It uses tags just like HTML and some people might say it contains too many characters. JSON, on the other hand, is a light-weight and easy-to-parse text format, requiring less coding. JSON, or JavaScript Object Notation, was derived from JavaScript, but it is a language-independent data format and many modern programming languages include code to generate and parse JSON-format data. So, JSON has become popular among developers as a data format.

All benefits considered from the perspectives of end-users, developers and businesses, where users will experience their personalized convenience, developers will be able to build functions and services more efficiently with less time and cost by using the reliable external source, and businesses will gain the opportunities to connect and integrate with more companies and therefore grow their businesses, and the number of available web APIs has grown and open source tools have been developed and sophisticated, APIs will continue to grow to a bright future.

Discussion (0)