2026-01-26 20:57:08 +00:00
|
|
|
# Core API Package
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
REST API infrastructure with OpenAPI documentation, rate limiting, webhook signing, and secure API key management.
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
## Installation
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
```bash
|
|
|
|
|
composer require host-uk/core-api
|
|
|
|
|
```
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
## Features
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
### OpenAPI/Swagger Documentation
|
|
|
|
|
Auto-generated API documentation with multiple UI options:
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
```php
|
|
|
|
|
use Core\Mod\Api\Documentation\Attributes\{ApiTag, ApiResponse};
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
#[ApiTag('Products')]
|
|
|
|
|
#[ApiResponse(200, ProductResource::class)]
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
return ProductResource::collection(Product::paginate());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
**Access documentation:**
|
|
|
|
|
- `GET /api/docs` - Scalar UI (default)
|
|
|
|
|
- `GET /api/docs/swagger` - Swagger UI
|
|
|
|
|
- `GET /api/docs/redoc` - ReDoc
|
|
|
|
|
- `GET /api/docs/openapi.json` - OpenAPI spec
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
### Secure API Keys
|
|
|
|
|
Bcrypt hashing with backward compatibility:
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
```php
|
|
|
|
|
use Core\Mod\Api\Models\ApiKey;
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
$key = ApiKey::create([
|
|
|
|
|
'name' => 'Production API',
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
'scopes' => ['read', 'write'],
|
|
|
|
|
]);
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
// Returns the plain key (shown only once)
|
|
|
|
|
$plainKey = $key->getPlainKey();
|
2026-01-26 20:47:46 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
**Features:**
|
|
|
|
|
- Bcrypt hashing for new keys
|
|
|
|
|
- Legacy SHA-256 support
|
|
|
|
|
- Key rotation with grace periods
|
|
|
|
|
- Scope-based permissions
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
### Rate Limiting
|
|
|
|
|
Granular rate limiting per endpoint:
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
```php
|
|
|
|
|
use Core\Mod\Api\RateLimit\RateLimit;
|
|
|
|
|
|
|
|
|
|
#[RateLimit(limit: 100, window: 60, burst: 1.2)]
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
|
{
|
|
|
|
|
// Limited to 100 requests per 60 seconds
|
|
|
|
|
// With 20% burst allowance
|
|
|
|
|
}
|
2026-01-26 20:47:46 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
**Features:**
|
|
|
|
|
- Per-endpoint limits
|
|
|
|
|
- Workspace isolation
|
|
|
|
|
- Tier-based limits
|
|
|
|
|
- Standard headers: `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset`
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
### Webhook Signing
|
|
|
|
|
HMAC-SHA256 signatures for outbound webhooks:
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
```php
|
|
|
|
|
use Core\Mod\Api\Models\WebhookEndpoint;
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
$endpoint = WebhookEndpoint::create([
|
|
|
|
|
'url' => 'https://example.com/webhooks',
|
|
|
|
|
'events' => ['order.created', 'order.updated'],
|
|
|
|
|
'secret' => WebhookEndpoint::generateSecret(),
|
|
|
|
|
]);
|
|
|
|
|
```
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
**Verification:**
|
2026-01-26 20:47:46 +00:00
|
|
|
```php
|
2026-01-26 20:57:08 +00:00
|
|
|
$signature = hash_hmac('sha256', $timestamp . '.' . $payload, $secret);
|
|
|
|
|
hash_equals($signature, $request->header('X-Webhook-Signature'));
|
|
|
|
|
```
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
### Scope Enforcement
|
|
|
|
|
Fine-grained API permissions:
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
```php
|
|
|
|
|
use Core\Mod\Api\Middleware\EnforceApiScope;
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
Route::middleware(['api', EnforceApiScope::class.':write'])
|
|
|
|
|
->post('/products', [ProductController::class, 'store']);
|
|
|
|
|
```
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
## Configuration
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
// config/api.php (after php artisan vendor:publish --tag=api-config)
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'rate_limits' => [
|
|
|
|
|
'default' => 60,
|
|
|
|
|
'tiers' => [
|
|
|
|
|
'free' => 100,
|
|
|
|
|
'pro' => 1000,
|
|
|
|
|
'enterprise' => 10000,
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'docs' => [
|
|
|
|
|
'enabled' => env('API_DOCS_ENABLED', true),
|
|
|
|
|
'require_auth' => env('API_DOCS_REQUIRE_AUTH', false),
|
|
|
|
|
],
|
|
|
|
|
];
|
2026-01-26 20:47:46 +00:00
|
|
|
```
|
|
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
## API Guides
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
The package includes comprehensive guides:
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
- **Authentication** - API key creation and usage
|
|
|
|
|
- **Quick Start** - Getting started in 5 minutes
|
|
|
|
|
- **Rate Limiting** - Understanding limits and tiers
|
|
|
|
|
- **Webhooks** - Setting up and verifying webhooks
|
|
|
|
|
- **Errors** - Error codes and handling
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
Access at: `/api/guides`
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
## Requirements
|
|
|
|
|
|
|
|
|
|
- PHP 8.2+
|
|
|
|
|
- Laravel 11+ or 12+
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
## Changelog
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
See [changelog/2026/jan/features.md](changelog/2026/jan/features.md) for recent changes.
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
## Security
|
2026-01-26 20:47:46 +00:00
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
See [changelog/2026/jan/security.md](changelog/2026/jan/security.md) for security updates.
|
2026-01-26 20:47:46 +00:00
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
2026-01-26 20:57:08 +00:00
|
|
|
EUPL-1.2 - See [LICENSE](../../LICENSE) for details.
|