The Import API uploads data to the commercetools platform. Its asynchronous design is suitable to sync a large amount of resources from external systems into commercetools projects.
The arrows in the diagram indicate the data flow. The data to be imported needs to be one of the supported Import Resource Types.
The Import API has the following advantages:
You can send bulk data at once, and your systems can do something else while your data is imported asynchronously.
You can separately import products, product variants, and prices.
Automatic dependency handling
The Import API automatically handles data dependencies such as the parental relations of categories.
Unlike other CLI tools and UI tools, it gives you better integration opportunity with your modern application infrastructure.
We introduce the following abstract data type for an expository purpose:
An ImportRequest stands for the abstract data type that encompasses CategoryImportRequest, ProductTypeImportRequest, ProductImportRequest, ProductDraftImportRequest, ProductVariantImportRequest, PriceImportRequest, OrderImportRequest, and CustomerImportRequest.
To use the Import API, you need to create an ImportSink. An ImportSink can be thought of as a temporarily storage for data to be imported. You can add import operations to an ImportSink by creating an ImportRequest. The operations are then processed and validated. To track the progress of import operations (known as ProcessingStates), you can call the get endpoint of ImportOperations or ImportSummaries.
The Import API uses a particular workflow.
- Create an ImportSink for the resource type you would like to import. Keep in mind that an ImportSink can only import a single resource type. If you want to import more than one type of data, you need to create ImportSinks separate for each data type. The ImportResourceType enum lists all supported resource data types.
- Next, Create an Import Request for the resource type you would like to import. For example, for importing categories you would need to Create an import request specific to categories.
- After sending an ImportRequest, the Import API returns a list of import operation statuses, which are given in terms of ProcessingStates. You can then poll the ImportSink to check the status of each ImportOperation.
- The ImportSink processes each operation. It validates the structure of the operation and any references in the operation, and updates the operation's state. For more information, see State Flow.
- If successful, the import operation's state is
Importedand changes are imported to commercetools.
- During the import process, it is possible to get an aggregated summary of all the ImportOperation states by fetching an ImportSummary.
- If the ImportSummary shows that some ImportOperations have ended up in state
Rejected, it is also possible to see the errors for each ImportOperation by querying them by the respective states. Here is an example of how to query ProductDraftImports by state, the same goes for other resources.
You only need to set up an ImportSink for a data type once. If you already have an ImportSink for a data type, you can send it ImportRequests at any time.