Bookshop API

Quick Start Guide for the Bookshop API

This guide will help you quickly get started with the Bookshop API, which allows you to manage books and authors in an online bookshop application. The API supports operations such as creating, reading, updating, and deleting book and author records.

Step 1: Setting Up

  1. Install Dependencies

    Make sure you have Node.js installed on your system to run JavaScript code and make HTTP requests.

  2. Choose a Tool for API Requests

    You can use built-in functions like fetch or a library such as Axios.

  3. Configure Your Environment

    If the API requires authentication, set up environment variables to securely store your API keys or tokens.

Step 2: Making Your First API Call

Let's start by fetching a list of books from the GET /api/books endpoint.

Example: Fetch All Books

Create a file named fetchBooks.js with the following code:

async function fetchBooks() {
  try {
      const response = await fetch('/api/books', {
          method: 'GET',
          headers: {
              'Content-Type': 'application/json'
          }
      });

      if (!response.ok) {
          throw new Error('HTTP error!');
      }

      const data = await response.json();
      console.log('List of Books:', data);
  } catch (error) {
      console.error('Error fetching books:', error);
  }

}

fetchBooks();

Run the script:

node fetchBooks.js

You should see a list of books printed to the console.

Step 3: Working with Authors

Now, let's fetch a list of authors from the GET /api/authors endpoint.

Example: Fetch All Authors

Create a file named fetchAuthors.js with the following code:

async function fetchAuthors() {
  try {
      const response = await fetch('/api/authors', {
          method: 'GET',
          headers: {
              'Content-Type': 'application/json'
          }
      });

      if (!response.ok) {
          throw new Error('HTTP error! Status: ${response.status}');
      }

      const data = await response.json();
      console.log('List of Authors:', data);
  } catch (error) {
      console.error('Error fetching authors:', error);
  }

}

fetchAuthors();

Run the script:

node fetchAuthors.js

Step 4: Creating a New Book

To add a new book to the Bookshop, use the POST /api/books endpoint.

Example: Create a New Book

Create a file named createBook.js:

async function createBook() {
  try {
      const response = await fetch('/api/books', {
          method: 'POST',
          headers: {
              'Content-Type': 'application/json'
      },
          body: JSON.stringify({
              title: 'War and Peace',
              author: 'Leo Tolstoy',
              genre: 'Historical Fiction',
              publicationYear: 1869
          })
      });

  if (!response.ok) {
    throw new Error('HTTP error! Status: ${response.status}');
  }

  const data = await response.json();
  console.log('Book Created:', data);

  } catch (error) {
      console.error('Error creating book:', error);
  }

}

createBook();

Run the script:

node createBook.js

Step 5: Update an Author

To update an existing author's details, use the PUT /api/authors/:id endpoint.

Example: Update an Author

Create a file named updateAuthor.js:

async function updateAuthor(authorId) {
  try {
      const response = await fetch('/api/authors/${authorId}', {
          method: 'PUT',
          headers: {
              'Content-Type': 'application/json'
      },
          body: JSON.stringify({
              name: 'George R.R. Martin',
              biography: 'American novelist and short-story writer...',
              birthDate: '1948-09-20',
              nationality: 'American'
          })
      });

      if (!response.ok) {
      throw new Error('HTTP error! Status: ${response.status}');
      }

      const data = await response.json();
      console.log('Author Updated:', data);
  } catch (error) {
      console.error('Error updating author:', error);
  }

}

updateAuthor(1); // Replace with the ID of the author you want to update

Run the script:

node updateAuthor.js

Step 6: Delete a Book

To delete a book from the Bookshop, use the DELETE /api/books/{id} endpoint.

Example: Delete a Book

Create a file named deleteBook.js:

async function deleteBook(bookId) {
  try {
      const response = await fetch('/api/books/${bookId}', {
          method: 'DELETE',
          headers: {
              'Content-Type': 'application/json'
          }
      });

      if (!response.ok) {
          throw new Error('HTTP error! Status: ${response.status}');
      }

      const data = await response.json();
      console.log('Book Deleted:', data);
  } catch (error) {
      console.error('Error deleting book:', error);
  }

}

deleteBook(1); // Replace with the ID of the book you want to delete

Run the script:

node deleteBook.js

Next Steps

Now that you're familiar with the basics of using the Bookshop API, explore the full API Documentation to learn more about available endpoints and advanced usage.

This Quick Start Guide should help you integrate the Bookshop API into your application quickly and efficiently.