Book API Documentation

Welcome to the Book API. Use this documentation to interact with the API endpoints.

API Endpoints

GET /api/books

Retrieve all books.

curl -X GET "https://your-project.vercel.app/api/books" -H "Authorization: Bearer YOUR_TOKEN"
          

GET /api/books/{id}

Retrieve a book by ID.

curl -X GET "https://your-project.vercel.app/api/books/{id}" -H "Authorization: Bearer YOUR_TOKEN"
          

POST /api/books

Create a new book. Do not include the ID in the request body.

curl -X POST "https://your-project.vercel.app/api/books" -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{
  "title": "Book Title",
  "description": "Book Description",
  "pages": 123
}'
          

PUT /api/books/{id}

Replace a book by ID. You must provide all fields in the request body.

curl -X PUT "https://your-project.vercel.app/api/books/{id}" -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{
  "title": "Updated Title",
  "description": "Updated Description",
  "pages": 456
}'
          

PATCH /api/books/{id}

Partially update a book by ID. Only include the fields you want to update.

curl -X PATCH "https://your-project.vercel.app/api/books/{id}" -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{
  "title": "Partially Updated Title"
}'
          

DELETE /api/books/{id}

Delete a book by ID.

curl -X DELETE "https://your-project.vercel.app/api/books/{id}" -H "Authorization: Bearer YOUR_TOKEN"
          

DELETE /api/books

Delete all books.

curl -X DELETE "https://your-project.vercel.app/api/books" -H "Authorization: Bearer YOUR_TOKEN"
          

GET /api/books-pages/paginate

Paginate through books. Specify the page number and the limit of books per page.

curl -X GET "https://your-project.vercel.app/api/books-pages/paginate?page=1&limit=3" -H "Authorization: Bearer YOUR_TOKEN"
          

GET /api/books/filter/title

Filter books by title.

curl -X GET "https://your-project.vercel.app/api/books/filter/title?title=Book%20Title" -H "Authorization: Bearer YOUR_TOKEN"
          

GET /api/books/filter/description

Filter books by description.

curl -X GET "https://your-project.vercel.app/api/books/filter/description?description=Book%20Description" -H "Authorization: Bearer YOUR_TOKEN"
          

GET /api/books/filter/pages

Filter books by the number of pages.

curl -X GET "https://your-project.vercel.app/api/books/filter/pages?pages=123" -H "Authorization: Bearer YOUR_TOKEN"
          

POST /api/login

Authenticate and get a JWT token.

curl -X POST "https://your-project.vercel.app/api/login" -H "Content-Type: application/json" -d '{
  "username": "your-username",
  "password": "your-password"
}'
          

Authentication

This API uses JWT for authentication. You need to include a valid JWT token in the Authorization header for all requests except login.

-H "Authorization: Bearer YOUR_TOKEN"
        

PostgreSQL Database

This API uses PostgreSQL for data storage. Make sure to set up your PostgreSQL database and provide the connection URL as an environment variable.

POSTGRES_URL=your_postgres_url
        

Installation

Prerequisites

  • Node.js (>=14.x)
  • npm (>=6.x)
  • PostgreSQL (for local development)

Steps

  1. Clone the repository:
  2. git clone https://github.com/PierreSwtich/qa-simple-book-api.git
    cd qa-simple-book-api
              
  3. Install dependencies:
  4. npm install
              
  5. Set up environment variables in a `.env` file:
  6. SECRET_KEY=your_secret_key
    USERNAMEADMIN=your_username
    PASSWORDADMIN=your_password
    POSTGRES_URL=your_postgres_url
              
  7. Run the application locally:
  8. npm run dev
              

The server will start on http://localhost:3000. Swagger UI is available at http://localhost:3000/api-docs.