Laravel Rest API

The Books list API

This API returns a book list to demonstrate how to make good API's

Required

php >= 8.2
composer
docker

To install docker, in case you never used, you can visit the Docker Desktop site and see how to install and configure (WSL) on windows.

Usage (windows WSL):

1 - Install composer dependencies

./vendor/bin/sail composer install
or
./vendor/bin/sail composer update

2 - Exec sail command to start the docker container

./vendor/bin/sail up

You can use the option (-d) to ignore logs and unlock your terminal.

./vendor/bin/sail up -d

Running migrations:

./vendor/bin/sail artisan migrate

Registering user and start API

On the home page you have a form to register new users, and receive the token for using the API in your email.

http://localhost/api_restful/

Starting application

Now to use this API you need some data. So enter this url in your browser to generate some data (key for JWT and some books for you to get in requests).

http://localhost/api_restful/preparations

Endpoints

1 - /api/all_books/{limit}

You can get all of books, with this endpoint:

http://localhost/api_restful/api/all_books/{limit}

The variable $limit is the max numbers of books that you want.

2 - /api/book/{id}

With this endpoint you can get the book by your id number, by passing $id.

http://localhost/api_restful/api/book/{id}

And now, you can do your's requests on Postman, or your favorite app, but don't forget your token, you should put it on your header requisition, as bellow:

JS
fetch('https://localhost/api/all_books/10', {
    method: 'GET',
    headers: {
        Authorization: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
    }
}).then(result => result.text())

Make good use and good luck.

Developer