importProductsWorkflow - Medusa Core Workflows Reference

This documentation provides a reference to the importProductsWorkflow. It belongs to the @medusajs/medusa/core-flows package.

This workflow starts a product import from a CSV file in the background. It's used by the Import Products Admin API Route.

You can use this workflow within your custom workflows, allowing you to wrap custom logic around product import. For example, you can import products from another system.

The workflow only starts the import, but you'll have to confirm it using the Workflow Engine. The below example shows how to confirm the import.

Source Code

Examples#

To start the import of a CSV file:

Notice that the workflow returns a

. You'll use this ID to confirm the import afterwards.

You confirm the import using the Workflow Engine. For example, in an API route:

Code
1import {2  AuthenticatedMedusaRequest,3  MedusaResponse,4} from "@medusajs/framework/http"5import {6  importProductsWorkflowId,7  waitConfirmationProductImportStepId,8} from "@medusajs/core-flows"9import { IWorkflowEngineService } from "@medusajs/framework/types"10import { Modules, TransactionHandlerType } from "@medusajs/framework/utils"11import { StepResponse } from "@medusajs/framework/workflows-sdk"12
13export const POST = async (14  req: AuthenticatedMedusaRequest,15  res: MedusaResponse16) => {17  const workflowEngineService: IWorkflowEngineService = req.scope.resolve(18    Modules.WORKFLOW_ENGINE19  )20  const transactionId = req.params.transaction_id21
22  await workflowEngineService.setStepSuccess({23    idempotencyKey: {24      action: TransactionHandlerType.INVOKE,25      transactionId,26      stepId: waitConfirmationProductImportStepId,27      workflowId: importProductsWorkflowId,28    },29    stepResponse: new StepResponse(true),30  })31
32  res.status(202).json({})33}
TipThis example API route uses the same implementation as the Confirm Product Import Admin API Route.

Steps#

Input#

ImportProductsDTOImportProductsDTO
fileContentstring
The content of the CSV file.
filenamestring
The name of the CSV file.

Output#

ImportProductsSummaryImportProductsSummary
toCreatenumber
toUpdatenumber
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break