PHPackages                             petyots/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. petyots/api-guard

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

petyots/api-guard
=================

A simple way of authenticating your APIs with API keys using Laravel. Forked from chrisbjr/api-guard And Working with L5.3.\*

v3.1.2(9y ago)21.2k1PHPPHP &gt;=5.5.9

Since Jun 15Pushed 9y ago1 watchersCompare

[ Source](https://github.com/petyots/api-guard)[ Packagist](https://packagist.org/packages/petyots/api-guard)[ Docs](https://github.com/chrisbjr/api-guard)[ RSS](/packages/petyots-api-guard/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (14)Versions (28)Used By (0)

ApiGuard
========

[](#apiguard)

[![Latest Stable Version](https://camo.githubusercontent.com/0f94c039999141e33222909a19d394eba956fd02720c928fe2d45629380b0742/68747470733a2f2f706f7365722e707567782e6f72672f706574796f74732f6170692d67756172642f762f737461626c65)](https://packagist.org/packages/petyots/api-guard) [![Total Downloads](https://camo.githubusercontent.com/c1c06612987b59e7150a2c980841d15dfa4f33151ead12ed55274390ad7a60b5/68747470733a2f2f706f7365722e707567782e6f72672f706574796f74732f6170692d67756172642f646f776e6c6f616473)](https://packagist.org/packages/petyots/api-guard)

[![Join the chat at https://gitter.im/chrisbjr/api-guard](https://camo.githubusercontent.com/abe08b740a4156153736f791393ec4da6619c4be73212e75769f52edacc0e2b5/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667)](https://gitter.im/chrisbjr/api-guard?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

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.

PATCHED TO WORK WITH LARAVEL 5.3.\*
-----------------------------------

[](#patched-to-work-with-laravel-53)

Laravel 5 is finally supported!
-------------------------------

[](#laravel-5-is-finally-supported)

\*\*Laravel 5.1.x to 5.3.x: `~3.*`

\*\*Laravel 5.1.x: `~2.*`

\*\*Laravel 4.2.x: [`~1.*`](https://github.com/chrisbjr/api-guard/tree/laravel4) (Recently updated version for Laravel 4. Please note that there are namespace changes here)

\*\*Laravel 4.2.x: [`0.*`](https://github.com/chrisbjr/api-guard/tree/v0.7) (The version that most of you are using)

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

[](#quick-start)

### Laravel 5.1.x to 5.2.x

[](#laravel-51x-to-52x)

Run `composer require petyots/api-guard`

In your `config/app.php` add `Chrisbjr\ApiGuard\Providers\ApiGuardServiceProvider` to the end of the `providers` array

```
'providers' => array(

    ...
    Chrisbjr\ApiGuard\Providers\ApiGuardServiceProvider::class,
),
```

Add the `ApiGuardAuth` facade to the end of the `aliases` array as well

```
'aliases' => array(

    ...
    'ApiGuardAuth' => \Chrisbjr\ApiGuard\Facades\ApiGuardAuth::class,
),
```

Add the following middleware inside your `app/Http/Kernel.php` file:

```
protected $routeMiddleware = [
    ...
    'apiguard' => \Chrisbjr\ApiGuard\Http\Middleware\ApiGuard::class,
];
```

Now publish the migration and configuration files for api-guard:

```
$ php artisan vendor:publish --provider="Chrisbjr\ApiGuard\Providers\ApiGuardServiceProvider"

```

Then run the migration:

```
$ php artisan migrate

```

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

### Laravel 5.0.x to 5.1.x (old users)

[](#laravel-50x-to-51x-old-users)

Note: Documentation for use with Laravel 5.0.x and 5.1.x differs from Laravel 5.2.x. Please refer to the README [here](https://github.com/chrisbjr/api-guard/tree/v2.3.0).

### Laravel 4.2.x

[](#laravel-42x)

Note: Documentation for use with Laravel 4.2.x differs from Laravel 5.0.x. Please refer to the README [here](https://github.com/chrisbjr/api-guard/tree/v1.0). If you are using version `0.*` you can find the README [here](https://github.com/chrisbjr/api-guard/tree/v0.7)

### 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.

Run the following command to generate an API key:

`php artisan api-key:generate`

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.

To generate an API key that is linked to a user, you can do the following:

`php artisan api-key:generate --user-id=1`

To generate an API key from within your application, you can use the following method in the `ApiKey` model:

```
$apiKey = Chrisbjr\ApiGuard\Models\ApiKey::make()

```

Usage
-----

[](#usage)

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

Note: The namespace of the `ApiGuardController` differs from previous versions.

```
