Skip to content

Nette APIRobust REST APIs inside your Nette application

Add a complete API layer to Nette: versioned endpoints, authorization, validated params and outputs, CORS, rate limiting, logging and OpenAPI docs – wired through Nette DI, one handler class per endpoint.

What it looks like ​

Open the diagram at full size

A handler is one class:

php
<?php

declare(strict_types=1);

namespace App\Api;

use Tomaj\NetteApi\Handlers\BaseHandler;
use Tomaj\NetteApi\Params\GetInputParam;
use Tomaj\NetteApi\Response\JsonApiResponse;
use Tomaj\NetteApi\Response\ResponseInterface;

final class HelloHandler extends BaseHandler
{
    public function summary(): string
    {
        return 'Say hello';
    }

    public function params(): array
    {
        return [
            (new GetInputParam('name'))->setDefault('world')->setDescription('Who to greet'),
        ];
    }

    public function handle(array $params): ResponseInterface
    {
        return new JsonApiResponse(200, ['message' => 'Hello ' . $params['name'] . '!']);
    }
}

and a few lines of configuration register it as GET /api/v1/hello:

yaml
application:
	mapping:
		# Api:Api:default is Tomaj\NetteApi\Presenters\ApiPresenter
		Api: Tomaj\NetteApi\Presenters\*Presenter

services:
	router: App\RouterFactory::create

	# Nette 3.3 creates presenters only from the DI container
	apiPresenter:
		factory: Tomaj\NetteApi\Presenters\ApiPresenter

	# required by ApiPresenter: when to validate output schemas and show error details
	- Tomaj\NetteApi\Output\Configurator\DebuggerConfigurator
	# required by ApiPresenter: turns errors into JSON responses
	- Tomaj\NetteApi\Error\DefaultErrorHandler

	apiDecider:
		factory: Tomaj\NetteApi\ApiDecider
		setup:
			# GET /api/v1/hello
			- addApi(
				Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'hello'),
				App\Api\HelloHandler(),
				Tomaj\NetteApi\Authorization\NoAuthorization()
			)

The hello world example runs exactly this code in CI.

Where to go next ​

FAQ ​

What is Nette API? ​

A library that adds a REST API layer to a Nette application: endpoints, validated params and outputs, authorization, CORS, rate limits, logging and an OpenAPI document. See Getting started.

How does it fit into a Nette application? ​

Through one route to its ApiPresenter and services registered in your neon config, so handlers get their dependencies from Nette DI like any other service. See Getting started.

Which versions of PHP and Nette does Nette API 3.x need? ​

PHP 8.3 or newer, nette/application and nette/http 3.x, and Latte 3. The full list is in Getting started.

Do I have to use presenters for my endpoints? ​

No. The library ships its own ApiPresenter, and your code lives in handlers. See Handlers.

How do API versions work? ​

The version is part of every endpoint, so v1 and v2 of the same URL can point to different handlers. See Links and versions and the versions example.

How do I protect an endpoint? ​

Give it an authorization when you register it: bearer token, API key in a header, query or cookie, or your own class. See Authorization.

Does it slow down every request? ​

Handlers registered by class name are created from the DI container only when their endpoint is called. The exception is the OpenAPI, listing and console endpoints: they read every endpoint through getApis(), which creates all handlers. See Architecture.

Can I generate API documentation from my code? ​

Yes. Register OpenApiHandler and it describes every endpoint as OpenAPI 3.0, in JSON or YAML. See OpenAPI.

Is the documentation available in Slovak? ​

Yes, every page has a Slovak version. Use the language menu at the top, or start at the Slovak home page.

How do I upgrade to 4.0? ​

There is no 4.0 yet; 3.x is the current line. A 4.0.0 milestone is open with planned breaking changes, such as removing the unsafe 'auto' CORS mode and raising the minimum PHP version. It has no release date yet. The upgrade guide covers older major versions.

Released under the MIT License.