Skip to main content
Skip to main content

AdminBatchJobsResource

This class is used to send requests to Admin Batch Job API Routes. All its method are available in the JS Client under the medusa.admin.batchJobs property.

All methods in this class 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.

Methods

create

Create 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 confirm method.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.batchJobs.create({
type: 'product-export',
context: {},
dry_run: false
}).then((({ batch_job }) => {
console.log(batch_job.id);
})

Parameters

payloadAdminPostBatchesReqRequired
The data of the batch job to create.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminBatchJobRes>Required
Resolves to the batch job's details.

list

Retrieve 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 Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.batchJobs.list()
.then(({ batch_jobs, limit, offset, count }) => {
console.log(batch_jobs.length)
})

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

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.batchJobs.list({
expand: "created_by_user"
})
.then(({ batch_jobs, limit, offset, count }) => {
console.log(batch_jobs.length)
})

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

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.batchJobs.list({
expand: "created_by_user",
limit,
offset
})
.then(({ batch_jobs, limit, offset, count }) => {
console.log(batch_jobs.length)
})

Parameters

customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

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

Returns

ResponsePromiseResponsePromise<AdminBatchJobListRes>Required
The list of batch jobs with pagination fields.

cancel

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

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.batchJobs.cancel(batchJobId)
.then(({ batch_job }) => {
console.log(batch_job.id);
})

Parameters

batchJobIdstringRequired
The ID of the batch job.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminBatchJobRes>Required
Resolves to the batch job's details.

confirm

When a batch job is created, it's not executed automatically if dry_run is set to true. This method confirms that the batch job should be executed.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.batchJobs.confirm(batchJobId)
.then(({ batch_job }) => {
console.log(batch_job.id);
})

Parameters

batchJobIdstringRequired
The ID of the batch job.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminBatchJobRes>Required
Resolves to the batch job's details.

retrieve

Retrieve the details of a batch job.

Example

import Medusa from "@medusajs/medusa-js"
const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
// must be previously logged in or use api token
medusa.admin.batchJobs.retrieve(batchJobId)
.then(({ batch_job }) => {
console.log(batch_job.id);
})

Parameters

batchJobIdstringRequired
The ID of the batch job.
customHeadersRecord<string, any>Required
Custom headers to attach to the request.

Default: {}

Returns

ResponsePromiseResponsePromise<AdminBatchJobRes>Required
Resolves to the batch job's details.
Was this section helpful?