API

Scaffold comes with a fully functional API layer. It handles authentication using Laravel Sanctum tokens. Users can create and delete their own tokens from their account, and a token is only visible at the moment it is created.

Tokens

There are two ways to get a token.

From the user's account

The API tokens page (user/api-tokens) lets a user name, create and delete tokens. It is handled by User\ApiTokenController.

Device login

Mobile or desktop clients can exchange a user's credentials for a token. The response contains a plain-text token to send as a bearer token.

POST api/device-login

[email protected]
password=secret
device_name=iPhone
{
    "data": {
        "token": "1|abc123..."
    }
}

Endpoints

The endpoints use the routes/api.php routes, all prefixed with api/ and protected by auth:sanctum. The API is throttled to 60 requests a minute.

GET     api/users/me
PUT     api/users/update
DELETE  api/users/destroy

GET     api/chat/threads
POST    api/chat

The me endpoint uses the UserResource for its explicit output. The chat endpoints are covered in the AI Chat docs.

Base Controller

The base controller for the API controllers (Api\ApiController) contains a user() method which returns auth('sanctum')->user(), and aborts with a 401 when there is no user. This way you don't have to keep rewriting that accessor.

Errors

Any error on an api/* route renders as JSON, including 404s.

API Documentation

OpenAPI docs are generated automatically by Scramble from the controllers in app/Http/Controllers/Api and the resources in app/Http/Resources. Don't write API doc annotations by hand; make sure the FormRequests and API Resources are correct instead. Settings live in config/scramble.php.