Build Predictive Machine Learning with Flink | Workshop on Dec 18 | Register Now

What is Rest API?

REST API stands for Representational State Transfer. Learn more about REST API, how it simplifies server communication, and how it leverages large-scale data.

What is REST?

A Representational State Transfer, or REST, is a standard that guides the design and development of the processes that allow us to interact with (create, read, update, and delete) data stored on web servers. In other words, REST simplifies communication with data on a server by providing hypertext transfer protocol (HTTP) methods that can be used to perform various interactions with data on web servers.

With REST, you can communicate with servers using HTTP protocol. Because this is designed for usage on the largest scale—the Internet—the coupling between the client and the origin server has to be as loose as possible to allow for the scale.

The Elements of REST

There are six elements that guide REST. When applied to the system’s architecture, these impart performance, scalability, simplicity, modifiability, visibility, portability, and reliability:

Client/Server:

The client is separated from the servers by a well-defined interface.

Stateless:

The client does not consume server storage when it isn’t processing a request.

Cache:

Responses indicate a server’s own cacheability in order to reduce latency.

Uniform interface:

Interfaces are consistent in design.

Layered system:

The client cannot tell whether it is connected directly to the end server or to an intermediary on the path to the end server.

Code on demand:

The servers are able to temporarily extend or customize the functionality of a client as needed by transferring logic to the client that can be executed within a standard virtual machine (VM).

The Definition of REST API

An Application Programming Interface, or API, establishes a connection between programs, so that they can transfer data. If a program has an API, some of its data is used by the front-end of the program or another application entirely. 

In order for this data to be accessed and used, a request must be sent to the API. If the request meets the correct parameters, the API responds with the requested data, usually in JSON or XML format. An API includes documentation that outlines what data is available from it and how to structure your request for that data. 

In other words, the API is a courier of data that requires the right instructions to deliver. Or, to use a real-world example, the API is a server in a restaurant. When you order from the menu, the server puts your request into the kitchen and carries your food to your table.

REST API definition | Confluent

How do REST APIs work?

REST APIs make the communication process with the server easier by giving us various HTTP request methods to use:
  • GET: Used to read data on a server. This is the default method and is used with the fetch command.
  • POST: Used to create data. A command for this requires both the word POST with the endpoint as well as the request body and its corresponding data to send to the server.
  • PATCH/PUT: Used to update data. This requires the ID of the item being updated as well as the request body with the update being pushed.
  • DELETE: Used to permanently remove data. This command requires both the intended action and the ID of the item being deleted.
 
For example, if you want to access a webpage to see the data contained on it, you make a GET request to a resource provided by a server. If the request is valid, the server responds by providing the data requested, then we see the contents of the webpage. It also sends a code indicating the status of the request as valid or invalid (200 is a success, 400 is a client error).

Examples of REST API

The following are JavaScript examples of REST API requests to the fictional “SamplePlaceholder” API.

fetch(‘https://sampleplaceholder.example.com/todos/1’)

     .then(response => response.json())

     .then(json => console.log(json))

In the above GET example, we are requesting one to-do item to be returned in JSON format.

fetch("https://sampleplaceholder.example.com/todos", {

  method: "POST",

  body: JSON.stringify({

    userId: 1,

    title: "Fix bugs",

    completed: false

  }),

  headers: {

    "Content-type": "application/json; charset=UTF-8"

  }

});

In the above POST example, the body contains the data to be sent to the server and added to the SamplePlaceholder to-dos API. The headers hold the type of content you want to send to the server, which in this case, is JSON data. After sending this, you would use the fetch command to verify that the new data appears as expected.

fetch('https://sampleplaceholder.example.com/posts/2', {

  method: 'PATCH',

  body: JSON.stringify({

    title: 'new todo',

  }),

  headers: {

    'Content-type': 'application/json; charset=UTF-8',

  },

})

  .then((response) => response.json())

  .then((json) => console.log(json));

In the above PATCH example, the command is to update a page with a new title. The second parameter is used to define the body (data to be sent) and the type of request to be sent, while the third parameter is the header that specifies the type of data you will send, for example, JSON.

fetch('https://sampleplaceholder.example.com/posts/5', {

  method: 'DELETE',

});

In the above DELETE example, the command is to permanently remove the page titled “5,” found in posts.

How Confluent Can Help

Confluent’s cloud-native solution, the 10x Apache Kafka® service, is elastic, resilient, and performant. Powered by the Kora Engine, it manages 30,000+ fully managed clusters with 99% uptime SLA. With 70+ fully managed connectors, it enables you to connect in any app and system so you can rapidly build, test, and deploy streaming data pipelines. 

Confluent Cloud for Apache Flink® provides a REST API for managing your Flink SQL statements and compute pools programmatically. And Confluent Cloud provides REST APIs for cluster management and the production of records to topics, for greater flexibility in how you manage your cluster.

If you’re ready to put Confluent Cloud to work and take advantage of our REST APIs for better data streaming management, get started for free.