Skip to content
Alexey Averikhin edited this page Dec 2, 2026 · 1 revision

A JSON API under /api/v1, for programs that act for a user. It is off by default: every address answers 404.

Turn it on

API_ENABLED=true

Users then find API tokens on their account page. Creating a token asks for their password again. The token is shown once, so they copy it then; only its hash is stored.

curl -H "Authorization: Bearer 1|your-token" https://your-app.example/api/v1/user

What a token may do

When creating a token, the user ticks its abilities: permissions from config/permissions.php, and only those their account has. Every request checks both again, so a token works only while:

  • the token has the ability, and its user still has the permission,
  • the user may still sign in (not deactivated, not unconfirmed),
  • the token exists. Users delete tokens on their account page.

So taking a permission from a role, deactivating a user or deleting the token stops it at once. With REQUIRE_TWO_FACTOR_FOR_ADMINISTRATORS=true, admin abilities also need two-factor authentication on the account.

Endpoints

Address Needs Answers
GET /api/v1/user any token the token's own user
GET /api/v1/users admin.access and users.manage all users, 25 a page
GET /api/v1/users/{id} admin.access and users.manage one user
GET /api/v1/roles admin.access and roles.manage all roles with their permissions

A user looks like this:

{
  "data": {
    "id": 2,
    "name": "Jane Example",
    "email": "jane@example.com",
    "active": true,
    "confirmed": true,
    "roles": ["authenticated"],
    "created_at": "2026-12-01T09:30:00+00:00"
  }
}

Errors are JSON too: 401 without a valid token, 403 without the permission, 404 while the API is off, 429 over the rate limit.

Settings

Setting Default
API_ENABLED false Turns the API and the token section on
API_RATE_LIMIT 60 Requests a minute, per user
API_TOKEN_EXPIRATION empty Minutes a token lasts. Empty: until it is deleted
API_PRODUCT_MODULE empty A NetLicensing module the whole API needs a licence for

Add an endpoint

Routes go in routes/api.php, inside the existing group, and ask for their permissions with token.can:. Admin data asks for admin.access as well:

Route::get('products', [ProductController::class, 'index'])
    ->middleware('token.can:admin.access,products.manage')
    ->name('products.index');

Answer with an API resource in app/Http/Resources/V1, so the JSON doesn't change when the model does. App\Http\Controllers\Api\V1\UserController is a complete example, and tests/Feature/ApiTest.php shows how to test one.

Clone this wiki locally