# Call an action from your own code

> Use ACTION() to run an action from a timer, a job or another action, with its own payload, query and user.

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

`ACTION(name, payload)` returns a caller that runs the action with all its validation, exactly as if it came from a route. The second argument is the payload (the input). Chain `.query()` and `.user()` to give it more data, and finish with `.callback(function(err, response) {})` or `.promise()` (use it with `await`). When the data is invalid, `err` is an error builder instead of an exception.

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

```js controllers/math.js
exports.install = function() {
	ROUTE('GET /sum/', sum);
	ROUTE('GET /sum-promise/', sum_promise);
};

function sum($) {
	ACTION('Math/add', { a: '2', b: '3' }).callback(function(err, response) {
		if (err)
			$.invalid(err);
		else
			$.json(response);
	});
}

async function sum_promise($) {
	try {
		var response = await ACTION('Math/add', { a: 10, b: 5 }).promise();
		$.json(response);
	} catch (err) {
		$.invalid(400);
	}
}
```
