API: Introduction
A criteria-scoped REST API for streams and entries. You opt in to routes and own authentication.
Streams API (streams/api) turns your stream definitions into a REST API. It builds JSON responses for stream definitions and entries from the same criteria you use in PHP, so an endpoint returns exactly what Streams::entries('posts')->where(...)->get() would.
What it is
- Resources and endpoints.
StreamsResource(list, show, create, update, patch, delete) andEntriesResource(list, show, create, update, patch, delete, query). - Interfaces. An
ApiInterfacegroups resources under a path, domain, and middleware stack. You can register several (for examplev1andadmin). - A consistent envelope. Every response has
data,meta,links, anderrorskeys. See Responses. - Self-describing. Responses carry a
selflink andmeta(the query, payload, route parameters, and stream handle), andGET /api/streamsreturns the stream definitions themselves, which is what lets the JavaScript client discover the API. - Optional HTTP caching through the
ApiCachemiddleware (ETags andCache-Control). See Caching. - Schema commands.
php artisan api:schemaandphp artisan api:documentationfor OpenAPI output. See OpenAPI.
Why it exists
Streams already knows the shape of your data: fields, types, validation rules, and relationships live in streams/*.json. Writing CRUD controllers by hand repeats that knowledge and drifts from it. The API reads the stream definition at request time, so adding a field to a stream adds it to the API with no controller changes.
What it does not do
- It does not authenticate anyone. The package has no users, tokens, or policies. Your application owns the middleware. See Authentication.
- It does not mount routes on its own. Nothing is routed until you register an interface.
- It is not JSON:API. Request bodies are flat field maps. See Requests.
How to use it
composer require streams/api:1.0.x-dev
STREAMS_API_ENABLED=true
// app/Providers/AppServiceProvider.php
use Streams\Api\Support\Facades\API;
public function boot(): void
{
API::routeCrud(); // streams + entries under /api
}
GET /api/streams/posts/entries now lists entries. Protect it before you deploy: see Authentication.
How to extend it
- Register more interfaces with their own paths and middleware: Custom interfaces.
- Add endpoints and resources: Custom endpoints.
- Resolve a tenant per request: Tenancy.