Sample REST API Example: A Comprehensive Guide

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.

What is a REST API and How to Create One?
What is a REST API and How to Create One?

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.

What Is a REST API? Examples, Uses, and Challenges
What Is a REST API? Examples, Uses, and Challenges

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.

πŸ”Check out this Flow Diagram on REST API Authentication Methods!πŸ”
πŸ”Check out this Flow Diagram on REST API Authentication Methods!πŸ”

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.
Build Your First REST API with Node.js
Build Your First REST API with Node.js

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:

Understanding And Using REST APIs β€” Smashing Magazine
Understanding And Using REST APIs β€” Smashing Magazine

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

What is the right API for your Project, GraphQL or REST?
What is the right API for your Project, GraphQL or REST?

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:

What is REST API? – A Comprehensive Guide To RESTful APIs
What is REST API? – A Comprehensive Guide To RESTful APIs
Understanding REST and How to Develop RESTful APIs
Understanding REST and How to Develop RESTful APIs
Announcing the Remote Config REST API
Announcing the Remote Config REST API
What is API and REST APIs? – In easy language
What is API and REST APIs? – In easy language
the rest api logo is shown in white on a blue background with text that reads rest api
the rest api logo is shown in white on a blue background with text that reads rest api
Writing a RESTful Web Service Client: Best Practices, API Integration & Development Guide
Writing a RESTful Web Service Client: Best Practices, API Integration & Development Guide
Industry Level REST API using .NET 6 – Tutorial for Beginners
Industry Level REST API using .NET 6 – Tutorial for Beginners
Say Goodbye to slow APIs with FastAPI
Say Goodbye to slow APIs with FastAPI
REST API Endpoints
REST API Endpoints
an image of a diagram showing the different types of web pages and how to use them
an image of a diagram showing the different types of web pages and how to use them
Tech concepts
Tech concepts
How to Build a REST API in Node.js and Express.js and File JSON Database
How to Build a REST API in Node.js and Express.js and File JSON Database
What Is REST API and How Can You Grab Data for Your App or Website?
What Is REST API and How Can You Grab Data for Your App or Website?
Post by @im-a-developer Β· 1 image
Post by @im-a-developer Β· 1 image
Spring Microservices RESTFul API Documentation With Swagger Part 1
Spring Microservices RESTFul API Documentation With Swagger Part 1
Building an AngularJS application using the DreamFactory REST API backend
Building an AngularJS application using the DreamFactory REST API backend
Exposing a SOAP service as a Managed REST API
Exposing a SOAP service as a Managed REST API
I will develop ios app in swift or objective c with firebase
I will develop ios app in swift or objective c with firebase
the word happy is written in black and orange with an image of a robot on it
the word happy is written in black and orange with an image of a robot on it
How to Establish a Session and Authenticate with the vSphere 6.5 RESTful API - Chris Wahl
How to Establish a Session and Authenticate with the vSphere 6.5 RESTful API - Chris Wahl

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.

Related Articles

Random Python Codes Python Selenium Test Example Python Sample Based On Probability Python Data Visualization Examples Python Dictionary Sample Python Selenium Script Example Is Python Good For Backend Debugging Code In Python Python Linear Regression Code Example Python Code Sample Download