The evolution of web applications has led to a surge in the use of Representational State Transfer (REST) APIs, which have become the backbone of modern, scalable, and flexible software systems. Understanding how to work with REST APIs is a crucial skill for any developer, and one of the best ways to learn is by exploring practical examples. Let's dive into a sample REST API example to understand its components and how to interact with it.

In this guide, we'll focus on a sample REST API for managing a library book collection. Our API will have endpoints for creating, reading, updating, and deleting books, adhering to the standard CRUD (Create, Read, Update, Delete) operations supported by RESTful APIs.

API Base URL and Endpoints
The base URL for our sample API is https://api.example.com/library. From this base, we'll branching out to specific resource URLs.

Here's a list of endpoints we'll be using in our API:
/books:cano-get all books, create a new one, or search for books./books/{id}: get, update, or delete a specific book.

GET Requests
GET requests are used to retrieve data from the server. In our library API, we'll use GET requests to fetch all books or retrieve details of a specific book.
To get all books or search for books, use the /books endpoint with query parameters for search:

GET https://api.example.com/library/books?title=The+Catcher+In+the+Rye
The server will respond with an array of book objects that match the search query. To get details for a specific book, use the following approach:
GET https://api.example.com/library/books/42
The server will return a single book object with an ID of 42 if the book exists; otherwise, it will respond with a 404 Not Found status.
POST Requests

POST requests are used to create new resources on the server. In our API, we'll use POST to add a new book to the collection.
To create a new book, send a POST request to the /books endpoint with a JSON body containing the book details:




















POST https://api.example.com/library/books
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"publication_year": 1960
}
Handling Updates with PUT and PATCH
To update a resource, REST APIs use two methods: PUT and PATCH. We'll demonstrate both in our library API.
PUT Requests
PUT requests are used to replace an existing resource completely with the new data sent in the request body. In our API, a PUT request will update all fields of a book.
To update a book, send a PUT request to the /books/{id} endpoint with a JSON body containing the updated book details:
PUT https://api.example.com/library/books/42
{
"title": "adjusted title",
"author": "new author name",
"publication_year": 1961
}
A successful update will result in the server responding with the updated book object.
PATCH Requests
PATCH requests are used to update only specific fields of a resource, leaving the rest untouched. In our API, PATCH will only update the provided fields in a book.
To update specific fields in a book, send a PATCH request to the /books/{id} endpoint with a JSON body containing only the fields to be updated:
PATCH https://api.example.com/library/books/42
{
"author": "updated author name"
}
A successful update will result in the server responding with the updated book object, containing both the updated field and any other unchanged fields.
Deleting Resources with DELETE
DELETE requests are used to remove a resource from the server. In our library API, we'll use DELETE to remove a specific book from the collection.
To delete a book, send a DELETE request to the /books/{id} endpoint. Be cautious while deleting, as the action is irreversible:
DELETE https://api.example.com/library/books/42
A successful deletion will result in the server responding with a 204 No Content status, indicating that the resource has been deleted. If the book doesn't exist, the server will respond with a 404 Not Found status.
By exploring this sample REST API example, you've learned how to interact with a RESTful API using various HTTP methods, query parameters, and JSON bodies. Now you're ready to dive into more complex APIs and harness the power of REST in your own projects.