# Expose actions through one API endpoint

> Declare ROUTE('API ...') endpoints so a client can call many actions with POST {schema, data} on a single URL.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: 27b3adc10009244d13949af7ef18f0cad861057d5c38ebc55c6cf0d8e7a6b30a
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

An API route publishes actions under one URL. The client always sends `POST` with a JSON body `{ "schema": "name", "data": { ... } }`, and Total.js dispatches it to the action registered for that name. One URL, many operations: it is the style that jComponent's `API()` function speaks.

The prefix of the name decides what happens to `data`:

- `+name`: the input of the action is required and validated
- `-name`: the action takes no input
- `%name`: partial input (for patches)

```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');
};
```
