---
title: APIs
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/apis/
updated: 2026-11-18T16:57:19.575Z
---

# APIs

> NEWAPI(), API() and its methods, TotalAPI() and their configuration keys.

The functions and configuration keys of named APIs, including the Total.js API service. The guide [APIs](../../guides/apis/) explains how they work together with examples; this page is the exact reference.

## API

### NEWAPI(name, [config], callback)

Type: `method`

Registers the code that calls one API service.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name, or names separated by commas. |
| `config` | `Object` | no |  | Default settings copied into `opt.config`. |
| `callback` | `Function` | yes |  | `function(opt, next)`; `next(err, response)` ends the call. |

Registers the handler of an API service. `this` in the handler is the API call. A name is found in the spelling given to `NEWAPI()` or in lower case; several names separated by commas share one handler. The name `*` handles every name without its own handler. A function alone, or a config object and a function, registers the name `default`. The optional `config` holds default settings: before each call its keys are copied into `opt.config`, unless the call already set them with `.configure()`. Pass `null` as the callback to remove a handler. `F.newapi()` is the same function.

Example: Register a service
```js
NEWAPI('Weather', { region: 'eu' }, function(opt, next) {
	REQUEST({ url: 'https://' + opt.config.region + '.weather.example/' + opt.schema + '/', method: 'POST', type: 'json', body: JSON.stringify(opt.data), callback: function(err, response) {
		next(err, response && response.body.parseJSON());
	}});
});
```

### function(opt, next)

Type: `type`

The options a NEWAPI() handler receives.

The first argument of a `NEWAPI()` handler. It holds `opt.name` (the lower-case name of the service), `opt.schema` (the operation), `opt.data`, `opt.controller` (the `$` of the call), `opt.output` (set by `.output()`), `opt.files` (an array of `{ name, filename, path }` added by `.file()`), `opt.retries` (how many times the call was retried), `opt.config` (the config of `NEWAPI()` merged with `.configure()`) and `opt.api` (the API call). `opt.retry()` runs the handler again on the next tick and increases `opt.retries`.

Example: Retry
```js
NEWAPI('Weather', function(opt, next) {
	callservice(opt.schema, opt.data, function(err, response) {
		if (err && opt.retries < 3)
			opt.retry();
		else
			next(err, response);
	});
});
```

### API(name, schema, data, [$])

Type: `method`

Calls a service registered with NEWAPI().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the service. |
| `schema` | `String` | yes |  | The operation, passed as `opt.schema`. |
| `data` | `Object` | yes |  | The payload, passed as `opt.data`. |
| `$` | `Object` | no |  | The `$` of an action or a controller. |

Returns: `APICall` The call with the methods below.

Creates the call and runs the handler on the next tick, so you configure the call with its methods right away. `$` becomes `opt.controller`. When no handler matches (and no `*` handler exists), the call fails with `API is not initialized`. `F.api()` is the same function.

Example: Inside an action
```js
NEWACTION('weather_read', {
	input: '*city:String',
	action: function($, model) {
		API('Weather', 'current', model).callback($);
	}
});
```

### API().callback(fn)

Type: `method`

Sets the callback of the call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function|Object` | yes |  | A callback, or `$`. |

Returns: `APICall`

`fn` is `function(err, response)`, or the `$` of an action, whose `$.callback()` receives the result. `.pipe()` is an alias.

Example: Callback
```js
API('Weather', 'current', { city: 'Oslo' }).callback(function(err, response) {
	console.log(err, response);
});
```

### API().promise([$])

Type: `method`

Returns a promise of the call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | no |  | The `$` of an action. |

Returns: `Promise`

The promise resolves with the response. On an error it rejects with an `Error`; when `$` is given, the error goes to `$.invalid()` instead.

Example: Await
```js
var weather = await API('Weather', 'current', { city: 'Oslo' }).promise($);
```

### API().data(fn)

Type: `method`

Runs a function with the response of a successful call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(response)`. |

Returns: `APICall`

`fn(response)` runs before the callback.

Example: Data
```js
API('Weather', 'current', { city: 'Oslo' }).data(function(response) {
	MAIN.weather = response;
});
```

### API().fail(fn)

Type: `method`

