taphra / api-starter
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.
Requires
- php: ^8.2
- taphra/taphra: ^1.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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_sqliteextension (php -mlists 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 theAuthorizationheader, never in cookies, so there is no CSRF token to send. They carrytoken_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_minuteinapp/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_originsinapp/Config/app.yml. - Behind a proxy or load balancer, list it under
trusted_proxiesso the rate limit sees the real client IP. - An unset
APP_ENVmeans production: no debug pages.php taphra servesetsAPP_ENV=devfor 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
php taphra make:migration create comments table, describe the table with the SchemaBuilder (seeapp/Migrations/), thenphp taphra migrate.- Add
app/DataStores/CommentDataStore.phpandapp/Controllers/CommentsController.php(copyPostsController; new files named after their class need nocomposer dump-autoload). - Declare the routes in
app/Config/routes.yml, under the HTTP method of each action. An action that changes data must have aPOST,PUT,PATCHorDELETEroute, 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.