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"
}

-- Optionally -- api headers middleware ? 5. Seed admin User 6. Oauth + Secrets 7. Setup in postman (optinally extra postman package to get initial api points) 8. Install api-controller package.

---- End Part 1

--- start Part 2

    • setup Users Endpoint to map to users model
    • add Policy for security
    • Add Resource for response
    • Add Request for Validation
    • Add Scope for extra scope
  1. by this point should have a basic CRUD group.
  2. -- Custom Endpoint -- Me - for own profile

--- end Part 2

--- start part 3 (advanced extras)

  1. -- joins
Clone this wiki locally