Runs a function with the error of a failed call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(err)`. |

Returns: `APICall`

`fn(err)` runs before the callback.

Example: Fail
```js
API('Weather', 'current', { city: 'Oslo' }).fail(function(err) {
	console.error(err);
});
```

### API().error(message, [reverse])

Type: `method`

Turns an empty response into an error.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `String` | yes |  | The error. |
| `reverse` | `Boolean` | no | `false` | Fail on a non-empty response. |

Returns: `APICall`

When the handler answers without an error but the response is empty (a falsy value or an empty array), the call fails with `message`. With `reverse`, it fails when the response is truthy (an empty array too). `.err()` is an alias.

Example: Not found
```js
API('Weather', 'current', model).error('City not found').callback($);
```

### API().output(type)

Type: `method`

Sets the output format of the call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The format, e.g. `json`, `base64`, `stream`. |

Returns: `APICall`

The value is stored in `opt.output` for the handler. See `TotalAPI()` for the formats of the `TAPI` service.

Example: Base64
```js
API('Weather', 'map', { city: 'Oslo' }).output('base64').callback(console.log);
```

### API().file(filename, path, [name])

Type: `method`

Adds a file to the call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filename` | `String` | yes |  | The name of the file. |
| `path` | `String` | yes |  | The path of the file on the disk. |
| `name` | `String` | no | `file + index` | The field name. |

Returns: `APICall`

The file is added to `opt.files` as `{ name, filename, path }`. The default name is `file0`, `file1`, ...

Example: Upload
```js
API('Storage', 'upload', { folder: 'invoices' }).file('invoice.png', PATH.public('invoice.png')).callback(console.log);
```

### API().controller($)

Type: `method`

Sets the controller of the call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | yes |  | The `$` of an action or a controller. |

Returns: `APICall`

Stores `$.controller` (or `$` itself when it has none) in `opt.controller`.

Example: Controller
```js
API('Weather', 'current', model).controller($).callback($);
```

### API().audit($, message, [type])

Type: `method`

Writes an audit log after a successful call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | yes |  | The `$` of an action. |
| `message` | `String` | yes |  | The message. |
| `type` | `String` | no |  | The type of the log. |

Returns: `APICall`

Calls `$.audit(message, type)` once the call succeeds. Placeholders in the message such as `{data.id}` are filled from the data of the call, and `{user.name}` from the user of `$`.

Example: Audit
```js
API('Billing', 'invoice_create', model).audit($, 'Invoice for {data.email}').callback($);
```

### API().debug()

Type: `method`

Prints the result of the call to the console.

Returns: `APICall`

Prints the name, the operation, the error and the response.

Example: Debug
```js
API('Weather', 'current', { city: 'Oslo' }).debug().callback(NOOP);
```

### API().configure(obj)

Type: `method` · Since: `0.0.16`

Sets configuration keys of the call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | yes |  | The keys to set. |

Returns: `APICall`

Copies the keys of `obj` into `opt.config`. They override the config given to `NEWAPI()`. The `TAPI` service reads `token` from it.

Example: Another token
```js
API('TAPI', 'check', {}).configure({ token: CONF.customer_token }).callback(console.log);
```

### TotalAPI(schema, data, [callback])

Type: `method`

Calls an operation of Total.js API.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `schema` | `String` | yes |  | The operation, e.g. `check`. |
| `data` | `Object` | yes |  | The payload. |
| `callback` | `Function` | no |  | `function(err, response)`. |

Returns: `APICall` Configure it like any `API()` call.

The same as `API('TAPI', schema, data)`. The `TAPI` (or `TotalAPI`) service sends a `POST` request to `https://<$tapiurl>.api.totaljs.com/<schema>/` with the token from `.configure({ token })`, `CONF.totalapi`, `CONF.secret_totalapi` or `CONF.$tapisecret` and the app name. When `$tapi` is `false`, every operation except `check` fails with `totalapi_inactive`. A non-object `data` is sent as `{ value: data }`. With `.output()` the response is `json` (default), `html`, `plain`, `text`, `buffer`, `binary`, `base64`, `stream`, or `#storage [id] [filename]`, which saves it into that FileStorage. A response with a status above `200` fails with the error from Total API.

Example: Check the token
```js
TotalAPI('check', {}, function(err, response) {
	console.log(err, response);
});
```

Example: Save into FileStorage
```js
// operation: a Total API operation that returns a file
API('TAPI', operation, data).output('#files').callback(console.log);
```

### totalapi : token

Type: `config`

The token of Total.js API.

Returns: `String`

A string longer than five characters is stored as `CONF.secret_totalapi` and sent by the `TAPI` service. Any other value (`true`, `false`) is stored as `$tapi`.

Example: Config
```js
// /config
totalapi       : your-totalapi-token
```

### $tapi : true

Type: `config`

Allows calls of the TAPI service.

Returns: `Boolean`

Default `true`. When `false`, every `TAPI` operation except `check` fails with `totalapi_inactive`.

### $tapiurl : eu

Type: `config`

The region of Total.js API.

Returns: `String`

Default `eu`. Requests go to `https://<$tapiurl>.api.totaljs.com/`.
