Bookshop API

Books

The Books resource allows you to manage book records in your application. Each book resource represents a book entity, which includes details such as the title, author, genre, and publication year.

The following fields are available for each book resource:

Endpoints and Methods

The Books resource exposes various endpoints to manage book resources. Below are the available endpoints and their HTTP methods:

Endpoints Overview

MethodEndpointDescription
GET/api/booksFetch all books.
GET/api/books/{id}Fetch a specific book by ID.
POST/api/booksCreate a new book.
PUT/api/books/{id}Update a book's information.
DELETE/api/books/{id}Delete a book by ID.

Parameters

API requests may include parameters that influence the response. Parameters are categorized as path parameters, query parameters, or request bodies.

Path Parameters

Request Body Parameters

For creating or updating a book, the following fields are used in the request body:

NameTypeRequiredDescription
titlestringYesThe title of the book.
authorstringYesThe author of the book.
genrestringNoThe genre of the book (e.g., Fiction, Non-fiction).
publicationYearintegerNoThe year the book was published.

Request Example

Below are examples for each type of request to the Books resource.

Get All Books

fetch('/api/books')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Get Book by ID

fetch('/api/books/1')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Create a New Book

fetch('/api/books', {
  method: 'POST',
  headers: {
  'Content-Type': 'application/json'
  },
  body: JSON.stringify({
      title: 'The Great Gatsby',
      author: 'F. Scott Fitzgerald',
      genre: 'Fiction',
      publicationYear: 1925
  })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Response

These examples illustrate the expected JSON responses for each endpoint.

Get All Books

[
  {
      "id": 1,
      "title": "The Great Gatsby",
      "author": "F. Scott Fitzgerald",
      "genre": "Fiction",
      "publicationYear": 1925
  },
  {
      "id": 2,
      "title": "1984",
      "author": "George Orwell",
      "genre": "Dystopian",
      "publicationYear": 1949
  }
]

Get Book by ID

{
  "id": 1,
  "title": "The Great Gatsby",
  "author": "F. Scott Fitzgerald",
  "genre": "Fiction",
  "publicationYear": 1925
}

Create Book

{
  "id": 3,
  "title": "To Kill a Mockingbird",
  "author": "Harper Lee",
  "genre": "Fiction",
  "publicationYear": 1960
}