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.
Install Dependencies
Make sure you have Node.js installed on your system to run JavaScript code and make HTTP requests.
Choose a Tool for API Requests
You can use built-in functions like fetch
or a library such as Axios.
Configure Your Environment
If the API requires authentication, set up environment variables to securely store your API keys or tokens.
Let's start by fetching a list of books from the GET /api/books
endpoint.
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.
Now, let's fetch a list of authors from the GET /api/authors
endpoint.
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
To add a new book to the Bookshop, use the POST /api/books endpoint.
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
To update an existing author's details, use the PUT /api/authors/:id
endpoint.
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
To delete a book from the Bookshop, use the DELETE /api/books/{id}
endpoint.
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
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.