E-commerce integrations

One of the many powerful features of MailerLite is the ability to connect any e-commerce shop with the platform, enabling users to take advantage of features such as e-commerce automations and e-commerce campaign tracking.

Integration with any e-commerce platform can be made with a few simple API calls, allowing easy syncing of the shop’s products, categories, orders and customers.

Getting started

Before we begin, please familiarize yourself with the general API conventions, limits, and terms of service. Our API is extremely intuitive, so it shouldn't take you long if you've ever used a RESTful HTTP API before.

You will also need to authenticate your requests to our API. You can find out how to do that here.

For the purposes of this article, we’ll use the example of a webshop selling t-shirts located at www.superamazingtshirts.com. We want to integrate our shop with our account on MailerLite.

Create a shop

First, we need to do a simple POST request to the MailerLite API to create a record for our shop:

POST connect.mailerlite.com/api/ecommerce/shops
1
{
  "name": "Super Amazing T-Shirts",
  "url": "superamazingtshirts.com",
  "currency": "USD",
  "enabled": true
}
1
2
3
4
5
6

The above will give us the following response:

{
   "data": {
       "id": "134",
       "name": "Super Amazing T-Shirts",
       "url": "superamazingtshirts.com",
       "currency": "USD",
       "enabled": true,
       "enable_popups": null,
       "group": null,
       "created_at": "2022-04-10T15:24:56.000000Z",
       "updated_at": "2022-04-10T15:24:56.000000Z",
       "currency_sign": "quot;
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

We need to note down the shop’s ID, as we will use it in all of our subsequent API calls.

Syncing categories and products

Syncing categories and products into MailerLite allows you to create e-commerce automations that are triggered when a specific product, or a product from a specific category, is purchased.

In our Super Amazing T-Shirts case, we would like to trigger an automation when a user purchases from our Superdeals category; more on that later.

We want to import all of our categories first, so we are going to use the respective e-commerce categories import API to add them all in one request

POST connect.mailerlite.com/api/ecommerce/shops/134/categories/import
1
[
   {
       "name": "Superdeals",
       "exclude_from_automations": false
   },
   {
       "name": "Single color",
       "exclude_from_automations": false
   },
   {
       "name": "Pattern",
       "exclude_from_automations": false
   },
   {
       "name": "Music",
       "exclude_from_automations": false
   }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

This will give us the following response:

{
   "data": [
       {
           "id": "52014320214607713",
           "resource_id": null,
           "name": "Superdeals",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:32:01.000000Z",
           "updated_at": "2022-04-10T15:32:01.000000Z"
       },
       {
           "id": "52014320218802018",
           "resource_id": null,
           "name": "Single color",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:32:01.000000Z",
           "updated_at": "2022-04-10T15:32:01.000000Z"
       },
       {
           "id": "52014320221947747",
           "resource_id": null,
           "name": "Pattern",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:32:01.000000Z",
           "updated_at": "2022-04-10T15:32:01.000000Z"
       },
       {
           "id": "52014320225093476",
           "resource_id": null,
           "name": "Music",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:32:01.000000Z",
           "updated_at": "2022-04-10T15:32:01.000000Z"
       }
   ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

Now we have to store and keep track of all the IDs of the imported categories, so we can reference them when creating the products.

For simple e-shops with only a few products, this method is fine but for more complex datasets, it will be a problem. Instead you can use resource IDs, which is another name for Bring Your Own IDs.

This allows you to set the ID of each resource (such as category, product, order, cart, cart item, customer) and reference a resource using the ID you specified instead of the ID that the platform assigned to it.

Let’s do the same categories import but this time using resource IDs:

POST connect.mailerlite.com/api/ecommerce/shops/134/categories/import?with_resource_id
1
[
   {
       "resource_id": "cat-superdeals",
       "name": "Superdeals",
       "exclude_from_automations": false
   },
   {
       "resource_id": "cat-single-color",
       "name": "Single color",
       "exclude_from_automations": false
   },
   {
       "resource_id": "cat-pattern",
       "name": "Pattern",
       "exclude_from_automations": false
   },
   {
       "resource_id": "cat-music",
       "name": "Music",
       "exclude_from_automations": false
   }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

As you can see, using resource IDs is dead simple. All you have to do is set the with_resource_id flag in your url and pass the resource ID for each category.

The resource ID can be anything you like—it is set and managed by you. In our WooCommerce and Shopify integrations, for example, the resource IDs are the IDs of the categories, products, etc. that each platform assigns respectively.

The above call will return a similar response to the one we got previously:

{
   "data": [
       {
           "id": "52014701898368045",
           "resource_id": "cat-superdeals",
           "name": "Superdeals",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:38:05.000000Z",
           "updated_at": "2022-04-10T15:38:05.000000Z"
       },
       {
           "id": "52014701903610926",
           "resource_id": "cat-single-color",
           "name": "Single color",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:38:05.000000Z",
           "updated_at": "2022-04-10T15:38:05.000000Z"
       },
       {
           "id": "52014701909902383",
           "resource_id": "cat-pattern",
           "name": "Pattern",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:38:05.000000Z",
           "updated_at": "2022-04-10T15:38:05.000000Z"
       },
       {
           "id": "52014701915145264",
           "resource_id": "cat-music",
           "name": "Music",
           "exclude_from_automations": false,
           "created_at": "2022-04-10T15:38:05.000000Z",
           "updated_at": "2022-04-10T15:38:05.000000Z"
       }
   ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

A final note about resource IDs is that you can access each resource using its resource ID if you pass the with_resource_id flag.

For example, to get the Superdeals category using the system assigned ID, we would do the following request

GET connect.mailerlite.com/api/ecommerce/shops/134/categories/52014701898368045
1

To get the same category using its resource ID, we would do the following request

GET connect.mailerlite.com/api/ecommerce/shops/134/categories/cat-superdeals?with_resource_id
1

In both cases we will get the exact same response:

{
   "data": {
       "id": "52014701898368045",
       "resource_id": "cat-superdeals",
       "name": "Superdeals",
       "exclude_from_automations": false,
       "created_at": "2022-04-10T15:38:05.000000Z",
       "updated_at": "2022-04-10T15:38:05.000000Z"
   }
}
1
2
3
4
5
6
7
8
9
10

Using resource IDs, there’s no need to keep track of two sets of IDs for your data. You can use the IDs of your e-shop directly in MailerLite. For the rest of the article, we will stick to using resource IDs exclusively.

Let’s import a couple products using resource IDs:

POST connect.mailerlite.com/api/ecommerce/shops/134/products/import?with_resource_id
1
[
   {
       "resource_id": "prod-tshirt1",
       "name": "T-Shirt 1",
       "categories": [
           "cat-superdeals",
           "cat-pattern"
       ],
       "price": 20,
       "exclude_from_automations": false
   },
   {
       "resource_id": "prod-tshirt2",
       "name": "T-Shirt 2",
       "categories": [
           "cat-single-color"
       ],
       "price": 20,
       "exclude_from_automations": false
   }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Notice that when using the with_resource_id flag, all IDs are resource IDs. In this case, we are using the resource IDs that we defined for the categories.

We will get the following response for the products import:

{
   "data": [
       {
           "id": "52015833343329937",
           "name": "T-Shirt 1",
           "resource_id": "prod-tshirt1",
           "price": 20,
           "url": null,
           "image": null,
           "exclude_from_automations": false,
           "categories": [
               {
                   "id": "52014701898368045",
                   "resource_id": "cat-superdeals",
                   "name": "Superdeals",
                   "exclude_from_automations": false,
                   "created_at": "2022-04-10T15:38:05.000000Z",
                   "updated_at": "2022-04-10T15:38:05.000000Z"
               },
               {
                   "id": "52014701909902383",
                   "resource_id": "cat-pattern",
                   "name": "Pattern",
                   "exclude_from_automations": false,
                   "created_at": "2022-04-10T15:38:05.000000Z",
                   "updated_at": "2022-04-10T15:38:05.000000Z"
               }
           ]
       },
       {
           "id": "52015833348572818",
           "name": "T-Shirt 2",
           "resource_id": "prod-tshirt2",
           "price": 20,
           "url": null,
           "image": null,
           "exclude_from_automations": false,
           "categories": [
               {
                   "id": "52014701903610926",
                   "resource_id": "cat-single-color",
                   "name": "Single color",
                   "exclude_from_automations": false,
                   "created_at": "2022-04-10T15:38:05.000000Z",
                   "updated_at": "2022-04-10T15:38:05.000000Z"
               }
           ]
       }
   ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

We can import any existing orders if we want, but let’s assume that our e-shop is new and has no orders yet.

Creating orders

When a customer places an order on your shop, we can register it with MailerLite using the following request

POST connect.mailerlite.com/api/ecommerce/shops/134/orders?with_resource_id
1
{
   "resource_id": "new-order1",
   "customer": {
       "resource_id": "customer1",
       "email": "customer@email.com",
       "create_subscriber": true,
       "accepts_marketing": true
   },
   "cart": {
       "resource_id": "cart1",
       "items": [
           {
               "resource_id": "cart-item1",
               "product_resource_id": "prod-tshirt1",
               "quantity": 1,
               "price": 20
           }
       ]
   },
   "status": "pending",
   "total_price": 20
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Notice that we are using resource IDs for the order, customer, cart and cart item. We will use these resource IDs to reference them in future requests.

And we’ll get the following response:

{
   "data": {
       "id": "52016443246511136",
       "resource_id": "new-order1",
       "customer": {
           "id": "52016278146123709",
           "resource_id": "customer1",
           "email": "customer@email.com",
           "accepts_marketing": true,
           "subscriber": {
               "id": "52016278181775299",
               "email": "customer@email.com",
               "status": "active",
               "source": "ecommerce",
               "sent": 0,
               "opens_count": 0,
               "clicks_count": 0,
               "open_rate": 0,
               "click_rate": 0,
               "ip_address": null,
               "subscribed_at": "2022-04-10 16:03:08",
               "unsubscribed_at": null,
               "created_at": "2022-04-10 16:03:08",
               "updated_at": "2022-04-10 16:03:08",
               "opted_in_at": null,
               "optin_ip": null
           }
       },
       "cart": {
           "id": "52016443227636766",
           "checkout_url": null,
           "cart_total": "0",
           "items": [
               {
                   "id": "52016443234976799",
                   "resource_id": "cart-item1",
                   "product": {
                       "id": "52015669709899317",
                       "name": "T-Shirt 1",
                       "resource_id": "prod-tshirt1",
                       "price": "20",
                       "url": null,
                       "image": null,
                       "exclude_from_automations": false,
                       "categories": [
                           {
                               "id": "52014701898368045",
                               "resource_id": "cat-superdeals",
                               "name": "Superdeals",
                               "exclude_from_automations": false,
                               "created_at": "2022-04-10T15:38:05.000000Z",
                               "updated_at": "2022-04-10T15:38:05.000000Z"
                           },
                           {
                               "id": "52014701909902383",
                               "resource_id": "cat-pattern",
                               "name": "Pattern",
                               "exclude_from_automations": false,
                               "created_at": "2022-04-10T15:38:05.000000Z",
                               "updated_at": "2022-04-10T15:38:05.000000Z"
                           }
                       ]
                   },
                   "variant": null,
                   "quantity": 1,
                   "price": "20"
               }
           ]
       },
       "total": 20,
       "status": "pending",
       "created_at": "2022-04-10T16:05:45.000000Z",
       "updated_at": "2022-04-10T16:05:45.000000Z"
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

When you submit an order to MailerLite with status pending, any abandoned cart automation that you have set up will be triggered. For this reason, you should submit the pending order as soon as you have enough information about the customer (e.g. the customer’s email and marketing preferences).

Let’s now say that the user adds another item to the cart. We cannot use the Order API to update the items of the order—we’ll have to use the cart items API:

POST connect.mailerlite.com/api/ecommerce/shops/134/carts/cart1/items?with_resource_id
1
{
   "resource_id": "cart-item2",
   "product_resource_id": "prod-tshirt2",
   "quantity": 1,
   "price": 20
}
1
2
3
4
5
6

The above request will add the item to our cart.

Finishing the order

To mark the order as complete and trigger the e-commerce automations, all we have to do is call the respective endpoint:

PUT connect.mailerlite.com/api/ecommerce/shops/134/orders/new-order1?with_resource_id
1
{
   "status": "complete"
}
1
2
3

And we will get the order object as a response:

{
   "data": {
       "id": "52016443246511136",
       "resource_id": "new-order1",
       "shop": {
           "id": "134",
           "name": "Super Amazing T-Shirts",
           "url": "superamazingtshirts.com",
           "currency": "USD",
           "enabled": true,
           "enable_popups": false,
           "group": null,
           "created_at": "2022-04-10T15:24:56.000000Z",
           "updated_at": "2022-04-10T15:24:56.000000Z",
           "currency_sign": "quot;
       },
       "customer": {
           "id": "52016278146123709",
           "resource_id": "customer1",
           "email": "customer@email.com",
           "accepts_marketing": true,
           "subscriber": {
               "id": "52016278181775299",
               "email": "customer@email.com",
               "status": "active",
               "source": "ecommerce",
               "sent": 0,
               "opens_count": 0,
               "clicks_count": 0,
               "open_rate": 0,
               "click_rate": 0,
               "ip_address": null,
               "subscribed_at": "2022-04-10 16:03:08",
               "unsubscribed_at": null,
               "created_at": "2022-04-10 16:03:08",
               "updated_at": "2022-04-10 16:03:08",
               "opted_in_at": null,
               "optin_ip": null
           }
       },
       "cart": {
           "id": "52016443227636766",
           "checkout_url": null,
           "cart_total": "0",
           "items": [
               {
                   "id": "52016443234976799",
                   "resource_id": "cart-item1",
                   "product": {
                       "id": "52015669709899317",
                       "name": "T-Shirt 1",
                       "resource_id": "prod-tshirt1",
                       "price": "20",
                       "url": null,
                       "image": null,
                       "exclude_from_automations": false,
                       "categories": [
                           {
                               "id": "52014701898368045",
                               "resource_id": "cat-superdeals",
                               "name": "Superdeals",
                               "exclude_from_automations": false,
                               "created_at": "2022-04-10T15:38:05.000000Z",
                               "updated_at": "2022-04-10T15:38:05.000000Z"
                           },
                           {
                               "id": "52014701909902383",
                               "resource_id": "cat-pattern",
                               "name": "Pattern",
                               "exclude_from_automations": false,
                               "created_at": "2022-04-10T15:38:05.000000Z",
                               "updated_at": "2022-04-10T15:38:05.000000Z"
                           }
                       ]
                   },
                   "variant": null,
                   "quantity": 1,
                   "price": "20"
               },
               {
                   "id": "52016782600308050",
                   "resource_id": "cart-item2",
                   "product": {
                       "id": "52015669714093622",
                       "name": "T-Shirt 2",
                       "resource_id": "prod-tshirt2",
                       "price": "20",
                       "url": null,
                       "image": null,
                       "exclude_from_automations": false,
                       "categories": [
                           {
                               "id": "52014701903610926",
                               "resource_id": "cat-single-color",
                               "name": "Single color",
                               "exclude_from_automations": false,
                               "created_at": "2022-04-10T15:38:05.000000Z",
                               "updated_at": "2022-04-10T15:38:05.000000Z"
                           }
                       ]
                   },
                   "variant": null,
                   "quantity": 1,
                   "price": "20"
               }
           ]
       },
       "total": "20",
       "status": "complete",
       "created_at": "2022-04-10T16:05:45.000000Z",
       "updated_at": "2022-04-10T16:14:32.000000Z"
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

Notice that the sum of the product prices doesn’t equal the order total. That’s because MailerLite doesn’t make any assumptions about the pricing of your store. You might charge tax, shipping and handling fees, or give discounts to your customers.

You will have to update the order total by making a PUT request on the same endpoint. You can even set the status to complete and update the total in the same call:

PUT connect.mailerlite.com/api/ecommerce/shops/134/orders/new-order1?with_resource_id
1
{
   "status": "complete",
   "total_price": 40
}
1
2
3
4

Setting the status of an order to complete will trigger any purchase e-commerce automations we might have and cancel any triggered abandoned cart ones.

Adding products into campaign emails

Any product that is added into MailerLite can easily be added to campaigns. All you have to do is add any of the product blocks from the e-commerce integration section of the builder into the email, select the block and click the Add products from your store button. You can then select any of your added products and they will be placed into the email, images and all.

E-commerce campaign tracking

If you send a campaign with e-commerce link tracking enabled, you can track which orders the campaign generated in your shop. For this to work, you need to include the MailerLite universal JavaScript code in all pages of your shop:

<!-- MailerLite Universal -->
<script>
    (function(w,d,e,u,f,l,n){w[f]=w[f]||function(){(w[f].q=w[f].q||[])
    .push(arguments);},l=d.createElement(e),l.async=1,l.src=u,
    n=d.getElementsByTagName(e)[0],n.parentNode.insertBefore(l,n);})
    (window,document,'script','https://assets.mailerlite.com/js/universal.js','ml');
    ml('account', '{my account id}');
</script>
<!-- End MailerLite Universal -->
1
2
3
4
5
6
7
8
9

If you have set up MailerLite embedded forms or pop-ups in your website, you should already have this JavaScript snippet added in your pages.

Now when you send a campaign with e-commerce link tracking enabled and direct customers to your shop, the universal script will track any purchases made by those customers, allowing you to view the effectiveness of your campaigns to generate revenue.

Last Updated: