API (an abbreviation of Application Programming Interface) is a special interface (a set of commands/controls) that is designed for the interaction of different programs with each other.

The programs themselves can be written in any programming language, work locally or remotely, on their own server, or in cloud infrastructure, no matter what.

The main thing is that through the API they can "understand" each other and interact: exchange data, transmit/receive commands for execution, etc. But in order for the API to be clear to both parties, they need to discuss a single communication format (protocol) in advance. That is why the API is documented in detail so that developers of other programs external to your interface can take into account the format of your data and requests when designing their software.

API Types

The API always appears before the GUI. Accordingly, we can start testing at an early stage, for example, when the API is ready, but the GUI is not. You can also often find that the API is a contract between services.

How it works

Methods of calling:

Services communicate with each other through the methods that I described above. Let's look at the examples below to see what the request and the response of the services look like.

Input data

HTTP Request

GET / HTTP/1.1
Host: test.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: wp-settings
Connection: keep-alive

Output data

HTTP Response

HTTP/1.1 200 OK
Date: Sun, 10 Feb 2013 03:51:41 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=5
Server: Apache
X-Pingback: //webkyrs.info/xmlrpc.php
 
<!DOCTYPE html PUBLIC «-//W3C//DTD XHTML 1.0 Transitional//EN»>
<html xmlns=»http://www.test.com/xhtml»>
<head>
<meta http-equiv=»Content-Type» content=»text/html; charset=utf-8″ />
<title>Test Name</title>
</head>
<body>
</body>
</html>

HTTP response codes

The server can send different codes in response to user requests. These can be error codes or just codes informing users about the status of the server. The most well-known codes are 4xx (client-side problems) and 5xx (server-side problems).

REST API

This is the ideology of API construction, which stands for Representative State Transfer API. It is based on the following principles formulated by its creator, Roy Fielding:

Client-server architecture

  1. Stateless server
  2. Cachability
  3. Multi-layer structure
  4. Unified interface
  5. Code on demand

Authentication

Usually, to use the API, you need a special key with which the server recognizes the user. In open APIs, the key may be missing or provided upon request (for example, after registration on the site).

The OAuth authentication protocol is widely used to integrate services with each other

Briefly about how to test the API

Of course, there are some specifics here, but we can use such generally accepted techniques as:

It seems to me that when testing, you need to take into account that APIs are created largely for the integration of services. And it is often not people who work with them, but other software systems. Therefore, it is necessary to evaluate the API from the point of view of its usability with other products, from the position of easy integration with it. A self-respecting API should also have clear and detailed documentation.

It can be concluded that all types of testing that we are used to - functional testing, load testing, security testing, usability testing, documentation testing - are not alien to API testing. In principle, this is not surprising, because the API is a full-fledged independent product.

Tools for working with the API

Regular GET requests can be sent using the browser. But there are many special tools that are designed for API development and testing. They provide an opportunity not only to send various types of requests but also to save requests, show results in various formats and act as a proxy server. And much much more.

Among such tools are: