Search by

taphra / api-starter

simoamtoun

Taphra JSON REST API starter: JWT sign-up and login, a posts resource with owner checks, validation, rate limiting, CORS and RFC 7807 errors, on SQLite.

Package info

github.com/taphra/api-starter

Type:project

pkg:composer/taphra/api-starter

Statistics

Installs: 6

Dependents: 0

Suggesters: 0

Stars: 0

v1.1.0 2026-12-03 10:25 UTC

This package is auto-updated.

Last update: 2026-12-03 10:25:20 UTC


README

Taphra's guides are at https://taphra.dev/docs/. Coding agents: read this app's AGENTS.md, and all the guides in one file at https://taphra.dev/llms-full.txt.

A JSON REST API on Taphra: sign-up and login with signed tokens (JWT), a posts resource that only its author can change, validation, rate limits, CORS and RFC 7807 error documents. It runs on a SQLite file, so there is no database server to set up.

Requirements

  • PHP 8.2 or later, with the pdo_sqlite extension (php -m lists your extensions)
  • Composer

Quick start

composer create-project taphra/api-starter my-api
cd my-api
php taphra migrate        # creates the users and posts tables
php taphra serve          # http://127.0.0.1:8000

composer create-project writes a random JWT_SECRET (and APP_SECRET_KEY) to .env, which app/bootstrap.php reads. A variable set in the real environment takes precedence. There is no default secret: when JWT_SECRET is missing or shorter than 32 bytes, the API issues and accepts no token and answers 500 (the reason is in app/tmp/Log/). Generate a new one with php -r "echo bin2hex(random_bytes(32)), PHP_EOL;".

Try it

In a second terminal:

curl -s -X POST http://127.0.0.1:8000/api/auth/register \
  -H 'Content-Type: application/json' \
  -d '{"name":"Ada","email":"ada@example.com","password":"correct horse"}'
{"user":{"id":1,"name":"Ada","email":"ada@example.com"},"token":"eyJ0eXAiOiJKV1Qi..."}

Keep the token and send it as a bearer token:

TOKEN=eyJ0eXAiOiJKV1Qi...      # the token from the answer above
curl -s -X POST http://127.0.0.1:8000/api/posts \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"title":"Hello","body":"First post"}'
{"id":1,"user_id":1,"title":"Hello","body":"First post","created_at":"...","updated_at":"..."}

Errors are problem documents (Content-Type: application/problem+json):

curl -s -X POST http://127.0.0.1:8000/api/posts -H 'Content-Type: application/json' -d '{}'
{"type":"about:blank","title":"Unauthorized","status":401,"detail":"Unauthorized"}

Routes

Method and path Auth What it does
GET / {"name":"api-starter","status":"ok"}
GET /health {"status":"ok"}
POST /api/auth/register {name, email, password}: 201, the user and a token
POST /api/auth/login {email, password}: the user and a token (valid one hour)
GET /api/auth/me token the signed-in user
GET /api/posts newest first, 20 per page (?page=2)
GET /api/posts/{id} one post
POST /api/posts token {title, body}: 201 and a Location header
PUT /api/posts/{id} author {title, body}
PATCH /api/posts/{id} author {title} and/or {body}
DELETE /api/posts/{id} author 204

A path called with another method gets a 405 with an Allow header. Invalid input gets a 422 with the errors per field; a post that is not yours, a 403.

Security defaults

  • Tokens are signed with JWT_SECRET (HS256) and expire after an hour. They travel in the Authorization header, never in cookies, so there is no CSRF token to send. They carry token_use: access; a token marked as a refresh token gets a 401.
  • Passwords are stored with password_hash(); the API never returns them.
  • Every route: 60 requests per minute per client IP (rate_limit_per_minute in app/Config/app.yml). Login: after 5 failures for an address from one IP, 15 minutes of 429.
  • CORS: no other origin may call the API from a browser until you list it under cors_origins in app/Config/app.yml.
  • Behind a proxy or load balancer, list it under trusted_proxies so the rate limit sees the real client IP.
  • An unset APP_ENV means production: no debug pages. php taphra serve sets APP_ENV=dev for its server.

The request pipeline

app/Static/index.php sends every request through PSR-15 middleware, then to the controller: ProblemJsonMiddleware (error bodies), CorsMiddleware, SecurityHeadersMiddleware, RateLimitMiddleware, JsonBodyMiddleware (JSON bodies are readable as $_POST) and ActionDispatcherMiddleware (routes in app/Config/routes.yml).

Docker

docker compose up --build

A development container on http://127.0.0.1:8080 (published on your machine only): it runs the migrations, then php taphra serve.

Adding a resource

  1. php taphra make:migration create comments table, describe the table with the SchemaBuilder (see app/Migrations/), then php taphra migrate.
  2. Add app/DataStores/CommentDataStore.php and app/Controllers/CommentsController.php (copy PostsController; new files named after their class need no composer dump-autoload).
  3. Declare the routes in app/Config/routes.yml, under the HTTP method of each action. An action that changes data must have a POST, PUT, PATCH or DELETE route, so the other methods get a 405.

Layout

app/
  Config/        app.yml (CORS, rate limit), routes.yml, databases.yml
  Controllers/   AuthController, PostsController, HomeController, AppController (base: token checks)
  Database/      the SQLite file
  DataStores/    UserDataStore, PostDataStore
  Http/          ProblemJsonMiddleware, CorsFromConfig
  Lib/           JwtSecret
  Migrations/    users and posts tables
  Static/        document root: index.php (the pipeline)
  bootstrap.php  runs on every request and command: .env, mode
composer.json
taphra           the command line

To use MySQL or PostgreSQL, edit app/Config/databases.yml.

Licence

This starter, like Taphra, is open-source software under the MIT licence (Copyright 2019-2026 Prodacity LLC); see LICENSE.