Skip to content

Building a Basic API Tutorial

Craig Smith edited this page Dec 19, 2019 · 9 revisions

For this tutorial we will be making use of the following (you can substitute your own tools for the most)

  1. Docksal
  2. Postman for api testing
  3. Laravel 6.x
  4. Laravel Passport
  5. This Package (Duh)
  6. React Admin - for our visual site

Perhaps more as we build. this tutorial will be broken down into smallish steps that can be done pretty quickly.

Getting Started:

  1. Outside of the scope of this tutorial (and can be skipped if you use a different dev environment) Install Docksal & Postman on your machine
  2. Setup your dev environment and install laravel: composer create-project --prefer-dist laravel/laravel apitutorial
  3. Install Laravel Passport: composer require laravel/passport -
    1. follow the install instructions @ https://laravel.com/docs/6.x/passport#installation
    2. setup password grant @ https://laravel.com/docs/6.x/passport#creating-a-password-grant-client
  4. Setup some user seeding,
    1. run php artisan make:seeder UserTableSeeder
    2. edit database/seeds/DatabaseSeeder.php and add to the run method $this->call(UsersTableSeeder::class);
    3. run composer dump-autoload followed by php artisan db:seed
  5. Install Postman
  6. Setup our first api endpoint: to http://xxx/oauth/token as a post with a json body of
{
    "grant_type": "password",
    "client_id": "client-id",
    "client_secret": "client-secret",
    "username": "[email protected]",
    "password": "my-password",
    "scope": "*"
}

once you trigger the call you should get a response looking like:"

{
    "token_type": "Bearer",
    "expires_in": 31622400,
    "access_token": "xxx",
    "refresh_token": "xxx"
}

From this point you can add any additional packages you want to use for permissions / roles gates etc, the scope of which is a bit to much for this tutorial. I would suggest https://docs.spatie.be/laravel-permission/v3/introduction/

Continued in Part 2