PHPackages                             rupadana/filament-api-service - 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. rupadana/filament-api-service

ActiveLibrary[API Development](/categories/api)

rupadana/filament-api-service
=============================

A simple api service for supporting filamentphp

4.0.3(6mo ago)204103.8k↓13.9%49[7 PRs](https://github.com/rupadana/filament-api-service/pulls)7MITPHPPHP ^8.2CI passing

Since Aug 26Pushed 3mo ago6 watchersCompare

[ Source](https://github.com/rupadana/filament-api-service)[ Packagist](https://packagist.org/packages/rupadana/filament-api-service)[ Docs](https://github.com/rupadana/api-service)[ GitHub Sponsors](https://github.com/rupadana)[ RSS](/packages/rupadana-filament-api-service/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (16)Versions (60)Used By (7)

Filament Api Service
====================

[](#filament-api-service)

[![Total Downloads](https://camo.githubusercontent.com/a485a51804826fb44157dced21f0b6d50ceb4279d45217ef95b7515f98d3f7dc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7275706164616e612f66696c616d656e742d6170692d736572766963652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rupadana/filament-api-service)[![Run Test](https://github.com/rupadana/filament-api-service/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/rupadana/filament-api-service/actions/workflows/run-tests.yml/badge.svg?branch=main)

Filament API Service is a powerful yet simple package that enables automatic API generation for FilamentPHP resources. It eliminates the need for manual route registration and provides built-in support for authentication, authorization, filtering, sorting, and more.

Features
--------

[](#features)

- **Automatic API Generation** – Instantly create RESTful API endpoints for Filament resources.
- **Authentication &amp; Authorization** – Includes built-in authentication endpoints and integrates with Spatie Laravel Permission &amp; Filament Shield.
- **Role-Based Access Control** – Token-based authentication with customizable policies.
- **Query Filters &amp; Sorting** – Supports Spatie's laravel-query-builder for dynamic filtering, sorting, and field selection.
- **Transformers** – Customize API responses using dedicated transformers.
- **Multi-Tenancy Support** – Enable tenant-aware API responses with minimal configuration.
- **Middleware Customization** – Add or override middlewares at the panel or resource level.
- **Public &amp; Secure APIs** – Define public or protected endpoints with a simple configuration.
- **API Documentation** – Automatically generate API documentation with Scramble.

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

[](#installation)

Install the package via Composer:

```
composer require rupadana/filament-api-service
```

Register it in your Filament Provider:

```
use Rupadana\ApiService\ApiServicePlugin;

$panel->plugins([
    ApiServicePlugin::make()
])
```

For further configuration, run:

```
php artisan vendor:publish --tag=api-service-config
```

```
return [
    'navigation' => [
        'token' => [
            'cluster' => null,
            'group' => 'User',
            'sort' => -1,
            'icon' => 'heroicon-o-key',
            'should_register_navigation' => false,
        ],
    ],
    'models' => [
        'token' => [
            'enable_policy' => true,
        ],
    ],
    'route' => [
        'panel_prefix' => true,
        'use_resource_middlewares' => false,
    ],
    'tenancy' => [
        'enabled' => false,
        'awareness' => false,
    ],
    'login-rules' => [
        'email' => 'required|email',
        'password' => 'required',
    ],
    'login-middleware' => [
        // Add any additional middleware you want to apply to the login route
    ],
    'logout-middleware' => [
        'auth:sanctum',
        // Add any additional middleware you want to apply to the logout route
    ],
    'use-spatie-permission-middleware' => true,
];
```

Usage
-----

[](#usage)

Create an API service for a Filament resource:

```
php artisan make:filament-api-service BlogResource
```

Since version 3.0, routes automatically registered. it will grouped as '/api/`admin`'. `admin` is panelId. to disable panelId prefix, please set `route.panel_prefix` to `false`

Once generated, the following API endpoints are automatically available:

MethodEndpointDescriptionGET/api/`admin`/blogsReturn LengthAwarePaginatorGET/api/`admin`/blogs/1Return single resourcePUT/api/`admin`/blogs/1Update resourcePOST/api/`admin`/blogsCreate resourceDELETE/api/`admin`/blogs/1Delete resourceTo disable the admin panel prefix, update the config:

```
'route' => [
    'panel_prefix' => false
],
```

### Token Resource

[](#token-resource)

By default, Token resource only show on `super_admin` role. you can modify give permission to other permission too.

Token Resource is protected by TokenPolicy. You can disable it by publishing the config and change this line.

```
'models' => [
        'token' => [
            'enable_policy' => false // default: true
        ]
    ],
```

Important

If you use Laravel 11, don't forget to run `php artisan install:api` to publish the personal\_access\_tokens migration after that run `php artisan migrate` to migrate the migration, but as default if you run the `php artisan install:api` it will ask you to migrate your migration.

### Filtering &amp; Allowed Field

[](#filtering--allowed-field)

We used `"spatie/laravel-query-builder": "^5.3"` to handle query selecting, sorting and filtering. Check out [the spatie/laravel-query-builder documentation](https://spatie.be/docs/laravel-query-builder/v5/introduction) for more information.

In order to allow modifying the query for your model you can implement the `HasAllowedFields`, `HasAllowedSorts` and `HasAllowedFilters` Contracts in your model.

```
class User extends Model implements HasAllowedFields, HasAllowedSorts, HasAllowedFilters {
    // Which fields can be selected from the database through the query string
    public static function getAllowedFields(): array
    {
        // Your implementation here
    }

    // Which fields can be used to sort the results through the query string
    public static function getAllowedSorts(): array
    {
        // Your implementation here
    }

    // Which fields can be used to filter the results through the query string
    public static function getAllowedFilters(): array
    {
        // Your implementation here
    }
}
```

### Create a Handler

[](#create-a-handler)

To create a handler you can use this command. We have 5 Handler, CreateHandler, UpdateHandler, DeleteHandler, DetailHandler, PaginationHandler, If you want a custom handler then write what handler you want.

```
php artisan make:filament-api-handler BlogResource
```

or

```
php artisan make:filament-api-handler Blog
```

#### Customize Handlers

[](#customize-handlers)

If you want to customize the generated handlers, you can export them using the following command.

```
php artisan vendor:publish --tag=api-service-stubs
```

### Transform API Response

[](#transform-api-response)

```
php artisan make:filament-api-transformer Blog
```

it will be create BlogTransformer in `App\Filament\Resources\BlogResource\Api\Transformers`

```
