# Update existing rule.

`PUT /v1/rules/{id}`

Update existing rule.

Authentication (security requirement alternatives):

```json
[
  {
    "firefly_iii_auth": []
  },
  {
    "local_bearer_auth": []
  }
]
```

- firefly_iii_auth: oauth2. OAuth2 authorization code flow. Create a client on your installation's `/profile` page; it authorizes at `/oauth/authorize` and issues tokens at `/oauth/token` there. The URLs below are the demo site's. [How to use the API](https://sourcey-firefly.pages.dev/how-to/firefly-iii/features/api/index.md) walks through it.
- local_bearer_auth: http (bearer). A Personal Access Token, sent as a bearer token. Create one on your installation under Options > Profile > OAuth > Personal Access Tokens.

Parameters:

- `X-Trace-Id` (header, string, uuid, optional) - Unique identifier associated with this request.
  Type: string, uuid
  
  Example:
  
  ```json
  "40c71bbb-c676-4f24-83cf-cc725d7d7a00"
  ```

- `id` (path, string, required) - The ID of the object.
  Type: string
  
  Example:
  
  ```json
  "123"
  ```

Request body: application/json (required)

JSON array with updated rule information. See the model for the exact specifications.

Type: object

Schema: [RuleUpdate](https://sourcey-firefly.pages.dev/api/models/ruleupdate/index.md)

- `title` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "First rule title."
  ```

- `description` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "First rule description"
  ```

- `rule_group_id` (optional)
  Type: string, string
  
  ID of the rule group under which the rule must be stored. Either this field or rule_group_title is mandatory.
  
  Example:
  
  ```json
  "81"
  ```

- `order` (optional)
  Type: integer, int32
  
  Example:
  
  ```json
  5
  ```

- `trigger` (optional)
  Reference: [RuleTriggerType](https://sourcey-firefly.pages.dev/api/models/ruletriggertype/index.md); Type: string, string
  
  Which action is necessary for the rule to fire? Use either store-journal, update-journal or manual-activation.
  
  enum: ["store-journal","update-journal","manual-activation"]
  
  Example:
  
  ```json
  "store-journal"
  ```

- `active` (optional)
  Type: boolean
  
  Whether or not the rule is even active. Default is true.
  
  default: true
  
  Example:
  
  ```json
  true
  ```

- `strict` (optional)
  Type: boolean
  
  If the rule is set to be strict, ALL triggers must hit in order for the rule to fire. Otherwise, just one is enough. Default value is true.
  
  Example:
  
  ```json
  true
  ```

- `stop_processing` (optional)
  Type: boolean
  
  If this value is true and the rule is triggered, other rules  after this one in the group will be skipped. Default value is false.
  
  default: false
  
  Example:
  
  ```json
  false
  ```

- `triggers` (optional)
  Type: array
  
  Items:
    Reference: [RuleTriggerUpdate](https://sourcey-firefly.pages.dev/api/models/ruletriggerupdate/index.md); Type: object

- `actions` (optional)
  Type: array
  
  Items:
    Reference: [RuleActionUpdate](https://sourcey-firefly.pages.dev/api/models/ruleactionupdate/index.md); Type: object

Request body: application/x-www-form-urlencoded (required)

JSON array with updated rule information. See the model for the exact specifications.

Type: object

Schema: [RuleUpdate](https://sourcey-firefly.pages.dev/api/models/ruleupdate/index.md)

- `title` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "First rule title."
  ```

- `description` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "First rule description"
  ```

- `rule_group_id` (optional)
  Type: string, string
  
  ID of the rule group under which the rule must be stored. Either this field or rule_group_title is mandatory.
  
  Example:
  
  ```json
  "81"
  ```

- `order` (optional)
  Type: integer, int32
  
  Example:
  
  ```json
  5
  ```

- `trigger` (optional)
  Reference: [RuleTriggerType](https://sourcey-firefly.pages.dev/api/models/ruletriggertype/index.md); Type: string, string
  
  Which action is necessary for the rule to fire? Use either store-journal, update-journal or manual-activation.
  
  enum: ["store-journal","update-journal","manual-activation"]
  
  Example:
  
  ```json
  "store-journal"
  ```

- `active` (optional)
  Type: boolean
  
  Whether or not the rule is even active. Default is true.
  
  default: true
  
  Example:
  
  ```json
  true
  ```

- `strict` (optional)
  Type: boolean
  
  If the rule is set to be strict, ALL triggers must hit in order for the rule to fire. Otherwise, just one is enough. Default value is true.
  
  Example:
  
  ```json
  true
  ```

- `stop_processing` (optional)
  Type: boolean
  
  If this value is true and the rule is triggered, other rules  after this one in the group will be skipped. Default value is false.
  
  default: false
  
  Example:
  
  ```json
  false
  ```

- `triggers` (optional)
  Type: array
  
  Items:
    Reference: [RuleTriggerUpdate](https://sourcey-firefly.pages.dev/api/models/ruletriggerupdate/index.md); Type: object

- `actions` (optional)
  Type: array
  
  Items:
    Reference: [RuleActionUpdate](https://sourcey-firefly.pages.dev/api/models/ruleactionupdate/index.md); Type: object

Responses:

- 200: Updated rule stored, result in response

Content type: application/vnd.api+json

Type: object

Schema: [RuleSingle](https://sourcey-firefly.pages.dev/api/models/rulesingle/index.md)

- `data` (required)
  Reference: [RuleRead](https://sourcey-firefly.pages.dev/api/models/ruleread/index.md); Type: object

- 400: Bad request

Content type: application/json

Type: object

Schema: [BadRequestResponse](https://sourcey-firefly.pages.dev/api/models/badrequestresponse/index.md)

- `message` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "Bad Request"
  ```

- `exception` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "BadRequestHttpException"
  ```

- 401: Unauthenticated

Content type: application/json

Type: object

Schema: [UnauthenticatedResponse](https://sourcey-firefly.pages.dev/api/models/unauthenticatedresponse/index.md)

- `message` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "Unauthenticated"
  ```

- `exception` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "AuthenticationException"
  ```

- 404: Page not found

Content type: application/json

Type: object

Schema: [NotFoundResponse](https://sourcey-firefly.pages.dev/api/models/notfoundresponse/index.md)

- `message` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "Resource not found"
  ```

- `exception` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "NotFoundHttpException"
  ```

- 422: Validation error. The body will have the exact details.

Content type: application/json

Type: object

Schema: [ValidationErrorResponse](https://sourcey-firefly.pages.dev/api/models/validationerrorresponse/index.md)

- `message` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "The given data was invalid."
  ```

- `errors` (optional)
  Type: object
  
  - `email` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `force` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `blocked` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `field` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `role` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `blocked_code` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `name` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `type` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `iban` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `start` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `end` (optional)
    Type: array
    
    Items:
      Type: string, string
  
  - `date` (optional)
    Type: array
    
    Items:
      Type: string, string

- 500: Internal exception

Content type: application/json

Type: object

Schema: [InternalExceptionResponse](https://sourcey-firefly.pages.dev/api/models/internalexceptionresponse/index.md)

- `message` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "Internal Exception"
  ```

- `exception` (optional)
  Type: string, string
  
  Example:
  
  ```json
  "InternalException"
  ```

## Examples

### cURL

```bash
curl -X PUT 'https://firefly.example.com/api/v1/rules/123' \
  -H 'X-Trace-Id: 40c71bbb-c676-4f24-83cf-cc725d7d7a00' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "title": "First rule title.",
  "description": "First rule description",
  "rule_group_id": "81",
  "order": 5,
  "trigger": "store-journal",
  "active": true,
  "strict": true,
  "stop_processing": false,
  "triggers": [
    {
      "type": "user_action",
      "value": "tag1",
      "order": 5,
      "active": true,
      "stop_processing": false
    }
  ],
  "actions": [
    {
      "type": "set_category",
      "value": "Daily groceries",
      "order": 5,
      "active": true,
      "stop_processing": false
    }
  ]
}'
```

### JavaScript

```javascript
const body = `{
  "title": "First rule title.",
  "description": "First rule description",
  "rule_group_id": "81",
  "order": 5,
  "trigger": "store-journal",
  "active": true,
  "strict": true,
  "stop_processing": false,
  "triggers": [
    {
      "type": "user_action",
      "value": "tag1",
      "order": 5,
      "active": true,
      "stop_processing": false
    }
  ],
  "actions": [
    {
      "type": "set_category",
      "value": "Daily groceries",
      "order": 5,
      "active": true,
      "stop_processing": false
    }
  ]
}`;

const response = await fetch('https://firefly.example.com/api/v1/rules/123', {
  method: 'PUT',
  headers: {
    'X-Trace-Id': '40c71bbb-c676-4f24-83cf-cc725d7d7a00',
    'Authorization': 'Bearer YOUR_TOKEN',
    'Content-Type': 'application/json',
  },
  body,
});

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

### Python

```python
import requests

body = '''{
  "title": "First rule title.",
  "description": "First rule description",
  "rule_group_id": "81",
  "order": 5,
  "trigger": "store-journal",
  "active": true,
  "strict": true,
  "stop_processing": false,
  "triggers": [
    {
      "type": "user_action",
      "value": "tag1",
      "order": 5,
      "active": true,
      "stop_processing": false
    }
  ],
  "actions": [
    {
      "type": "set_category",
      "value": "Daily groceries",
      "order": 5,
      "active": true,
      "stop_processing": false
    }
  ]
}'''

response = requests.put(
    'https://firefly.example.com/api/v1/rules/123',
    data=body.encode('utf-8'),
    headers={
        'X-Trace-Id': '40c71bbb-c676-4f24-83cf-cc725d7d7a00',
        'Authorization': 'Bearer YOUR_TOKEN',
        'Content-Type': 'application/json',
    },
)
print(response.text)
```
