Skip to main content
Skip to main content

Batch Jobs

Queries and mutations listed here are used to send requests to the Admin Batch Job API Routes.

All hooks listed require user authentication.

A batch job is a task that is performed by the Medusa backend asynchronusly. For example, the Import Product feature is implemented using batch jobs. The methods in this class allow admins to manage the batch jobs and their state.

Related Guide: How to import products.

Mutations

useAdminCreateBatchJob

This hook creates a Batch Job to be executed asynchronously in the Medusa backend. If dry_run is set to true, the batch job will not be executed until the it is confirmed, which can be done using the useAdminConfirmBatchJob hook.

Example

import React from "react"
import { useAdminCreateBatchJob } from "medusa-react"

const CreateBatchJob = () => {
const createBatchJob = useAdminCreateBatchJob()
// ...

const handleCreateBatchJob = () => {
createBatchJob.mutate({
type: "publish-products",
context: {},
dry_run: true
}, {
onSuccess: ({ batch_job }) => {
console.log(batch_job)
}
})
}

// ...
}

export default CreateBatchJob

Mutation Function Parameters

AdminPostBatchesReqAdminPostBatchesReqRequired
The details of the batch job to create.

Mutation Function Returned Data

AdminBatchJobResAdminBatchJobResRequired
The batch job's details.

useAdminCancelBatchJob

This hook marks a batch job as canceled. When a batch job is canceled, the processing of the batch job doesn’t automatically stop.

Example

import React from "react"
import { useAdminCancelBatchJob } from "medusa-react"

type Props = {
batchJobId: string
}

const BatchJob = ({ batchJobId }: Props) => {
const cancelBatchJob = useAdminCancelBatchJob(batchJobId)
// ...

const handleCancel = () => {
cancelBatchJob.mutate(undefined, {
onSuccess: ({ batch_job }) => {
console.log(batch_job)
}
})
}

// ...
}

export default BatchJob

Hook Parameters

idstringRequired
The ID of the batch job.

Mutation Function Returned Data

AdminBatchJobResAdminBatchJobResRequired
The batch job's details.

Queries

useAdminBatchJobs

This hook retrieves a list of Batch Jobs. The batch jobs can be filtered by fields such as type or confirmed_at. The batch jobs can also be sorted or paginated.

Example

To list batch jobs:

import React from "react"
import { useAdminBatchJobs } from "medusa-react"

const BatchJobs = () => {
const {
batch_jobs,
limit,
offset,
count,
isLoading
} = useAdminBatchJobs()

return (
<div>
{isLoading && <span>Loading...</span>}
{batch_jobs?.length && (
<ul>
{batch_jobs.map((batchJob) => (
<li key={batchJob.id}>
{batchJob.id}
</li>
))}
</ul>
)}
</div>
)
}

export default BatchJobs

To specify relations that should be retrieved within the batch jobs:

import React from "react"
import { useAdminBatchJobs } from "medusa-react"

const BatchJobs = () => {
const {
batch_jobs,
limit,
offset,
count,
isLoading
} = useAdminBatchJobs({
expand: "created_by_user",
limit: 10,
offset: 0
})

return (
<div>
{isLoading && <span>Loading...</span>}
{batch_jobs?.length && (
<ul>
{batch_jobs.map((batchJob) => (
<li key={batchJob.id}>
{batchJob.id}
</li>
))}
</ul>
)}
</div>
)
}

export default BatchJobs

By default, only the first 10 records are retrieved. You can control pagination by specifying the limit and offset properties:

import React from "react"
import { useAdminBatchJobs } from "medusa-react"

const BatchJobs = () => {
const {
batch_jobs,
limit,
offset,
count,
isLoading
} = useAdminBatchJobs({
expand: "created_by_user",
limit: 20,
offset: 0
})

return (
<div>
{isLoading && <span>Loading...</span>}
{batch_jobs?.length && (
<ul>
{batch_jobs.map((batchJob) => (
<li key={batchJob.id}>
{batchJob.id}
</li>
))}
</ul>
)}
</div>
)
}

export default BatchJobs

Hook Parameters

Filters and pagination configurations to apply on the retrieved batch jobs.

Query Returned Data

limitnumberRequired
The maximum number of items that can be returned in the list.
offsetnumberRequired
The number of items skipped before the returned items in the list.
countnumberRequired
The total number of items available.
batch_jobsBatchJob[]Required
An array of batch job details.

useAdminBatchJob

This hook retrieves the details of a batch job.

Example

import React from "react"
import { useAdminBatchJob } from "medusa-react"

type Props = {
batchJobId: string
}

const BatchJob = ({ batchJobId }: Props) => {
const { batch_job, isLoading } = useAdminBatchJob(batchJobId)

return (
<div>
{isLoading && <span>Loading...</span>}
{batch_job && <span>{batch_job.created_by}</span>}
</div>
)
}

export default BatchJob

Hook Parameters

idstringRequired
The ID of the batch job.

Query Returned Data

batch_jobBatchJobRequired
Batch job details.
Was this section helpful?