yusufgenc/filament-api-forge
Expose Filament Resources as REST APIs automatically—no Sanctum. Hash-based API keys with scopes, per-resource access control, rate limiting, IP restrictions, Spatie Query Builder filtering/sorting, and auto-generated OpenAPI + public Swagger docs.
Installation:
composer require yusufgenc/filament-api-forge
Publish the package config and migrations:
php artisan vendor:publish --provider="Yusufgenc\FilamentApiForge\FilamentApiForgeServiceProvider"
php artisan migrate
Enable for a Resource:
Add HasApi trait to your Filament Resource:
use Yusufgenc\FilamentApiForge\Traits\HasApi;
class PostResource extends Resource implements HasApi
{
// ...
}
First Use Case:
/filament-api-forge to generate an API key.GET /api/posts) using the key in the forge_ prefix format (e.g., forge_<hashed-key>)./api/docs.Resource Integration:
HasApi are automatically exposed.public static function getApiConfig(): array
{
return [
'enabled' => true,
'auth' => 'api', // or 'public'
'rate_limit' => 'high',
'ip_restrictions' => ['192.168.1.1'],
];
}
Authentication:
forge_<token>) or query param (forge_token).config/filament-api-forge.php.Query Builder Support:
GET /api/posts?filter[status]=published&sort=-created_at&page=1
Access Control:
getApiConfig():
'access_control' => [
'create' => 'admin',
'update' => 'editor',
'delete' => 'super-admin',
],
can() gates or Filament’s authorizeResource() methods.Rate Limiting:
config/filament-api-forge.php) or per-resource:
'rate_limit' => 'high', // 'low', 'medium', 'high', or custom
Documentation:
/api/docs (Swagger UI).'public_docs' => true,
forge_ tokens in PHPUnit tests:
$response = $this->withHeaders(['Authorization' => 'forge_' . $apiKeyHash])
->getJson('/api/posts');
public static function getApiConfig(): array
{
return ['cache' => true];
}
public static function getRelations(): array
{
return [
// ...
Tables\Actions\Action::make('notify')
->url(fn (Post $record) => route('api.posts.notify', $record))
->openUrlInNewTab(),
];
}
Token Hashing:
CORS Issues:
config/cors.php) allows API routes:
'paths' => ['api/*', 'filament-api-forge/*'],
Rate Limiting:
config/filament-api-forge.php:
'rate_limits' => [
'low' => '5/minute',
'medium' => '20/minute',
'high' => '60/minute',
],
'rate_limit' => null in getApiConfig() for unauthenticated routes.Query Builder Conflicts:
filter[resource]). Use underscores for custom filters:
GET /api/posts?filter[is_featured]=true
Filament Admin Access:
canAccessDashboard() if needed.Logs: Enable debug mode in config/filament-api-forge.php:
'debug' => env('APP_DEBUG', false),
Check storage/logs/laravel.log for API-related errors.
API Responses:
{
"message": "Unauthorized",
"errors": {
"auth": ["Invalid API key"]
}
}
Route Debugging:
php artisan route:list --path=api
Custom API Routes: Override auto-generated routes by binding a controller:
Route::get('/custom-posts', [CustomPostController::class, 'index'])
->middleware('filament-api-forge.auth');
Authentication Providers:
Extend the HashAuthProvider for custom logic:
// app/Providers/FilamentApiForgeServiceProvider.php
public function boot()
{
$this->app->extend('filament-api-forge.auth', fn () => new CustomAuthProvider());
}
Documentation Extensions:
Add custom OpenAPI schemas via Filament’s registerResources():
FilamentApiForge::extendDocs(function (OpenApi $openApi) {
$openApi->info->title('My Custom API');
});
Event Listeners:
Listen to API events (e.g., ApiRequestHandled):
event(new ApiRequestHandled($request, $response));
.env to toggle API features:
FILAMENT_API_FORGE_ENABLED=false
api auth for public read-only endpoints:
'auth' => 'none',
throttle middleware for additional protection:
Route::middleware(['throttle:60,1'])->group(...);
How can I help you explore Laravel packages today?