# Commands

Sending commands to devices and stored command management

## Fetch a list of Saved Commands

`GET /commands`

Without params, it returns a list of Saved Commands the user has access to

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Parameters:

- `all` (query, boolean, optional) - Can only be used by admins or managers to fetch all entities
  Type: boolean

- `userId` (query, integer, optional) - Standard users can use this only with their own _userId_
  Type: integer

- `deviceId` (query, integer, optional) - Standard users can use this only with _deviceId_s, they have access to
  Type: integer

- `groupId` (query, integer, optional) - Standard users can use this only with _groupId_s, they have access to
  Type: integer

- `refresh` (query, boolean, optional)
  Type: boolean

- `limit` (query, integer, optional) - Limit the number of returned results
  Type: integer

- `offset` (query, integer, optional) - Offset for pagination
  Type: integer

- `keyword` (query, string, optional) - Search keyword filter
  Type: string

Responses:

- 200: OK

Content type: application/json

Type: Array<Command>

Items:
  Reference: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md); Type: Command

- 401: Unauthorized

## Examples

### cURL

```bash
curl -X GET 'https://demo.traccar.org/api/commands' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE'
```

### JavaScript

```javascript
const response = await fetch('https://demo.traccar.org/api/commands', {
  method: 'GET',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
  },
});

const data = await response.text();
```

### Python

```python
import requests

response = requests.get(
    'https://demo.traccar.org/api/commands',
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    },
)
print(response.text)
```


## Create a Saved Command

`POST /commands`

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Request body: application/json (required)

Type: Command

Schema: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md)

- `id` (optional)
  Type: integer, int64
  
  Unique saved command identifier

- `deviceId` (optional)
  Type: integer, int64
  
  Target device identifier when the command is bound to one device

- `description` (optional)
  Type: string
  
  User friendly label displayed in the UI

- `type` (optional)
  Type: string
  
  Command type as defined by the device protocol

- `textChannel` (optional)
  Type: boolean
  
  Whether to send the command using the SMS channel

- `attributes` (optional)
  Type: object
  
  Additional parameters required by the command type

Responses:

- 200: OK

Content type: application/json

Type: Command

Schema: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md)

- `id` (optional)
  Type: integer, int64
  
  Unique saved command identifier

- `deviceId` (optional)
  Type: integer, int64
  
  Target device identifier when the command is bound to one device

- `description` (optional)
  Type: string
  
  User friendly label displayed in the UI

- `type` (optional)
  Type: string
  
  Command type as defined by the device protocol

- `textChannel` (optional)
  Type: boolean
  
  Whether to send the command using the SMS channel

- `attributes` (optional)
  Type: object
  
  Additional parameters required by the command type

- 400: Bad request

- 401: Unauthorized

## Examples

### cURL

```bash
curl -X POST 'https://demo.traccar.org/api/commands' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}'
```

### JavaScript

```javascript
const body = `{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}`;

const response = await fetch('https://demo.traccar.org/api/commands', {
  method: 'POST',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    'Content-Type': 'application/json',
  },
  body,
});

const data = await response.text();
```

### Python

```python
import requests

body = '''{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}'''

response = requests.post(
    'https://demo.traccar.org/api/commands',
    data=body.encode('utf-8'),
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
        'Content-Type': 'application/json',
    },
)
print(response.text)
```


## Fetch a Saved Command

`GET /commands/{id}`

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Parameters:

- `id` (path, integer, required)
  Type: integer

Responses:

- 200: OK

Content type: application/json

Type: Command

Schema: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md)

- `id` (optional)
  Type: integer, int64
  
  Unique saved command identifier

- `deviceId` (optional)
  Type: integer, int64
  
  Target device identifier when the command is bound to one device

- `description` (optional)
  Type: string
  
  User friendly label displayed in the UI

- `type` (optional)
  Type: string
  
  Command type as defined by the device protocol

- `textChannel` (optional)
  Type: boolean
  
  Whether to send the command using the SMS channel

- `attributes` (optional)
  Type: object
  
  Additional parameters required by the command type

- 401: Unauthorized

- 404: Not found

