The Bulk Management api endpoints allow us to import new catalog 2.0 data in bulk from CSV files.
Currently, we only have two options here:
- Parse a CSV file prior to import, to quickly get the column names and number of rows. This is currently more intended as an internal endpoint that allows for faster processing of very large csv files that can't easily be parsed within the browser.
- Bulk Create Catalog - import the CSV file and convert it to items, attributes, options, etc, using the schema provided through the
mappings
parameter.
Here is an example script that demonstrates how the mappings
parameter needs to be correctly utilized in order to use the Bulk Create Catalog API. Please note that the code generated in the example for that api endpoint will not function correctly due to limitations in website functionality. Use this example instead for correct usage.
(async () => {
type IgnoreColumn = {
columnName: string;
type: 'ignore';
};
type ItemColumn = {
columnName: string;
type: 'item';
config?: {
suffix: string;
};
};
type AttributeColumn = {
columnName: string;
type: 'attribute';
config?: {
sufix: string;
};
};
type MetadataColumn = {
columnName: string;
type: 'metadata';
config: {
key?: string;
type: 'string' | 'number';
};
};
type PriceColumn = {
columnName: string;
type: 'price';
config: {
currency: 'string'; // The complete list of supported currencies is provided in the documentation in the mappings parameter
};
};
type VariantCustomIdColumn = {
columnName: string;
type: 'customId';
};
type VariantLabelColumn = {
columnName: string;
type: 'label';
};
type RequiredColumns = [ItemColumn, AttributeColumn];
type OptionalColumns = Array<
IgnoreColumn | MetadataColumn | PriceColumn | VariantCustomIdColumn | VariantLabelColumn
>;
type MappingsColumnMap = [...RequiredColumns, ...OptionalColumns];
const type = 'new-catalog-import';
const dryRun = 'true'; //set to false to actually import the table
const tableHeader = ['SKU', 'Color'];
const columnsMap: MappingsColumnMap = [
{ columnName: 'SKU', type: 'item' },
{ columnName: 'Color', type: 'attribute' },
];
const csvHeader = tableHeader.join(',') + '\n';
const csvContent = csvHeader + '\n' + 'Test Item, Red\n';
const blob = new Blob([csvContent], { type: 'text/csv' });
const formData = new FormData();
formData.append('file', blob, `test.csv`);
formData.append('mappings', JSON.stringify(columnsMap));
formData.append('type', type);
formData.append('dryRun', dryRun);
try {
const response = await fetch(`https://preview.threekit.com/api/v2/catalog/import`, {
method: 'POST',
headers: {
accept: 'application/json',
authorization: 'Bearer YOUR_PRIVATE_TOKEN',
},
body: formData,
});
const data = await response.json();
console.log(JSON.stringify(data));
} catch (error) {
console.log(error);
}
process.exit(0);
})();