All jobs

Add a JSON REST API to a CakePHP App

Yes. An agent adds a JSON REST API to a CakePHP 5 app: an Api route prefix, resource routes, a table with its migration, JSON views and tests, in one pull request.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the request pipeline first, because a CakePHP app built for HTML pages refuses API clients in ways that are easy to miss. In an app created from the cakephp/app skeleton, src/Application.php already adds BodyParserMiddleware, so a JSON body arrives through $request->getData(). It also adds CsrfProtectionMiddleware for every route, and PagesControllerTest::testCsrfAppliedError proves what that means for a client with no token: a POST answers 403. config/routes.php has a single / scope using DashedRoute with fallbacks(), there is no table class under src/Model/Table, and Application::bootstrap() sets allowFallbackClass(false), so a controller cannot load a table that has no class.

It adds the data model the issue asks for; the examples here use an Articles resource. bin/cake bake migration writes the table definition into a new config/Migrations directory, bin/cake bake model writes ArticlesTable with its validation rules and the Article entity, and the agent tightens the baked validator to the rules in the issue and marks only the writable fields as accessible, so a client cannot set id or the timestamps. cakephp/bake and cakephp/migrations are already in composer.json, so nothing new is installed for this step.

Then the routes and the controller. A prefix('Api') block in config/routes.php sets the json extension and calls resources('Articles'), which maps the five REST actions to src/Controller/Api/ArticlesController.php. The controller returns JsonView from viewClasses() and serialises its view variables; index paginates with a maxLimit so one request cannot ask for the whole table, add answers 201 with the new record, a failed validation answers 422 with the entity's errors keyed by field, and a missing id falls through to CakePHP's error handler, which the agent checks renders JSON for a JSON request. For CSRF it configures the existing middleware to skip requests routed to the Api prefix instead of removing it, which works because RoutingMiddleware runs earlier in the queue, and it leaves testCsrfAppliedError in place as proof that HTML forms are still protected.

Tests are written with IntegrationTestTrait, as the two existing test classes are. tests/Fixture/ArticlesFixture.php supplies rows, and tests/TestCase/Controller/Api/ArticlesControllerTest.php sends JSON with an Accept: application/json header to each route: the list and its pagination fields, one record, a 404, a create with a body that has no CSRF token, a 422 naming the bad field, an update that ignores id in the body, and a delete. tests/bootstrap.php runs Migrator, so the test database gets the table from the new migration. The agent starts bin/cake server in its sandbox and repeats the calls with curl, then runs composer check and PHPStan at the level 8 that phpstan.neon sets, which is where an untyped controller property or a nullable entity shows up.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "A Linear issue labelled api asks for a JSON REST API for articles in this CakePHP 5 app: list with pagination, read one, create, update and delete, with validation errors returned per field. Read composer.json, config/routes.php, config/plugins.php, src/Application.php, src/Controller/AppController.php, tests/bootstrap.php and the tests under tests/TestCase first. Bake a migration under config/Migrations and the Articles table and entity; make only the writable fields accessible. Add an Api route prefix with the json extension and resources('Articles'), and src/Controller/Api/ArticlesController.php using JsonView through viewClasses(), a paginate maxLimit, 201 on create, 422 with the entity errors on failed validation and JSON 404s. Keep CsrfProtectionMiddleware for the HTML routes and make it skip requests routed to the Api prefix; do not remove it. Add a fixture and an integration test for every route, including a create with no CSRF token and an update that tries to change id. Start bin/cake server and repeat the calls with curl. Run composer check and PHPStan at the level in phpstan.neon. Open a pull request that links the issue, pastes the test, phpcs, PHPStan and curl output, and states plainly that these routes have no authentication yet.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

The pull request adds the migration, ArticlesTable, the Article entity, the Api controller, the fixture and the controller test, and changes config/routes.php and src/Application.php. Its description links the Linear issue, shows the route list from bin/cake routes, the PHPUnit, phpcs and PHPStan output, and the curl transcript against the server the agent started: each request, its status and its JSON body.

The existing thirteen tests are unchanged and still run, so the home page, the error pages and CSRF on HTML forms behave as before. In a repository that keeps the skeleton's workflow, CI runs the suite against SQLite on two PHP versions; a team whose production database is MySQL or Postgres asks for the migration to be run on that server in the sandbox as well.

The description ends with what the reviewer has to decide. The routes are open to anyone who can reach the app, because the skeleton has no authentication and the agent does not invent a scheme the issue did not ask for; the usual follow-up on the same session adds token authentication to the Api prefix. The reviewer also confirms the maxLimit, the list of accessible fields, and whether delete should remove the row or mark it.

Run it on every Linear issue labelled api

In the console, connect Linear under Settings → Connections, then add an automation to the Project with a linear trigger on linear.issue.labeled and the label api. Its prompt is the general form of this one: build the resource the issue describes, under the Api prefix, with a migration, tests and the same checks. The trigger puts the issue after the prompt, so each labelled issue starts one run that reads its own requirements; a change made by an app integration never fires it, only one by a person in your Rush org.

From your own server, the same run is one POST /o/{org}/sessions per issue with the repository in resources and the issue's https://linear.app/<workspace>/issue/<ID> URL beside it, which Rush reads into the prompt through your Linear connection. Send a request_id that is a UUID v5 of the issue id so a retried call does not start a second run.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

An API is only as good as the requests it has answered. In its own sandbox the agent runs the migration, starts the app with bin/cake server and calls every route with curl, including the ones that should fail, so the 403 a missing CSRF exemption causes or the HTML error page a JSON client would have received is found before the pull request exists. The commands and their output are kept with the run, and the transcript in the description lets a reviewer see the real responses without checking the branch out.

It also turns a backlog of small API issues into parallel work. Five labelled issues are five runs, each in its own sandbox with its own SQLite file and its own branch, and none of them waits for a developer to finish something else. The team's time goes to the questions in each description: who may call this, and what a delete should mean.

Frequently asked questions

Why not just remove the CSRF middleware?

Because the HTML forms still need it. The agent keeps CsrfProtectionMiddleware in the queue and makes it skip only requests routed to the Api prefix, where clients send JSON and no browser session is involved. PagesControllerTest::testCsrfAppliedError stays as the check that a form POST without a token is still refused.

Does the API require authentication?

Not after this request, and the pull request says so in plain words. The skeleton has no users or tokens, and the issue asked for the resource. Ask in the issue or in a follow-up for token or JWT authentication on the Api prefix and the agent adds the authentication plugin and tests for 401 responses.

Do clients have to add .json to every URL?

No. The json extension is enabled for clients that prefer /api/articles.json, and the controller also answers JSON to a request whose Accept header asks for it. The tests cover both forms so neither stops working unnoticed.

Can it expose a table I already have instead of creating one?

Yes. Name the table in the issue. The agent reads its existing table class, validation and associations, adds no migration, and limits the accessible fields and the serialised output to the columns you list, so nothing internal leaks through the new routes.

More CakePHP jobs

The same job on other stacks

Keep reading