## Examples

### cURL

```bash
curl -X GET 'https://demo.traccar.org/api/commands/0' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE'
```

### JavaScript

```javascript
const response = await fetch('https://demo.traccar.org/api/commands/0', {
  method: 'GET',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
  },
});

const data = await response.text();
```

### Python

```python
import requests

response = requests.get(
    'https://demo.traccar.org/api/commands/0',
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    },
)
print(response.text)
```


## Update a Saved Command

`PUT /commands/{id}`

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Parameters:

- `id` (path, integer, required)
  Type: integer

Request body: application/json (required)

Type: Command

Schema: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md)

- `id` (optional)
  Type: integer, int64
  
  Unique saved command identifier

- `deviceId` (optional)
  Type: integer, int64
  
  Target device identifier when the command is bound to one device

- `description` (optional)
  Type: string
  
  User friendly label displayed in the UI

- `type` (optional)
  Type: string
  
  Command type as defined by the device protocol

- `textChannel` (optional)
  Type: boolean
  
  Whether to send the command using the SMS channel

- `attributes` (optional)
  Type: object
  
  Additional parameters required by the command type

Responses:

- 200: OK

Content type: application/json

Type: Command

Schema: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md)

- `id` (optional)
  Type: integer, int64
  
  Unique saved command identifier

- `deviceId` (optional)
  Type: integer, int64
  
  Target device identifier when the command is bound to one device

- `description` (optional)
  Type: string
  
  User friendly label displayed in the UI

- `type` (optional)
  Type: string
  
  Command type as defined by the device protocol

- `textChannel` (optional)
  Type: boolean
  
  Whether to send the command using the SMS channel

- `attributes` (optional)
  Type: object
  
  Additional parameters required by the command type

- 400: Bad request

- 401: Unauthorized

## Examples

### cURL

```bash
curl -X PUT 'https://demo.traccar.org/api/commands/0' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}'
```

### JavaScript

```javascript
const body = `{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}`;

const response = await fetch('https://demo.traccar.org/api/commands/0', {
  method: 'PUT',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    'Content-Type': 'application/json',
  },
  body,
});

const data = await response.text();
```

### Python

```python
import requests

body = '''{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}'''

response = requests.put(
    'https://demo.traccar.org/api/commands/0',
    data=body.encode('utf-8'),
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
        'Content-Type': 'application/json',
    },
)
print(response.text)
```


## Delete a Saved Command

`DELETE /commands/{id}`

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Parameters:

- `id` (path, integer, required)
  Type: integer

Responses:

- 204: No Content

- 401: Unauthorized

## Examples

### cURL

```bash
curl -X DELETE 'https://demo.traccar.org/api/commands/0' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE'
```

### JavaScript

```javascript
const response = await fetch('https://demo.traccar.org/api/commands/0', {
  method: 'DELETE',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
  },
});

const data = await response.text();
```

### Python

```python
import requests

response = requests.delete(
    'https://demo.traccar.org/api/commands/0',
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    },
)
print(response.text)
```


## Fetch a list of Saved Commands supported by Device at the moment

`GET /commands/send`

Return a list of saved commands linked to Device and its groups, filtered by current Device protocol support

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Parameters:

- `deviceId` (query, integer, required) - Standard users can use this only with _deviceId_s, they have access to
  Type: integer

Responses:

- 200: OK

Content type: application/json

Type: Array<Command>

Items:
  Reference: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md); Type: Command

- 400: Could happen when the user doesn't have permission for the device

## Examples

### cURL

```bash
curl -X GET 'https://demo.traccar.org/api/commands/send?deviceId=0' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE'
```

### JavaScript

```javascript
const response = await fetch('https://demo.traccar.org/api/commands/send?deviceId=0', {
  method: 'GET',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
  },
});

const data = await response.text();
```

### Python

```python
import requests

response = requests.get(
    'https://demo.traccar.org/api/commands/send?deviceId=0',
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    },
)
print(response.text)
```


## Dispatch commands to device

`POST /commands/send`

Dispatch a new command or Saved Command if _body.id_ set

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Parameters:

