|
| 1 | +# API v1 (Beta) |
| 2 | + |
| 3 | +!!! warning "Beta" |
| 4 | + The v1 API is in **beta**. Endpoints and behaviour may change without |
| 5 | + notice while it is under active development. Full endpoint documentation |
| 6 | + is coming later — for now this page covers the basics and how to extend |
| 7 | + it. The stable, fully documented API remains [v0](index.md). |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +The v1 API is served under the `/api/v1` prefix and is built on |
| 12 | +[Laravel Restify](https://restify.binarcode.com/) with |
| 13 | +[Sanctum](https://laravel.com/docs/sanctum) bearer-token authentication. |
| 14 | + |
| 15 | +## Authentication |
| 16 | + |
| 17 | +Most v1 endpoints require a personal access token sent as a bearer token: |
| 18 | + |
| 19 | +```curl |
| 20 | +curl -H 'Authorization: Bearer YOURAPITOKENHERE' https://librenms.org/api/v1/health |
| 21 | +``` |
| 22 | + |
| 23 | +Create a v1 token from the web interface at `/api-access/` under the |
| 24 | +**API v1 tokens** section. |
| 25 | + |
| 26 | +## Available endpoints |
| 27 | + |
| 28 | +| Method | Path | Auth | Description | |
| 29 | +|--------|------------------|---------------|--------------------------------------| |
| 30 | +| `GET` | `/api/v1/ping` | none (public) | Liveness probe. Returns `{"status":"ok"}`. | |
| 31 | +| `GET` | `/api/v1/health` | bearer token | Subsystem health (database, cache). | |
| 32 | + |
| 33 | +More endpoints will be added as the v1 API matures. |
| 34 | + |
| 35 | +## Adding custom endpoints |
| 36 | + |
| 37 | +There are two places to add v1 functionality, depending on whether you |
| 38 | +need a plain route or a full CRUD resource. |
| 39 | + |
| 40 | +### 1. Custom (non-repository) endpoints |
| 41 | + |
| 42 | +Routes that are not backed by a Restify repository — health checks, |
| 43 | +actions, reports, etc. — are registered in the `routes()` method of |
| 44 | +`app/Providers/RestifyServiceProvider.php`, under the `api/v1` prefix. |
| 45 | +Point each route at an invokable controller in |
| 46 | +`app/Http/Controllers/Api/V1/`. |
| 47 | + |
| 48 | +```php |
| 49 | +// app/Providers/RestifyServiceProvider.php |
| 50 | +protected function routes(): void |
| 51 | +{ |
| 52 | + Route::prefix('api/v1')->group(function (): void { |
| 53 | + // Public, unauthenticated endpoint. |
| 54 | + Route::get('ping', PingController::class)->name('v1.ping'); |
| 55 | + |
| 56 | + // Authenticated endpoint — add the sanctum middleware. |
| 57 | + Route::get('health', HealthController::class) |
| 58 | + ->middleware('auth:sanctum') |
| 59 | + ->name('v1.health'); |
| 60 | + }); |
| 61 | + |
| 62 | + parent::routes(); |
| 63 | + |
| 64 | + // ... |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +A minimal controller: |
| 69 | + |
| 70 | +```php |
| 71 | +// app/Http/Controllers/Api/V1/PingController.php |
| 72 | +namespace App\Http\Controllers\Api\V1; |
| 73 | + |
| 74 | +use App\Http\Controllers\Controller; |
| 75 | +use Illuminate\Http\JsonResponse; |
| 76 | + |
| 77 | +class PingController extends Controller |
| 78 | +{ |
| 79 | + public function __invoke(): JsonResponse |
| 80 | + { |
| 81 | + return response()->json(['status' => 'ok']); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +Guidelines: |
| 87 | + |
| 88 | +- Place controllers in `app/Http/Controllers/Api/V1/`. |
| 89 | +- Add `->middleware('auth:sanctum')` to any route that should require a |
| 90 | + token. Leave it off only for intentionally public endpoints (e.g. `ping`). |
| 91 | +- Register custom routes **before** `parent::routes()` so they are defined |
| 92 | + alongside the Restify-managed routes. |
| 93 | + |
| 94 | +### 2. Restify repository (CRUD) resources |
| 95 | + |
| 96 | +For standard create/read/update/delete access to a model, add a Restify |
| 97 | +repository in `app/Restify/` and register it in the `boot()` method of |
| 98 | +`RestifyServiceProvider`: |
| 99 | + |
| 100 | +```php |
| 101 | +// app/Providers/RestifyServiceProvider.php |
| 102 | +public function boot(): void |
| 103 | +{ |
| 104 | + parent::boot(); |
| 105 | + |
| 106 | + Restify::repositories([ |
| 107 | + \App\Restify\DeviceRepository::class, |
| 108 | + // ...add repositories here as v1 resources are introduced |
| 109 | + ]); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +Restify then generates the standard resource routes (index, show, store, |
| 114 | +update, destroy) under `/api/v1` automatically. See the |
| 115 | +[Laravel Restify documentation](https://restify.binarcode.com/) for how to |
| 116 | +define repositories, fields, and authorization. |
| 117 | + |
| 118 | +### After changing routes |
| 119 | + |
| 120 | +Route and config are cached on container start, so after adding or changing |
| 121 | +endpoints clear the caches: |
| 122 | + |
| 123 | +```bash |
| 124 | +php artisan route:clear |
| 125 | +php artisan config:clear |
| 126 | +``` |
| 127 | + |
| 128 | +Verify the result with `php artisan route:list --path=api/v1`. |
0 commit comments