Your First Application
This guide takes you from an empty directory to a tested, running JSON API in a few minutes. Along the way you will see what makes PAM different from PHP-FPM: your application boots once, stays in memory, and serves every request from the same warm process.
You will use the api template, which creates a Nimbus application. Nimbus is PAM’s API-first framework: a Laravel-like structure, one routes file and controllers resolved through a container.
Before you start, install PAM and check that pam --version works.
Creating the Project
Section titled “Creating the Project”pam init hello-api --template apicd hello-apipam init writes the project files and then installs the Composer dependencies with PAM’s embedded Composer. When it finishes you will see:
● PROJECT ONLINE──────────────────────────────────────────────────────────── Preset API Directory hello-api Next cd hello-api && pam dev index.phpThe interesting files are small. index.php loads the application and starts listening:
<?php
declare(strict_types=1);
/** @var \Nimbus\App $app */$app = require __DIR__ . '/bootstrap/app.php';
$app->listen((int) (getenv('PAM_PORT') ?: 3000));bootstrap/app.php builds the application without starting a listener, so tests and the bin/nimbus tinker shell can reuse it:
<?php
declare(strict_types=1);
use Nimbus\App;
$root = dirname(__DIR__);
$app = new App();$app->routes($root . '/routes.php');
return $app;See Directory Structure for every file the template creates.
Running the Development Server
Section titled “Running the Development Server”pam dev index.phpPAM / DEV MATRIX hot reload active────────────────────────────────────────────────────────────● [ok] Watching /home/alice/hello-api Ctrl+C stops the development runtime.● READY Pam listening on http://127.0.0.1:3000In another terminal, call the starter route:
curl -i http://127.0.0.1:3000/api/pingHTTP/1.1 200 OKcontent-type: application/json; charset=utf-8content-length: 18
{"message":"pong"}pam dev watches your PHP files, composer.json, composer.lock and .env. When one changes, it restarts the runtime and prints Change detected · reloading runtime. Stop it with Ctrl+C.
To use another port, set PAM_PORT for the process:
PAM_PORT=8080 pam dev index.phpAdding a Route and a Controller
Section titled “Adding a Route and a Controller”Routes live in routes.php, which receives the router as $router. Add a route with a parameter:
<?php
declare(strict_types=1);
use App\Controllers\GreetingController;use App\Controllers\PingController;use Nimbus\RouteRegistrar;
/** @var RouteRegistrar $router */
$router->get('api/ping', [PingController::class, 'show']);$router->get('api/hello/{name}', [GreetingController::class, 'show']);Generate the controller class with the API generator. It creates app/Controllers/GreetingController.php with an index action; replace it with a show action:
pam make:controller GreetingController<?php
declare(strict_types=1);
namespace App\Controllers;
use Pam\Http\Request;use Pam\Http\Response;
final readonly class GreetingController{ public function show(Request $request, Response $response): Response { $name = $request->route('name');
return $response->json([ 'message' => "Hello, {$name}!", 'worker' => getmypid(), ]); }}Save the files. pam dev reloads on its own, so you can call the new route right away:
curl http://127.0.0.1:3000/api/hello/Alice{"message":"Hello, Alice!","worker":823692}Unknown paths return a Problem Details response with a stable integer code:
curl -i http://127.0.0.1:3000/api/missingHTTP/1.1 404 Not Foundcontent-type: application/problem+json; charset=utf-8
{"type":"https:\/\/pam.dev\/problems\/4","title":"Route not found.","status":404,"code":4}Seeing Persistence in Action
Section titled “Seeing Persistence in Action”Under PHP-FPM, every request starts from scratch. Under PAM, the worker that served the previous request is still alive. Add a closure route with a static counter to see it (put the two use lines with the other imports at the top of the file):
use Pam\Http\Request;use Pam\Http\Response;
$router->get('api/visits', static function (Request $request, Response $response): Response { static $visits = 0;
return $response->json(['visits' => ++$visits]);});curl http://127.0.0.1:3000/api/visits # {"visits":1}curl http://127.0.0.1:3000/api/visits # {"visits":2}curl http://127.0.0.1:3000/api/visits # {"visits":3}The counter survives between requests because the process does. That is where PAM’s speed comes from, and it is also the one rule you must keep in mind: anything you store in a static property, a global or a singleton is shared by every later request on that worker. Each worker also has its own copy, so a cluster of workers does not share the count. Read Memory & State before you keep per-user data anywhere but the request.
Testing the Application
Section titled “Testing the Application”The template ships a PHPUnit suite that boots the application in memory, without opening a socket. Add a test for the new route:
<?php
declare(strict_types=1);
namespace Tests;
final class GreetingTest extends TestCase{ public function test_it_greets_by_name(): void { $this->getJson('/api/hello/Alice') ->assertOk() ->assertJsonPath('message', 'Hello, Alice!'); }
public function test_unknown_routes_return_problem_details(): void { $this->getJson('/api/missing') ->assertNotFound() ->assertJsonPath('code', 4); }}Run the suite inside PAM’s embedded PHP:
pam testPHPUnit 12.5.45 by Sebastian Bergmann and contributors.
Runtime: PHP 8.5.13Configuration: /home/alice/hello-api/phpunit.xml
... 3 / 3 (100%)
OK (3 tests, 9 assertions)pam test picks PHPUnit or Pest automatically; force one with --phpunit or --pest, and pass any runner arguments after it, such as pam test --filter GreetingTest. The template also defines pam composer test as a shortcut.
Running a Production Cluster Locally
Section titled “Running a Production Cluster Locally”pam dev runs one process with hot reload. In production you run several workers under a supervisor with pam start:
pam start index.php --workers 4 --admin-address 127.0.0.1:3010Pam control plane listening on http://127.0.0.1:3010● READY Pam listening on http://127.0.0.1:3000 worker=1● READY Pam listening on http://127.0.0.1:3000 worker=2...Pam master 1068555 started 4 worker(s); send SIGHUP for zero-downtime reloadThe control plane answers /live, /ready, /startup and /metrics. Send SIGHUP to the master for a zero-downtime reload and SIGTERM to drain in-flight requests and stop. Workers & Supervision explains the model in depth.
Building a Bundle
Section titled “Building a Bundle”pam build copies the project, the pam binary and its PHP library into a new directory with an integrity manifest:
pam build● PRODUCTION BUNDLE READY──────────────────────────────────────────────────────────── Output /home/alice/hello-api/dist Launch /home/alice/hello-api/dist/bin/pam-runThe output directory must not exist yet; choose another one with --output, and another entry file with --entry. Deployment explains what the bundle contains and how to run it, along with containers and the process manager.
Next Steps
Section titled “Next Steps”- Configuration: ports, environment,
php.iniand extensions. - Request Lifecycle: what happens between
listen()and your controller. - Routing, Controllers and Validation with Nimbus.
- Choose a Target if you are building Laravel, mobile or desktop applications.