- `groupId` (query, integer, optional) - Send the command to all devices in the group
  Type: integer

Request body: application/json (required)

Type: Command

Schema: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md)

- `id` (optional)
  Type: integer, int64
  
  Unique saved command identifier

- `deviceId` (optional)
  Type: integer, int64
  
  Target device identifier when the command is bound to one device

- `description` (optional)
  Type: string
  
  User friendly label displayed in the UI

- `type` (optional)
  Type: string
  
  Command type as defined by the device protocol

- `textChannel` (optional)
  Type: boolean
  
  Whether to send the command using the SMS channel

- `attributes` (optional)
  Type: object
  
  Additional parameters required by the command type

Responses:

- 200: Command sent

Content type: application/json

Type: Command

Schema: [Command](https://sourcey-traccar.pages.dev/api-reference/models/command/index.md)

- `id` (optional)
  Type: integer, int64
  
  Unique saved command identifier

- `deviceId` (optional)
  Type: integer, int64
  
  Target device identifier when the command is bound to one device

- `description` (optional)
  Type: string
  
  User friendly label displayed in the UI

- `type` (optional)
  Type: string
  
  Command type as defined by the device protocol

- `textChannel` (optional)
  Type: boolean
  
  Whether to send the command using the SMS channel

- `attributes` (optional)
  Type: object
  
  Additional parameters required by the command type

- 202: Command queued

Content type: application/json

Type: QueuedCommand | Array<QueuedCommand>

anyOf:
  Reference: [QueuedCommand](https://sourcey-traccar.pages.dev/api-reference/models/queuedcommand/index.md); Type: QueuedCommand

  Type: Array<QueuedCommand>
  
  Items:
    Reference: [QueuedCommand](https://sourcey-traccar.pages.dev/api-reference/models/queuedcommand/index.md); Type: QueuedCommand

- 400: Could happen when the user doesn't have permission or an incorrect command _type_ for the device

## Examples

### cURL

```bash
curl -X POST 'https://demo.traccar.org/api/commands/send' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}'
```

### JavaScript

```javascript
const body = `{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}`;

const response = await fetch('https://demo.traccar.org/api/commands/send', {
  method: 'POST',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    'Content-Type': 'application/json',
  },
  body,
});

const data = await response.text();
```

### Python

```python
import requests

body = '''{
  "id": 0,
  "deviceId": 0,
  "description": "string",
  "type": "string",
  "textChannel": true,
  "attributes": {}
}'''

response = requests.post(
    'https://demo.traccar.org/api/commands/send',
    data=body.encode('utf-8'),
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
        'Content-Type': 'application/json',
    },
)
print(response.text)
```


## Fetch a list of available Commands for the Device or all possible Commands if Device ommited

`GET /commands/types`

Authentication (security requirement alternatives):

```json
[
  {
    "BasicAuth": []
  },
  {
    "ApiKey": []
  }
]
```

- BasicAuth: http (basic)
- ApiKey: http (bearer)

Parameters:

- `deviceId` (query, integer, optional) - Internal device identifier. Only works if device has already reported some locations
  Type: integer

- `textChannel` (query, boolean, optional) - When `true` return SMS commands. If not specified or `false` return data commands
  Type: boolean

Responses:

- 200: OK

Content type: application/json

Type: Array<CommandType>

Items:
  Reference: [CommandType](https://sourcey-traccar.pages.dev/api-reference/models/commandtype/index.md); Type: CommandType

- 400: Could happen when trying to fetch from a device the user does not have permission

## Examples

### cURL

```bash
curl -X GET 'https://demo.traccar.org/api/commands/types' \
  -H 'Authorization: Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE'
```

### JavaScript

```javascript
const response = await fetch('https://demo.traccar.org/api/commands/types', {
  method: 'GET',
  headers: {
    'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
  },
});

const data = await response.text();
```

### Python

```python
import requests

response = requests.get(
    'https://demo.traccar.org/api/commands/types',
    headers={
        'Authorization': 'Basic WU9VUl9VU0VSTkFNRTpZT1VSX1BBU1NXT1JE',
    },
)
print(response.text)
```
