# Mautic api for campaign events

**URL:** <https://forum.mautic.org/t/mautic-api-for-campaign-events/24098>\
**Category:** Mautic 4 Install/Upgrade Support\
**Created:** [May 14, 2022, 11:26pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098 "2022-05-14T23:26:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![donbowman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/donbowman/32/5702_2.png) [@donbowman](https://forum.mautic.org/u/donbowman)\
**Post date:** [May 14, 2022, 11:26pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/1 "2022-05-14T23:26:18Z")

</div>

Using mautic 4.x on php 7.4

Observations and asks for assistance on the api for campaigns:

1. Events. The UI can use an endpoint of /s/campaigns/events/new, but there is no analog in the REST API
2. Read vs write. When you do a GET on a campaign, you end up with a format not suitable for posting back, notably:

- dates. The publishUp date comes back in iso8601 format (YYYY-mm-ddTHH:MM:SS+offset). But, the post format only accepts YYYY-mm-dd HH:MM:SS without offset and without T.
- category. on POST, i put category: #, on GET, it comes back as category: {object}

1. events. Events refer to each other by ID, but, you cannot create an event individually, and, cannot post them in the api w/o the ID.
2. the campaign interconnects seem to relate to the canvas of display, making it tough to use programatically.

Has anyone any addvice here? I’ve found a messy workaround, i post a campaign with a single event, this fails (500), but, actually creates the campaign and event. I then do a find by name, retreiving the campaign id, then fetch the campaign, then correct all the fields that are now formatted incorrectly. I then fidn the event-id assigned, add a new event without an id, post, repeat. Very slow and error prone.

any suggestions?

---

<div class="post-metadata">

**Author:** ![donbowman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/donbowman/32/5702_2.png) [@donbowman](https://forum.mautic.org/u/donbowman)\
**Post date:** [May 15, 2022, 8:28pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/2 "2022-05-15T20:28:43Z")

</div>

[Mautic Developer Documentation](https://developer.mautic.org/#get-campaign) is the docs, but it shows e.g. a struct for events, but the return value is an array.

i can get 1 event created (by passing in 1 in the array with no id, it creates one), but i cannot get 2 events created (this trick only works one time).

Any suggestions?

```auto
{
    "campaign": {
        "id": 3,
        "name": "Email A/B Test",
        "description": null,
        "isPublished": true,
        "publishUp": null,
        "publishDown": null,
        "dateAdded": "2015-07-15T15:06:02-05:00",
        "createdBy": 1,
        "createdByUser": "Joe Smith",
        "dateModified": "2015-07-20T13:11:56-05:00",
        "modifiedBy": 1,
        "modifiedByUser": "Joe Smith",
        "category": null,
        "events": {
            "28": {
                "id": 28,
                "type": "lead.changepoints",
                "eventType": "action",
                "name": "Adjust lead points",
                "description": null,
                "order": 1,
                "properties": {
                  "points": 20
                },
                "triggerDate": null,
                "triggerInterval": 1,
                "triggerIntervalUnit": "d",
                "triggerMode": "immediate",
                "children": [],
                "parent": null,
                "decisionPath": null
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![dirk\_s](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/dirk_s/32/748_2.png) [@dirk\_s](https://forum.mautic.org/u/dirk_s)\
**Post date:** [June 4, 2023, 3:05pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/3 "2023-06-04T15:05:31Z")

</div>

Hi @donbowman - I was able to create events, but ran into other trouble as well: [API for campaigns broken? - Support - Mautic Community Forums](https://forum.mautic.org/t/api-for-campaigns-broken/28226)

---

<div class="post-metadata">

**Author:** ![campiotto](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/campiotto/32/6739_2.png) [@campiotto](https://forum.mautic.org/u/campiotto)\
**Post date:** [March 21, 2024, 12:20pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/5 "2024-03-21T12:20:22Z")

</div>

Hello donbowman, you made any progress in creating events via API? (Creating events via API should be simpler.)

---

<div class="post-metadata">

**Author:** ![patryk.gruszka](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/patryk.gruszka/32/9813_2.png) [@patryk.gruszka](https://forum.mautic.org/u/patryk.gruszka)\
**Post date:** [April 9, 2024, 9:43am UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/6 "2024-04-09T09:43:36Z")

</div>

You can use the create campaign API endpoint to create the events.

The payload should contain:

- The starting segment or form definition (`lists` or `forms` in the JSON)
- Events temporary IDs starting with the keyword ‘new’, they will be replaced with real IDs when the campaign is created
- Correct references to parent events
- Nodes and connections settings in `canvasSettings`

Here is an example payload `POST /api/campaigns/new`:

```auto
{
    "isPublished": true,
    "name": "API Campaign",
    "description": null,
    "allowRestart": 0,
    "events": [
        {
            "id": "new1",
            "name": "Contact field value",
            "type": "lead.field_value",
            "eventType": "condition",
            "order": 1,
            "properties": {
                "canvasSettings": {
                    "droppedX": "520",
                    "droppedY": "155"
                },
                "name": "",
                "triggerMode": "immediate",
                "triggerDate": null,
                "triggerInterval": "1",
                "triggerIntervalUnit": "d",
                "triggerHour": "",
                "triggerRestrictedStartHour": "",
                "triggerRestrictedStopHour": "",
                "anchor": "leadsource",
                "properties": {
                    "field": "email",
                    "operator": "endsWith",
                    "value": "example.com"
                },
                "type": "lead.field_value",
                "eventType": "condition",
                "anchorEventType": "source",
                "buttons": {
                    "save": ""
                },
                "field": "email",
                "operator": "endsWith",
                "value": "example.com"
            },
            "triggerDate": null,
            "triggerInterval": 1,
            "triggerIntervalUnit": "d",
            "triggerHour": null,
            "triggerRestrictedStartHour": null,
            "triggerRestrictedStopHour": null,
            "triggerRestrictedDaysOfWeek": [],
            "triggerMode": "immediate",
            "decisionPath": null,
            "parent": null
        },
        {
            "id": "new2",
            "name": "Modify contact's tags",
            "type": "lead.changetags",
            "eventType": "action",
            "order": 2,
            "properties": {
                "canvasSettings": {
                    "droppedX": "710",
                    "droppedY": "291"
                },
                "name": "",
                "triggerMode": "immediate",
                "triggerDate": null,
                "triggerInterval": "1",
                "triggerIntervalUnit": "d",
                "triggerHour": "",
                "triggerRestrictedStartHour": "",
                "triggerRestrictedStopHour": "",
                "anchor": "yes",
                "properties": {
                    "add_tags": [
                        "2"
                    ]
                },
                "type": "lead.changetags",
                "eventType": "action",
                "anchorEventType": "condition",
                "buttons": {
                    "save": ""
                },
                "triggerRestrictedDaysOfWeek": [],
                "add_tags": [
                    "example"
                ],
                "remove_tags": []
            },
            "triggerDate": null,
            "triggerInterval": 1,
            "triggerIntervalUnit": "d",
            "triggerHour": null,
            "triggerRestrictedStartHour": null,
            "triggerRestrictedStopHour": null,
            "triggerRestrictedDaysOfWeek": [],
            "triggerMode": "immediate",
            "decisionPath": "yes",
            "parent": {"id": "new1"}
        }
    ],
    "forms": [],
    "lists": [
        {
            "createdByUser": "Admin User",
            "modifiedByUser": null,
            "id": 9,
            "name": "Segment Test 1",
            "publicName": "Segment Test 1",
            "alias": "segment-test-1",
            "description": null,
            "category": null
        }
    ],
    "canvasSettings": {
        "nodes": [
            {
                "id": "new1",
                "positionX": "810",
                "positionY": "186"
            },
            {
                "id": "new2",
                "positionX": "710",
                "positionY": "291"
            },
            {
                "id": "lists",
                "positionX": "833",
                "positionY": "50"
            }
        ],
        "connections": [
            {
                "sourceId": "lists",
                "targetId": "new1",
                "anchors": {
                    "source": "leadsource",
                    "target": "top"
                }
            },
            {
                "sourceId": "new1",
                "targetId": "new2",
                "anchors": {
                    "source": "yes",
                    "target": "top"
                }
            }
        ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![campiotto](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/campiotto/32/6739_2.png) [@campiotto](https://forum.mautic.org/u/campiotto)\
**Post date:** [August 10, 2024, 1:28pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/7 "2024-08-10T13:28:03Z")

</div>

@patryk.gruszka I tested your json and it works perfectly, however when I tried to apply the concept to my scenario doing something very simple like sending a single email to a follow-up I got an error in the mautic log saying that the json is poorly formatted. If I put yours, it works perfectly. Do you know where I might be going wrong?

```auto
{
    "isPublished": true,
    "name": "Teste sunday mail ",
    "description": "My sunday mail",
    "allowRestart": 0,
    "events": [
        {
            "id": "new1",
            "name": "Enviar email de domingo",
            "type": "email.send",
            "eventType": "action",
            "channel": "email",
            "channelId": 691,
            "order": 0,
            "properties": {
                "canvasSettings": {
                    "droppedX": "621",
                    "droppedY": "155"
                },
                "triggerMode": "immediate",
                "triggerInterval": "1",
                "triggerIntervalUnit": "d",
                "anchor": "leadsource",
                "properties": {
                    "email": "691",
                    "email_type": "transactional",
                    "priority": "2",
                    "attempts": "3"
                }
            },
            "triggerMode": "immediate",
            "parent": null
        }
    ],
    "lists": [
        {  
        	"createdByUser": "Admin User",
            "modifiedByUser": null,
            "id": 123,
            "name": "teste",
            "alias": "teste1"
        }
    ],
    "canvasSettings": {
        "nodes": [
            {
                "id": "new1",
                "positionX": "621",
                "positionY": "155"
            },
            {
                "id": "lists",
                "positionX": "721",
                "positionY": "50"
            }
        ],
        "connections": [
            {
                "sourceId": "lists",
                "targetId": "new1",
                "anchors": {
                    "source": "leadsource",
                    "target": "top"
                }
            }
        ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![donbowman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/donbowman/32/5702_2.png) [@donbowman](https://forum.mautic.org/u/donbowman)\
**Post date:** [October 20, 2024, 2:56pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/8 "2024-10-20T14:56:59Z")

</div>

Coming back to this…  
I also find that the triggerDate. its marked as ‘datetime’ in the API spec, but you cannot JSON serialise this. So if I post a string ‘YYYY-mm-dd HH:MM:SS’, I get a stack trace

If i set triggerDate to null, it works, but I need a triggerDate.

```auto
Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "JMS\Serializer\Handler\DateHandler::serializeDateTime(): Argument #2 ($date) must be of type DateTime, string given, called in /app/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php on line 209" at /app/vendor/jms/serializer/src/Handler/DateHandler.php line 105 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): JMS\\Serializer\\Handler\\DateHandler::serializeDateTime(): Argument #2 ($date) must be of type DateTime, string given, called in /app/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php on line 209 at /app/vendor/jms/serializer/src/Handler/DateHandler.php:105)

```

---

<div class="post-metadata">

**Author:** ![donbowman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/donbowman/32/5702_2.png) [@donbowman](https://forum.mautic.org/u/donbowman)\
**Post date:** [October 27, 2024, 8:21pm UTC](https://forum.mautic.org/t/mautic-api-for-campaign-events/24098/9 "2024-10-27T20:21:23Z")

</div>

If I do a post of below to /campaigns/new, it gives 500 error. If I replace the triggerDate with null, it passes.

```auto
{
  "isPublished": true,
  "name": "test-campaign-1",
  "events": [
    {
      "id": "new0",
      "name": "0: SQL QUAD-1",
      "type": "sql.condition",
      "eventType": "condition",
      "properties": {
        "sql": "300001"
      },
      "triggerDate": "2014-12-22 08:31:53",
      "triggerMode": "date",
      "parent": null,
      "children": []
    }
  ],
  "lists": [1440098]
}

```

If i create it with ‘null’, and then use PATCH

```auto
{
  "id": 330085,
  "events": [
    {
      "id": 361267,
      "triggerDate": "2024-11-11 01:02:03",
      "triggerMode": "date"
    }
  ]
}

```

i get no error, but it doesn’t stick.
