Examples
Each example is a small Nette application in examples/. Its e2e.sh starts the PHP built-in server and checks every response with curl; CI runs all of them against the current code of the library.
git clone https://github.com/tomaj/nette-api.git
cd nette-api/examples/hello-world
composer install
./e2e.shLibrary version
The examples require tomaj/nette-api ^3.6, so composer install gets the latest 3.x release. Some of them use features added in 3.6.0, for example the masked headers in Logging and errors.
To run an example against your local checkout, as CI does, use examples/ci/example.sh hello-world from the repository root.
- Hello worldOne GET endpoint, the smallest working setup.
- Params and validationQuery params, allowed values, multi values and a JSON body with a schema.
- Authorization and rate limitBearer token, API key, HTTP Basic and 3 requests per minute.
- CORSA fixed origin and preflight for a browser frontend.
- OpenAPI and consoleOpenAPI JSON and YAML, endpoint listing, web console, {apiLink}.
- Logging and errorsRequest log with masked headers (needs 3.6.0), own error handler, production mode.
- Custom authorizationYour own ApiAuthorizationInterface with roles, the user in the handler, token or API key.
- JSON and raw bodyA JSON body checked by a schema, and a CSV body.
- File uploadOne file and many files, size and real type checked.
- PaginationTyped page params, allowed values, links, an output for every code.
- API versionsv1 and v2 of one endpoint, deprecation headers, OpenAPI per version.
- Rate limitYour own RateLimitInterface, 3 requests per hour per key, a custom 429.
- TestingPHPUnit tests for a handler, params, outputs and authorization.
All examples need PHP 8.3 or newer and Composer. They use composer.json with tomaj/nette-api: ^3.6.
FAQ
Which example should I start with?
Hello world, then the one for the feature you need.
Do the examples use a database?
No, data are in memory or in files, so composer install is the only setup.
Can I copy an example as a starting point?
Yes, the config, the router and the bootstrap are a complete application. Read Getting started for what each part does.
How does CI test them?
examples/ci/example.sh NAME copies the project, points Composer at the checked out library and runs e2e.sh.
Which ports do they use?
8081 to 8087 and 8090 to 8096, one per example (logging and errors uses two), so they can run side by side.