PHPackages                             ohffs/simple-api-key-middleware - 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. [API Development](/categories/api)
4. /
5. ohffs/simple-api-key-middleware

ActiveLibrary[API Development](/categories/api)

ohffs/simple-api-key-middleware
===============================

Very basic API key middleware for Laravel

1.0.3(4y ago)05MITPHP

Since Apr 2Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ohnotnow/simple-api-key-middleware)[ Packagist](https://packagist.org/packages/ohffs/simple-api-key-middleware)[ RSS](/packages/ohffs-simple-api-key-middleware/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (2)Versions (5)Used By (0)

Simple API key middleware for Laravel
=====================================

[](#simple-api-key-middleware-for-laravel)

This is a very basic middleware to check a bearer token on an incoming request.

Installation
------------

[](#installation)

```
composer require ohffs/simple-api-key-middleware
php artisan migrate
```

The migration will create a new database table called `simple_api_keys`. You can have a look in the packages `migrations/` directory to see the details.

Usage
-----

[](#usage)

The package registers a middleware alias `simple-api-key` (mapped to `\Ohffs\SimpleApiKeyMiddleware\ApiKeyMiddleware`). To protect a given route you could do :

```
Route::get('/secret-stuff', [SecretStuffController::class, 'show'])->middleware('simple-api-key');
```

See the Laravel docs for more examples such as route groups or default middlewares.

To generate an API key from the CLI you can use the artisan command :

```
php artisan api-key:generate "Key for secret stuff"
```

Which will create the key with that description and show you the resulting token. You can remove a key by doing :

```
php artisan api-key:remove 14-2398adshh2349addasd7213402
```

(where the 14-2398... is the token you generated). You can also programmatically create a token :

```
$apikey = \Ohffs\SimpleApiKeyMiddleware\SimpleApiKey::generate('My new Token');
echo $apikey->plaintext_token;
```

The `plaintext_token` is only available on freshly created tokens.

To make a request using the token, you pass it as a regular bearer-token. Eg, with `curl` :

```
curl -H "Authorization: Bearer 12-sd8623hsdfi9823nsdf9sdf" https://example.com/secret-stuff
```

Or using Laravel's HTTP client :

```
$response = Http::withToken('12-sd8623hsdfi9823nsdf9sdf')->get('https://example.com/secret-stuff');
```

### Configuration

[](#configuration)

The default configuration is as follows :

```
    // default length of generated api keys
    'token_length' => env('API_KEY_LENGTH', 64),
    // enable caching of token lookups
    'cache_enabled' => env('API_KEY_CACHE_ENABLED', true),
    // time to cache token lookup results
    'cache_ttl_seconds' => env('API_KEY_CACHE_TTL', 60),
```

You can set those environment variables, or if you run `php artisan vendor:publish` and pick the config from this package you can edit the resulting `config/simple_api_keys.php` file. By default the middleware will cache token lookups to save hitting the database for every one. If you are not using a cache such as Redis then you might want to disable the caching.

###  Health Score

22

—

LowBetter than 23% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

1496d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5472be37b2cbedfa6c905bb674cb5780378cf2adcfc47befe01ad7bb4d14f734?d=identicon)[ohnotnow](/maintainers/ohnotnow)

### Embed Badge

![Health badge](/badges/ohffs-simple-api-key-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/ohffs-simple-api-key-middleware/health.svg)](https://phpackages.com/packages/ohffs-simple-api-key-middleware)
```

###  Alternatives

[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[resend/resend-laravel

Resend for Laravel

1191.4M6](/packages/resend-resend-laravel)[flat3/lodata

OData v4.01 Producer for Laravel

96320.9k](/packages/flat3-lodata)[joggapp/laravel-aws-sns

Laravel package for the SNS events by AWS

3171.8k](/packages/joggapp-laravel-aws-sns)[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)[spatie/spatie-price-api

The Price API used at promotional sites for our own products

1515.1k1](/packages/spatie-spatie-price-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
