PHPackages                             awalko/api-guard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. awalko/api-guard

ActiveLibrary[HTTP &amp; Networking](/categories/http)

awalko/api-guard
================

A simple way of authenticating your APIs with API keys using Laravel

v0.5(11y ago)7521PHPPHP &gt;=5.4.0

Since Jun 15Pushed 11y ago1 watchersCompare

[ Source](https://github.com/awalko/api-guard)[ Packagist](https://packagist.org/packages/awalko/api-guard)[ Docs](https://github.com/chrisbjr/ApiGuard)[ RSS](/packages/awalko-api-guard/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (7)Used By (0)

ApiGuard
========

[](#apiguard)

A simple way of authenticating your APIs with API keys using Laravel. This package uses the following libraries:

- philsturgeon's [Fractal](https://github.com/thephpleague/fractal)
- maximebeaudoin's [api-response](https://github.com/ellipsesynergie/api-response)

The concept for managing API keys is also taken from Phil Sturgeon's [codeigniter-restserver](https://github.com/philsturgeon/codeigniter-restserver). I've been looking for an equivalent for Laravel but did not find any so this is an implementation for that.

Quick start
-----------

[](#quick-start)

### Required setup

[](#required-setup)

In the `require` key of `composer.json` file add the following

```
"awalko/api-guard": "dev-master"

```

Run the Composer update comand

```
$ composer update

```

In your `config/app.php` add `'Awalko\ApiGuard\ApiGuardServiceProvider'` to the end of the `$providers` array

```
'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'EllipseSynergie\ApiResponse\Laravel\ResponseServiceProvider',
    'Awalko\ApiGuard\ApiGuardServiceProvider',

),

```

Now generate the api-guard migration (make sure you have your database configuration set up correctly):

```
$ php artisan migrate --package="awalko/api-guard"

```

It will setup two tables - api\_keys and api\_logs.

### Generating your first API key

[](#generating-your-first-api-key)

Once you're done with the required setup, you can now generate your first API key.

Make sure your Laravel installation is accessible through a web server - if not, you can use `artisan` to quickly bring up your Laravel installation by running the command below:

```
$ php artisan serve

```

Once the web server is up, you can issue a POST request ApiGuard's pre-defined route for generating an API key. You can use `curl` in the command line as shown below:

```
$ curl -X POST http://localhost:8000/apiguard/api_key

```

This will generate an API key and should return the following data:

```
{
    data: {
        id: 9
        user_id: 0
        key: "7f03891b8f7c4ba10af2e0e37232f98fa2fc9a1a"
        level: 10
        ignore_limits: 1
        created_at: {
            date: "2014-06-26 12:07:49"
            timezone_type: 3
            timezone: "UTC"
        }
        updated_at: {
            date: "2014-06-26 12:07:49"
            timezone_type: 3
            timezone: "UTC"
        }
    }
}

```

Take note of your first API key.

Now, to prevent others from generating API keys through the route above, you can disable this in ApiGuard's configuration file.

To create your own configuration file for ApiGuard, run the following command:

```
$ php artisan config:publish awalko/api-guard

```

The configuration file will be found in `app/config/packages/awalko/api-guard/config.php`. Open this file and change the `generateApiKeyRoute` variable to `false`

```
'generateApiKeyRoute' => false

```

Generally, you will want to generate API keys for each user in your application. The `api_keys` table has a `user_id` field which you can populate for your users.

Usage
-----

[](#usage)

Basic usage of ApiGuard is to create a controller and extend that class to use the `ApiGuardController`.

```
