Commerce Layer lets you import multiple resources in batches. To do that, you need to create a new , specify the resource_type you want to import, and populate the inputs attribute with a JSON (default) or CSV list of items. Each element of the inputs list (which is an array of objects for JSON and a string for CSV) contains the resource attributes and relationships (at least the ones required for the specific resource to be created).
To import a CSV list you need to set the format attribute to csv. Otherwise, if the format is not specified, the import API will assume you're using the default format (JSON) and return an error.
When importing a CSV list of items you also need to flatten the resource attributes and relationships on a single line. The headers for the attributes of a relationship must be prepended by the associated resource name (see example). When importing tags together with a tagged resource the elements related to the tags.id and tags.name headers can contain a string with the list of IDs/names, comma separated (see example). In general, using CSV is fine for simple imports (resources with a few attributes and no relationship included), otherwise we strongly recommend using JSON to ensure better readability in terms of inputs list.
The process is asynchronous and you can poll the status attribute to check the import progress. Once the import has been completed you can check the number of imported items by inspecting the processed_count attribute. In case of validation errors, the errors_count and errors_log attributes are populated.
Resource relationships can be addressed 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, price tiers, SKU list items, etc.).
Some automations normally triggered on resources' creation or update (e.g. webhooks, callbacks, and most event-driven communications) might not be enabled when using imports. Read here for more information.
Attachment URL
When an import is started, the data you pass within the inputs list is validated, compressed (gzip), and then uploaded to an external storage service (currently Amazon S3). You can download the imported data using the link exposed in the attachment_url attribute, which is populated as soon as the import is completed.
External storage service URLs expire in 5 minutes. You need to uncompress (gunzip) the file in order to read the data back. If the imported data URL is expired you can just fetch the completed import to get a new working one.
Import limits
Maximum import size
There is no limit on the total number of resources you can import, but the single batches are subject to some soft limits: the inputs array must contain a maximum of 10000 items, otherwise the import will be rejected at the time of creation.
Maximum error percentage
During the import process, the errors count is also monitored: imports whose errors_count attribute value overflows the maximum percentage of 10% of the total import size (i.e. the number of items contained in the inputs array) will be interrupted.
Concurrent imports
The maximum number of concurrent imports (i.e. imports whose status is pending or in_progess) allowed per organization is 10.
If you absolutely need to import any of the supported resources in one go, overriding the maximum import size and concurrent imports API limits above, you can leverage the power of our CLI import plugin (see example).
Supported resources
At the moment, imports are available for the following resources (more to come):
Addresses
Bundles
Coupons
Customer addresses
Customer payment sources
Customer subscriptions
Customers
Gift cards
Line items
Line item options
Orders
Price tiers
Prices
Shipping categories
SKU list items
SKU lists
SKU options
SKUs
Stock items
Stock transfers
Tags
Tax categories
Please find some examples of how to import them here below.
With very few exceptions (e.g. shipments, transactions, etc.) you can use as inputs any output (both in JSON or CSV format) you get from the exports API.
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 array element. 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 process:
Bundles — code + market_id
Coupons — code + promotion_rule_id
Customer addresses — customer_id
Customer subscriptions — customer_id + reference
Customers — email
Gift cards — code
Orders — number
Prices — sku_code + price_list_id
Shipping categories — name
SKU options — name + currency_code
SKUs — code
Stock items — sku_code + stock_location_id
Tags — name
Tax categories — code + tax_calculator_id
Specifying the parent resource
If the resources you're importing refer 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:
Bundles — market_id
Coupons — promotion_rule_id
Customer payment sources — payment_method_id
Gift cards — market_id
Orders — market_id
Prices — price_list_id
SKU list items — sku_list_id
SKU options — market_id
Stock items — stock_location_id
Tax categories — tax_calculator_id
Disabled automations
Webhooks
Excluding those associated with order status change events (orders.place, orders.approve, orders.cancel, orders.authorize, orders.void, orders.pay, orders.refund, orders.start_fulfilling, orders.cancel_fulfilling, and orders.fulfill), during imports all of the real-time webhooks are disabled.
In case you need to leverage all the other real-time events you have to create/update resources via the related API endpoints.
After-save callbacks
All of the resource's after-save callbacks are not executed on imports. This means that some attributes dependent on such callbacks will not be persisted (i.e. timestamps after status change or numbers set with the ID value).
In case you need to run after-save callbacks you have to make standard CRUD actions via the related API endpoints.
Cleaning-up records
Just to be sure you can rollback, we strongly recommend to perform an export using the very same filter before any cleanup operation.
The following request creates an import with a list of customer addresses in JSON format (the associated address can be either specified by ID if existing or created if not existing):
Importing a list of customer payment sources (JSON)
The following request creates an import with a list of customer payment sources in JSON format (you must always provide the customer_token and payment_source_token you've got from your payment gateway):
To create an import of gift cards for the same market, remember to specify its ID as theparent_resource_id attribute. On the other hand, specifying the market_id or the market_code attributes for every single item of the inputs list lets you import gift cards associated with different markets all at once.
Importing a list of orders with line items and line item options (JSON)
The following request creates an import with a list of orders with associated line items and line item options in JSON format:
To create an import of orders for the same market, remember to specify its ID as the parent_resource_id attribute. On the other hand, specifying the market_id or the market_code attributes for every single item of the inputs list lets you import orders associated with different markets all at once.
Importing a list of prices with price tiers (CSV)
The following request creates an import with a list of prices and the associated price tiers in CSV format:
To create an import of prices for the same price list, remember to specify its ID as the parent_resource_id attribute. On the other hand, specifying the price_list_id or the price_list_code attributes for every single item of the inputs list lets you import prices associated with different price lists all at once.
Importing a list of SKU lists with SKU list items (JSON)
The following request creates an import with a list of SKU lists with associated SKU list items in JSON format: