Powered By GitBook
Importing resources
How to bulk import resources and their relationships
Commerce Layer lets you import multiple resources in batches. To do that, you need to create a new import resource, specify the resource_type you want to import, and populate the inputs attribute with a JSON list of items. Each element of the inputs array contains the resource attributes and relationships.
The process is asynchronous and you can poll its status attribute to check the import progress. At the end of the import the processed_count attribute will indicate the number of imported items. In case of validation errors, the errors_count and errors_logattributes are populated.
Resource relationships can be specified by using their IDs. To do that, you need to append the _id suffix to the name of the related resource (e.g. price_list_id). Some relationships can also be specified as a nested array of objects (e.g. order line items).

Import limits

Maximum import size

Imports are subject to some soft limits: the inputs array must contain a maximum of 2000 items, otherwise the import will be rejected at creation time.

Maximum error percentage

During the importing phase, the errors count is also monitored: imports whom errors_count attribute overflows the maximum percentage of 10% of the total import size (i.e. the numbers of items contained in the inputs array) will be interrupted.

Supported resources

At moment, imports are available for the following resources (more to come):

Unique keys

If the single resource to be imported doesn't exist it gets created, otherwise it is updated.
In order to detect if a resource already exists the id key is checked for each inputs entry. Some resources also support specific (combined) attributes to identify an existing record, which can be helpful in case the resource ID isn't known during the import phase:
    Orders — number
    Coupons — code + promotion_rule_id
    SKUs — code
    Prices — sku_code + price_list_id
    Stock items — sku_code + stock_location_id
    Gift cards — code
    Customers — email
    Customer subscriptions — customer_id + reference
    Tax categories — code + tax_calculator_id

Specifying the parent resource

If the resources you're importing refers to the same parent resource (e.g. prices to the same price list), you can avoid repeating its value on each inputs items by specifying the parent_resource_id attribute. In case you specify both the parent resource ID and the relationship ID for each item, the latter wins. These are the supported parent resources:
    Orders — market_id
    Coupons — promotion_rule_id
    Prices — price_list_id
    Stock items — stock_location_id
    Gift cards — market_id
    Tax categories — tax_calculator_id

Cleaning-up records

Set the cleanup_records attribute as true if you want all the resources that are not present in the inputs list to be deleted at the end of the import. You can monitor the number of destroyed records once the import has completed by inspecting the destroyed_count attribute.
Be careful when cleaning up records — this action is not reversible. The cleanup_records flag is better used in combination with the parent_resource_id attribute in order to reduce the scope of the destroyed records. If the parent resource ID is missing all of the not-imported resources for the organization will be affected by the cleanup, which might not be what you want.

Examples

Importing a list of orders

Request
Response
The following request creates an import with a list of orders with associated line items and archives them at the same time:
1
curl -X POST \
2
https://yourdomain.commercelayer.io/api/imports \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "orders",
9
"attributes": {
10
"resource_type": "orders",
11
"parent_resource_id": "KmjljGJzWQ"
12
"inputs": [
13
{
14
"customer_email": "[email protected]",
15
"_archive": 1,
16
"line_items": [
17
{
18
"item_type": "sku",
19
"name": "BABYONBU000000E63E7424MX",
20
"quantity": 2,
21
"unit_amount_cents": 1000
22
},
23
{
24
"item_type": "sku",
25
"name": "BABYONBU000000E63E7418MX",
26
"quantity": 1,
27
"unit_amount_cents": 2000
28
}
29
]
30
},
31
{
32
"customer_email": "[email protected]",
33
"_archive": 1,
34
"line_items": [
35
{
36
"item_type": "sku",
37
"name": "BABYONBU000000E63E7667MX",
38
"quantity": 2,
39
"unit_amount_cents": 1500
40
}
41
]
42
},
43
{...}
44
]
45
}
46
}
47
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created import object with its status set to pending:
1
{
2
"data": {
3
"id": "PmjlkIJzRA",
4
"type": "imports",
5
"links": {...},
6
"attributes": {
7
"resource_type": "orders",
8
"parent_resource_id": "KmjljGJzWQ",
9
"status": "pending",
10
"started_at": null,
11
"completed_at": null,
12
"interrupted_at": null,
13
"inputs": [...],
14
"inputs_size": 100,
15
"errors_count": 0,
16
"warnings_count": 0,
17
"destroyed_count": 0,
18
"processed_count": 0,
19
"errors_log": {},
20
"warnings_log": {},
21
"cleanup_records": false,
22
"created_at": "2018-01-01T12:00:00.000Z",
23
"updated_at": "2018-01-01T12:00:00.000Z",
24
"reference": null,
25
"reference_origin": null,
26
"metadata": {}
27
},
28
"meta": {
29
"mode": "test"
30
}
31
}
32
Copied!
To create an import of orders for the same market, remember to specify its ID as the parent_resource_id attribute.
For a list of all the required attributes you need to create an order, refer to the related section of this API reference:

Importing a list of coupons

Request
Response
The following request creates an import with a list of coupons and clean-up the ones existing for the same promotion rule:
1
curl -X POST \
2
https://yourdomain.commercelayer.io/api/imports \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "imports",
9
"attributes": {
10
"resource_type": "coupons",
11
"parent_resource_id": "FreQKlNxZA"
12
"cleanup_records": "true",
13
"inputs": [
14
{
15
"code": "XXXXXXXX",
16
"usage_limit": 10
17
},
18
{
19
"code": "YYYYYYYY",
20
"usage_limit": 5
21
},
22
{...}
23
]
24
}
25
}
26
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created import object with its status set to pending:
1
{
2
"data": {
3
"id": "PmjlkIJzRA",
4
"type": "imports",
5
"links": {...},
6
"attributes": {
7
"resource_type": "coupons",
8
"parent_resource_id": "FreQKlNxZA",
9
"status": "pending",
10
"started_at": null,
11
"completed_at": null,
12
"interrupted_at": null,
13
"inputs": [...],
14
"inputs_size": 100,
15
"errors_count": 0,
16
"warnings_count": 0,
17
"destroyed_count": 0,
18
"processed_count": 0,
19
"cleanup_records": true,
20
"created_at": "2018-01-01T12:00:00.000Z",
21
"updated_at": "2018-01-01T12:00:00.000Z",
22
"reference": null,
23
"reference_origin": null,
24
"metadata": {}
25
},
26
"meta": {
27
"mode": "test"
28
}
29
}
30
}
Copied!
Please note that in this case — since the cleanup_records attribute is set as true — all of the coupons for the specified promotion rule that aren't included in the inputs list will be destroyed.
For a list of all the required attributes you need to create a coupon, refer to the related section of this API reference:

Importing a list of SKUs

Request
Response
The following request creates an import with a list of SKUs:
1
curl -X POST \
2
https://yourdomain.commercelayer.io/api/imports \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "imports",
9
"attributes": {
10
"resource_type": "skus",
11
"inputs": [
12
{
13
"code":"BABYONBU000000E63E7424MX",
14
"name":"Black Baby Onesie Short Sleeve with Pink Logo (24 Months)",
15
"image_url":"https://img.yourdomain.com/skus/BABYONBU000000E63E74.png",
16
"reference": "BABYONBU000000E63E74",
17
"shipping_category_id": "zwzQeFBrNj"
18
},
19
{
20
"code":"BABYONBU000000E63E7418MX",
21
"name":"Black Baby Onesie Short Sleeve with Pink Logo (18 Months)",
22
"image_url":"https://img.yourdomain.com/skus/BABYONBU000000E63E74.png",
23
"reference": "BABYONBU000000E63E74",
24
"shipping_category_id": "zwzQeFBrNj"
25
},
26
{...}
27
]
28
}
29
}
30
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created import object with its status set to pending:
1
{
2
"data": {
3
"id": "PmjlkIJzRA",
4
"type": "imports",
5
"links": {...},
6
"attributes": {
7
"resource_type": "skus",
8
"parent_resource_id": null,
9
"status": "pending",
10
"started_at": null,
11
"completed_at": null,
12
"interrupted_at": null,
13
"inputs": [...],
14
"inputs_size": 100,
15
"errors_count": 0,
16
"warnings_count": 0,
17
"destroyed_count": 0,
18
"processed_count": 0,
19
"cleanup_records": false,
20
"created_at": "2018-01-01T12:00:00.000Z",
21
"updated_at": "2018-01-01T12:00:00.000Z",
22
"reference": null,
23
"reference_origin": null,
24
"metadata": {}
25
},
26
"meta": {
27
"mode": "test"
28
}
29
}
30
}
Copied!
Specifying the shipping_category_id attribute for each single item of the inputs array lets you import SKUs associated with different shipping categories all at once.
For a list of all the required attributes you need to create an SKU, refer to the related section of this API reference:

Importing a list of prices

Request
Response
The following request creates an import with a list of prices:
1
curl -X POST \
2
https://yourdomain.commercelayer.io/api/imports \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "imports",
9
"attributes": {
10
"resource_type": "prices",
11
"parent_resource_id": "vLrWRCJWBE",
12
"inputs": [
13
{
14
"sku_code": "BABYONBU000000E63E7424MX",
15
"amount_cents": 3990,
16
"compare_at_amount_cents": 5000
17
},
18
{
19
"sku_code": "BABYONBU000000E63E7418MX",
20
"amount_cents": 3490,
21
"compare_at_amount_cents": 4700
22
},
23
{...}
24
]
25
}
26
}
27
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created import object with its status set to pending:
1
{
2
"data": {
3
"id": "ZbDQzIoxnA",
4
"type": "imports",
5
"links": {...},
6
"attributes": {
7
"resource_type": "prices",
8
"parent_resource_id": "vLrWRCJWBE",
9
"status": "pending",
10
"started_at": null,
11
"completed_at": null,
12
"interrupted_at": null,
13
"inputs": [...],
14
"inputs_size": 100,
15
"errors_count": 0,
16
"warnings_count": 0,
17
"destroyed_count": 0,
18
"processed_count": 0,
19
"cleanup_records": false,
20
"created_at": "2018-01-01T12:00:00.000Z",
21
"updated_at": "2018-01-01T12:00:00.000Z",
22
"reference": null,
23
"reference_origin": null,
24
"metadata": {}
25
},
26
"meta": {
27
"mode": "test"
28
}
29
}
30
}
Copied!
To create an import of prices for the same price list, remember to specify its ID as the parent_resource_id attribute.
For a list of all the required attributes you need to create a price, refer to the related section of this API reference:

