> For the complete documentation index, see [llms.txt](https://docs.appmixer.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.appmixer.com/appmixer-ui-sdk/api-module.md).

# API Module

The Appmixer SDK uses this API module internally to connect to the REST API.

| Name                      | Description                 |
| ------------------------- | --------------------------- |
| `api.set(name, value)`    | Set configuration property. |
| `api.get(name)`           | Get configuration property. |
| `api.on(event, handler)`  | Add event listener.         |
| `api.off(event, handler)` | Remove event listener.      |

## Methods

#### `api.authenticateUser`

```javascript
await appmixer.api.authenticateUser(username, password)
```

Authenticate a user to Appmixer. Note that this can be a "virtual" user that exists for the sole purpose of associating a real user of your own product to a user in Appmixer. Each user in Appmixer can have a set of flows, can run and stop flows, and can see data going through their flows. The returned promise is either resolved with an object that contains a token (which you need to set with `appmixer.set('accessToken', token)` to be able to make calls to the API backend. Or the promise is rejected with an error object. If the error object returns a 403 status code (i.e. `err.response.status === 403`), the user does not exist in Appmixer.

#### `api.authenticateWithEmailAndPassword`

```javascript
await appmixer.api.authenticateWithEmailAndPassword(email, password)
```

Similar to the `api.authenticateUser`, but uses email instead of username.

#### `api.signupUser`

```javascript
await appmixer.api.signupUser(username, password, [email])
```

Create a new user in Appmixer. The returned promise is either resolved with an authentication object (containing the `token` property) or rejected if the sign-up fails. If the `email` parameter is not provided, then the `username` will be copied as email.

#### `api.createFlow`

```javascript
await appmixer.api.createFlow(name, [descriptor], [properties])
```

`appmixer.api.createFlow(name, [descriptor], [properties])`Create a new flow in Appmixer. The returned promise resolves to the ID of the newly created flow. The `properties` object can contain your own custom metadata inside the `customFields` property. This is especially useful for filtering flows based on your own custom metadata.

#### `api.deleteFlow`

```javascript
await appmixer.api.deleteFlow(flowId)
```

Delete an existing flow identified by `flowId`.

#### `api.getFlow`

```javascript
await appmixer.api.getFlow(flowId)
```

Get flow. The returned promise resolves to an object with the following information: `{ id, flow, name, stage, btime, mtime, thumbnail }`, where `flow` is the Flow Descriptor, `stage` is either `'running'` or `'stopped'`, `btime` is the time the flow was created ("birth" time), `mtime` is the time the flow was modified and thumbnail contains a thumbnail image (self-contained, in the [Data URI](https://en.wikipedia.org/wiki/Data_URI_scheme) format).

#### `api.getFlows`

```javascript
await appmixer.api.getFlows(query)
```

Get all flows of the user or filter them by `query`. `query` is an object with the following properties: `limit`, `offset`, `pattern` (a string to filter flows containing pattern in their names), `sort`, `projection` (allows you to exclude properties from the returned flow objects), `sharedWithPermissions` and `filter`.Example:

```json
{
  limit: 20,
  offset: 0,
  pattern: "slack",
  projection: "-flow,-thumbnail",
  sort: "mtime:-1",
  sharedWithPermission: "read",
  filter: "userId:423jfdsalfjl4234fdsa"
}
```

#### `api.getFlowsCount`

```javascript
await appmixer.api.getFlowsCount(query)
```

Get the number of all flows of the user or filter them by `query`. `query` is an object with `pattern` property that can include a string to filter flows containing a pattern in their names. Example: `{ "pattern": "dropbox" }`.

#### `api.updateFlow`

```javascript
await appmixer.api.updateFlow(flowId, update)
```

Update an existing flow. `update` can contain the following information: `{ flow, name, customFields }`, where `flow` is the Flow Descriptor of the flow and `customFields` is an object with your own custom metadata for this flow.

#### `api.startFlow`

```javascript
await appmixer.api.startFlow(flowId)
```

Start a flow.

#### `api.stopFlow`

```javascript
await appmixer.api.stopFlow(flowId)
```

Stop a flow.

#### `api.cloneFlow`

```javascript
await appmixer.api.cloneFlow(flowId)
```

Create a copy of an existing flow. The returned promise resolves to the ID of the newly created flow.

#### `api.getUser`

```javascript
await appmixer.api.getUser()
```

Get current user. The returned promise resolves to an object with `username`.

#### `api.getStores`

```javascript
await appmixer.api.getStores()
```

Get all the data stores. The returned promise resolves to an array of stores each an object with `name` and `storeId` properties.

#### `api.getStore`

```javascript
await appmixer.api.getStore(storeId)
```

Get one store. The returned promise resolves to an object with `name` and `storeId` properties.

#### `api.getStoreRecordsCount`

```javascript
await appmixer.api.getStoreRecordsCount(query)
```

Get the number of records in a store. `query` is an object with the following properties:

* `storeId` - ID of the store.
* `pattern` - optional regular expression matched against the keys and the values of the records. Only records whose key or value matches it are counted. If not provided, all the records of the store are counted.
* `escapePattern` - optional, set to `true` to search for `pattern` literally instead of interpreting it as a regular expression. Use it when the pattern comes from a user input.

```javascript
await appmixer.api.getStoreRecordsCount({
    storeId: '5c6d643f4849f447eba55c1d',
    pattern: 'price (USD)',
    escapePattern: true
});
```

See [Searching Records](/api/data-stores.md#searching-records) in the Data Stores API reference for the details of `pattern` and `escapePattern`.

#### `api.getStoreRecords`

```javascript
await appmixer.api.getStoreRecords(query)
```

Get store records. `query` is an object with the following properties:

* `storeId` - ID of the store.
* `pattern` - optional regular expression matched against the keys and the values of the records. Only records whose key or value matches it are returned. If not provided, all the records of the store are returned.
* `escapePattern` - optional, set to `true` to search for `pattern` literally instead of interpreting it as a regular expression. Use it when the pattern comes from a user input.
* `limit` - maximum number of records returned. Defaults to `1000`.
* `offset` - index of the first record returned. Defaults to `0`.
* `sort` - record property to sort by, followed by `:` and the sort order `-1` (descending) or `1` (ascending).

Example:

```javascript
await appmixer.api.getStoreRecords({
    limit: 30,
    offset: 0,
    pattern: 'price (USD)',
    escapePattern: true,
    sort: 'updatedAt:-1',
    storeId: '5c6d643f4849f447eba55c1d'
});
```

See [Searching Records](/api/data-stores.md#searching-records) in the Data Stores API reference for the details of `pattern` and `escapePattern`.

#### `api.createStore`

```javascript
await appmixer.api.createStore(name)
```

Create a new store. The returned promise resolves to the ID of the newly created store.

#### `api.deleteStore`

```javascript
await appmixer.api.deleteStore(storeId)
```

Delete a store.

#### `api.renameStore`

```javascript
await appmixer.api.renameStore(storeId, newName)
```

Rename an existing store.

#### `api.createStoreItem`

```javascript
await appmixer.api.createStoreItem(storeId, key, value)
```

Create a new record in a store.

#### `api.deleteStoreItems`

```javascript
await appmixer.api.deleteStoreItems(items)
```

Delete store items. `items` is an array of objects each having a `key` and `storeId` properties identifying the item and store from which the item should be removed.

#### `api.createAccount`

```javascript
await appmixer.api.createAccount(params, data)
```

Create a custom account.

#### `api.getAccounts`

```javascript
await appmixer.api.getAccounts(filter)
```

Get a list of connected accounts of the user. `filter` is a custom query string (see the [GET /accounts](/api/accounts.md#get-all-accounts) for an example). The returned promise resolves to an array of objects of the form `{ accountId, name, displayName, service, icon, profileInfo }`.

#### `api.getComponentAccounts`

```javascript
await appmixer.api.getComponentAccounts(componentType, componentId)
```

Get a list of accounts connected to a specific component.

#### `api.getAccountFlows`

```javascript
await appmixer.api.getAccountFlows(accountId)
```

Get a list of flows this account is used in. The returned promise resolves to an array of objects with `flowId` and `name` properties.

#### `api.setAccountName`

```javascript
await appmixer.api.setAccountName(accountId, newName)
```

Rename a connected account. Note that this name is displayed in the Accounts widget and also in the Inspector UI of the Designer.

#### `api.getLogs`

```javascript
await appmixer.api.getLogs(query)
```

Get logs. The `query` is an object of the form `{ from, size, sort, query }`:

```json
{
  from: 0,
  size: 30,
  sort: "@timestamp:desc",
  query: "@timestamp:[2018-01-01 TO 2018-01-01]"
}
```

Get logs of a specific flow:

```json
{
  from: 0,
  size: 30,
  sort: "@timestamp:desc",
  query: "@timestamp:[2018-01-01 TO 2018-01-01] AND +flowId:FLOW_ID"
}
```

#### `api.getLog`

```javascript
await appmixer.api.getLog(logId, index)
```

Get one log. `logId` and `index` are values returned from `getLogs()`.

#### `api.getPeopleTasks`

```javascript
await appmixer.api.getPeopleTasks(query)
```

Get all tasks of the user. `query.role` can be either "approver" or "requester" and allows you to filter tasks based on the role. `query.pattern` filters returned tasks by a term that must be contained in the task title. Setting`query.secret` to either the `approverSecret` or `requesterSecret` allows you to get a list of tasks of a different user for which you have the secret (other than the one identified by the access token, i.e. the currently signed-in user).

#### `api.getPeopleTasksCount`

```javascript
await appmixer.api.getPeopleTasksCount(query)
```

Returns the number of tasks based on the query. See `getPeopleTasks(query)` for more info.

#### `api.getPeopleTask`

```javascript
await appmixer.api.getPeopleTask(id)
```

Return one task identified by `id`.

#### `api.approveTask`

```javascript
await appmixer.api.approveTask(id, [params])
```

Approve a task identified by `id`. `params` is an optional object that can contain the `secret` property (approver secret). Having the secret allows you to approve a task of any user for which you have the secret, not just the currently signed-in user.

#### `api.rejectTask`

```javascript
await appmixer.api.rejectTask(id, [params])
```

Reject a task identified by `id`. `params` is an optional object that can contain the `secret` property (approver secret). Having the secret allows you to reject a task of any user for which you have the secret, not just the currently signed-in user.

#### `api.getFlowAuthentication`

```javascript
await appmixer.api.getFlowAuthentication(flowId)
```

This request will return an object with all the components in the flow that have `auth` section with all the available accounts.

## Events <a href="#events" id="events"></a>

```javascript
appmixer.api.on(event, handler)
```

#### **`error`**

```javascript
appmixer.api.on('error', error => {
  if (error.code === 401) {
    /* A request failed because the current access token is invalid ... */
  }
}
```

The event is triggered when a request fails with an error or when the access token is invalid.

#### **`warning`**

```javascript
appmixer.api.on('warning', warning => { ... }
```

The event is triggered when API validation fails with a warning. It is also triggered when the [`externalUserId`](/appmixer-ui-sdk/constructor.md#externaluserid) you set may not reach the audit log: with `code` `externalUserIdPatternMismatch` when the audit log is likely to drop the value, and `externalUserIdNotSent` when a request header cannot carry it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.appmixer.com/appmixer-ui-sdk/api-module.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
