How REST APIs Work: A Beginner's Guide

If you are new to web development, you might have heard of the term REST API, but what does it mean and how does it work? In this article, we will explain the basics of REST APIs and how they can help you build powerful and scalable web, mobile or IOT applications.

What is a REST API?

REST stands for Representational State Transfer, and it is a set of principles that define how web servers and clients should communicate with each other. A REST API is an application programming interface (API) that follows these principles and allows clients to access and manipulate data on a web server.

More often than not, a REST API sits between a web/mobile/IOT application and a database. The REST API allows for more controlled, secure and scalable way to interact with the database. The REST API and the database are part of the 'backend' of an overall architecture of a typical application.

Rest APIs sit between web/mobile/IOT applications and databases

A REST API consists of two main components: resources and methods. A resource is any piece of information that can be identified by a unique identifier (URI), such as a user, a product, or a blog post. A method is an action that can be performed on a resource, such as creating, reading, updating, or deleting it.

In LightSwitch API no-code interface, resources are represented by Models. Click here to see how you can define models and deploy a REST API in minutes.

The most common methods used in REST APIs are:

How does a REST API work?

A REST API works by using the HTTP protocol, which is the standard way of transferring data over the web. A client (such as a web browser or a mobile app) sends an HTTP request to a web server, specifying the resource and the method it wants to use. The web server then processes the request and sends back an HTTP response, containing the status code, headers, and body. The status code indicates whether the request was successful or not, and the body contains the data requested or the result of the operation.

For example, let's say we have a REST API that manages a list of users. To get all the users, we can send a GET request to the URI /users. The web server will respond with something like this:


json HTTP/1.1 200 OK Content-Type: application/json
[
    {
        "id": 1,
        "name": "Alice",
        "email": "[email protected]"
    },
    {
        "id": 2,
        "name": "Bob",
        "email": "[email protected]"
    },
    {
        "id": 3,
        "name": "Charlie",
        "email": "[email protected]"
    }
]

 

The status code 200 OK means that the request was successful, and the Content-Type header indicates that the data is in JSON format. The body contains an array of user objects, each with an id, name, and email property.

More on using GET endpoints with LightSwitch API here.

To create a new user, we can send a POST request to the same URI /users, with the user data in the body. The web server will respond with something like this:


json HTTP/1.1 201 Created Content-Type: application/json Location: /users/4
{
    "id": 4,
    "name": "David",
    "email": "[email protected]"
}

The status code 201 Created means that the resource was successfully created, and the Location header indicates where the new resource can be accessed. The body contains the user object that was created, with an id assigned by the server.

More on using POST endpoints with LightSwitch API here.

To update an existing user, we can send a PUT request to the URI /users/4, with the updated user data in the body. The web server will respond with something like this:


json HTTP/1.1 200 OK Content-Type: application/json
{
    "id": 4,
    "name": "David",
    "email": "[email protected]"
}

The status code 200 OK means that the resource was successfully updated, and the body contains the updated user object.

More on using PUT endpoints with LightSwitch API here.

To delete an existing user, we can send a DELETE request to the URI /users/4. The web server will respond with something like this:


    json HTTP/1.1 204 No Content

The status code 204 No Content means that the resource was successfully deleted, and there is no body in the response.

More on using DELETE endpoints with LightSwitch API here.

Why use a REST API?

There are many benefits of using a REST API for web development, such as:

Conclusion

In this article, we have explained what a REST API is, how it works, and why it is useful for web development. we hope you have learned something new and are ready to start building your own REST APIs. If you have any questions or feedback, please send us a quick message through the support page.

Back to Tutorial