Importing a list of stock items

Request
Response
The following request creates an import with a list of stock items:
1
curl -X POST \
2
https://yourdomain.commercelayer.io/api/imports \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "imports",
9
"attributes": {
10
"resource_type": "stock_items",
11
"parent_resource_id": "bnEeQuqZgn",
12
"inputs": [
13
{
14
"sku_code": "BABYONBU000000E63E7424MX",
15
"quantity": 110
16
},
17
{
18
"sku_code": "BABYONBU000000E63E7418MX",
19
"quantity": 140
20
},
21
{...}
22
]
23
}
24
}
25
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created import object with its status set to pending:
1
{
2
"data": {
3
"id": "YqQJGIYWjm",
4
"type": "imports",
5
"links": {...},
6
"attributes": {
7
"resource_type": "stock_items",
8
"parent_resource_id": "bnEeQuqZgn",
9
"status": "pending",
10
"started_at": null,
11
"completed_at": null,
12
"interrupted_at": null,
13
"inputs": [...],
14
"inputs_size": 100,
15
"errors_count": 0,
16
"warnings_count": 0,
17
"destroyed_count": 0,
18
"processed_count": 0,
19
"cleanup_records": false,
20
"created_at": "2018-01-01T12:00:00.000Z",
21
"updated_at": "2018-01-01T12:00:00.000Z",
22
"reference": null,
23
"reference_origin": null,
24
"metadata": {}
25
},
26
"meta": {
27
"mode": "test"
28
}
29
}
30
}
Copied!
To create an import of stock items for the same stock location, remember to specify its ID as the parent_resource_id attribute.
For a list of all the required attributes you need to create a stock item, refer to the related section of this API reference:

Importing a list of gift cards

Request
Response
The following request creates an import with a list of gift cards:
1
curl -X POST \
2
https://yourdomain.commercelayer.io/api/imports \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "imports",
9
"attributes": {
10
"resource_type": "gift_cards",
11
"parent_resource_id": "bgOEQhznoZ",
12
"inputs": [
13
{
14
"balance_cents": 12500,
15
"currency_code": "USD",
16
"single_use": false,
17
"rechargeable": true,
18
"image_url": "https://img.yourdomain.com/skus/MYGITFCARDIMAGE.png",
19
"expires_at": "2030-01-01T00:00:00Z",
20
"recipient_email": "[email protected]"
21
},
22
{
23
"balance_cents": 20000,
24
"currency_code": "EUR",
25
"single_use": true,
26
"rechargeable": false,
27
"expires_at": "2025-01-01T00:00:00Z",
28
"recipient_email": "[email protected]"
29
},
30
{...}
31
]
32
}
33
}
34
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created import object with its status set to pending:
1
{
2
"data": {
3
"id": "BAzkwIRNkq",
4
"type": "imports",
5
"links": {...},
6
"attributes": {
7
"resource_type": "gift_cards",
8
"parent_resource_id": "bgOEQhznoZ",
9
"status": "pending",
10
"started_at": null,
11
"completed_at": null,
12
"interrupted_at": null,
13
"inputs": [...],
14
"inputs_size": 100,
15
"errors_count": 0,
16
"warnings_count": 0,
17
"destroyed_count": 0,
18
"processed_count": 0,
19
"errors_log": {},
20
"warnings_log": {},
21
"cleanup_records": false,
22
"created_at": "2018-01-01T12:00:00.000Z",
23
"updated_at": "2018-01-01T12:00:00.000Z",
24
"reference": null,
25
"reference_origin": null,
26
"metadata": {}
27
},
28
"meta": {
29
"mode": "test"
30
}
31
}
32
}
Copied!
To create an import of gift cards for the same market, remember to specify its ID as theparent_resource_id attribute.
For a list of all the required attributes you need to create a gift card, refer to the related section of this API reference:

Importing a list of customers

Request
Response
The following request creates an import with a list of customers:
1
curl -X POST \
2
https://yourdomain.commercelayer.io/api/imports \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token' \
5
-H 'Content-Type: application/vnd.api+json' \
6
-d '{
7
"data": {
8
"type": "imports",
9
"attributes": {
10
"resource_type": "customers",
11
"inputs": [
12
{
13
"email": "[email protected]"
14
},
15
{
16
"email": "[email protected]",
17
"password": "secret",
18
"customer_group_id": "KngqdhAlpd"
19
},
20
{...}
21
]
22
}
23
}
24
}'
Copied!
On success, the API responds with a 201 Created status code, returning the created import object with its status set to pending:
1
{
2
"data": {
3
"id": "ZqDQzIoznm",
4
"type": "imports",