# Esporre azioni tramite un unico endpoint API

> Dichiara endpoint ROUTE('API ...') così un client può chiamare molte azioni con POST {schema, data} su un'unica URL.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: it
Content-SHA256: 40f0a6bd30c161871fcf0a730fd2d7757d1b960cd2deddf0a7518f6c99f90d6a
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

Una route API pubblica azioni sotto un'unica URL. Il client invia sempre `POST` con un body JSON `{ "schema": "name", "data": { ... } }` e Total.js lo smista all'azione registrata con quel nome. Una URL, molte operazioni: è lo stile che parla la funzione `API()` di jComponent.

Il prefisso del nome decide cosa succede a `data`:

- `+name`: l'input dell'azione è obbligatorio e viene validato
- `-name`: l'azione non accetta input
- `%name`: input parziale (per le patch)

```js definitions/math.js
NEWACTION('Math/add', {
	name: 'Add two numbers',
	input: '*a:Number, *b:Number',
	action: function($, model) {
		$.callback({ sum: model.a + model.b });
	}
});

NEWACTION('Math/time', {
	name: 'Server time',
	action: function($) {
		$.callback({ year: new Date().getFullYear() });
	}
});
```

```js controllers/api.js
exports.install = function() {
	ROUTE('API /api/ +math_add --> Math/add');
	ROUTE('API /api/ -math_time --> Math/time');
};
```
