An API controller gives your plugin a server endpoint that React code can call. Start with a fixed response. Then replace it with the external service in the next chapter.
This chapter follows Writing API Endpoints and the inspected preview plugin's controller pattern.
Define the catalog contract
The tutorial uses this request:
GET /api/v2/my-custom-catalog?q=design&sort=title&page=1&limit=10
The response has this shape:
{
"items": [
{
"id": "resource-1",
"title": "Design foundations",
"url": "https://example.com/resources/design"
}
],
"page": 1,
"hasMore": false
}
These fields define the tutorial's external API contract. They are not a standard Vanilla content resource.
q is a search string. sort accepts title or newest. page starts at 1. limit controls the page size. The external service owns the search, ordering and page selection.
Define reusable schemas
Create Library/CatalogSchema.php in the addon.
<?php
namespace MyCustom\Catalog\Library;
use Garden\Schema\Schema;
class CatalogSchema
{
public static function query(): Schema
{
return Schema::parse([
"q:s" => ["default" => "", "maxLength" => 200],
"sort:s" => ["default" => "title", "enum" => ["title", "newest"]],
"page:i" => ["default" => 1, "minimum" => 1],
"limit:i" => ["default" => 10, "minimum" => 1, "maximum" => 30],
]);
}
public static function page(): Schema
{
return Schema::parse([
"items:a" => Schema::parse(["id:s", "title:s", "url:s"]),
"page:i" => ["minimum" => 1],
"hasMore:b",
]);
}
}
The limits are tutorial choices. Keep them aligned with the actual external API.
The response schema checks the fields and types. It does not establish that every URL is safe to open. Validate permitted URL schemes and any required host restrictions before rendering links.
Create the controller
Create Controllers/Api/MyCustomCatalogApiController.php:
<?php
namespace MyCustom\Catalog\Controllers\Api;
use Garden\Web\Data;
use MyCustom\Catalog\Library\CatalogSchema;
class MyCustomCatalogApiController extends \AbstractApiController
{
public function index(array $query = []): Data
{
$this->permission("session.valid");
$query = CatalogSchema::query()->validate($query);
// A temporary fixture. This does not implement search or paging.
$page = [
"items" => [[
"id" => "resource-1",
"title" => "Design foundations",
"url" => "https://example.com/resources/design",
]],
"page" => $query["page"],
"hasMore" => false,
];
return new Data(CatalogSchema::page()->validate($page));
}
}
The API router discovers enabled addon controllers using the ApiController naming convention. index handles the resource's collection GET request.
Always call permission(). Here, session.valid requires authentication. Add any record or integration permissions needed by your own endpoint. Hiding the widget in React does not protect its API.
Document the endpoint
Create openapi/my-custom-catalog.yml.
This is an abbreviated OpenAPI example. Add sort, page and limit parameters, the complete response schema, and error responses before publishing it.
openapi: 3.0.2
paths:
/my-custom-catalog:
get:
tags: [My Custom Catalog]
summary: Search the external catalog
parameters:
- name: q
in: query
schema:
type: string
maxLength: 200
responses:
"200":
description: A catalog page
"403":
description: Access denied
Use paths relative to API v2. The API router supplies /api/v2.
Documenting Endpoints explains how addon OpenAPI files are merged into the community's API documentation.
Try the endpoint
Refresh the local addon cache after adding PHP classes. While signed in, open /api/v2/my-custom-catalog on your local site.
You should see the fixture response. Then try ?limit=0 and ?sort=unknown. The request should fail validation.
Repeat the request as a signed-out visitor. Confirm that access is denied. Do not infer permission behavior from the administrator account alone.
Prepare the endpoint for external data
In Call a third-party API, you will inject CatalogService and replace the fixture with:
// Method-body excerpt after permission and query validation.
$page = $this->catalog->fetchPage($query);
return new Data(CatalogSchema::page()->validate($page));
Add the constructor dependency shown in that chapter. The service will own authentication, fetching, response validation and caching.
Checkpoint: the endpoint is discoverable, documented, permission checked and input validated.
The test chapter shows how to verify it with SiteTestCase. See the source review notes for the router and reference controller.