Skip to main content
Skip to main content

How to Manage Draft Orders

In this document, you’ll learn how to manage draft orders using the admin REST APIs.

Overview

Using the draft orders admin REST APIs, you can manage draft orders and perform other related functionalities.

Scenario

You want to add or use the following admin functionalities:

  • Manage draft orders, including listing, creating, updating, and deleting draft orders.
  • Managing line items in draft orders, including adding, updating, and deleting items from a draft order.
  • Authorize and capture a draft order’s payment.

Prerequisites

Medusa Components

It is assumed that you already have a Medusa backend installed and set up. If not, you can follow our quickstart guide to get started.

JS Client

This guide includes code snippets to send requests to your Medusa backend using Medusa’s JS Client, among other methods.

If you follow the JS Client code blocks, it’s assumed you already have Medusa’s JS Client installed and have created an instance of the client.

Medusa React

This guide also includes code snippets to send requests to your Medusa backend using Medusa React, among other methods.

If you follow the Medusa React code blocks, it's assumed you already have Medusa React installed and have used MedusaProvider higher in your component tree.

Authenticated Admin User

You must be an authenticated admin user before following along with the steps in the tutorial.

You can learn more about authenticating as an admin user in the API reference.


List Draft Orders

You can list draft orders by sending a request to the List Draft Orders API Route:

medusa.admin.draftOrders.list()
.then(({ draft_orders, limit, offset, count }) => {
console.log(draft_orders.length)
})

This API Route doesn't require any path or query parameters. You can pass search or pagination query parameters as explained in the API reference.

The request returns an array of draft order objects along with pagination parameters.


Create a Draft Order

You can create a draft order by sending a request to the Create Draft Order API Route:

medusa.admin.draftOrders.create({
email,
region_id,
items: [
{
// defined product
quantity: 1,
variant_id,
},
{
// custom product
quantity: 1,
unit_price: 1000,
title: "Custom Product",
},
],
shipping_methods: [
{
option_id,
// for custom shipping price
price,
},
],
})
.then(({ draft_order }) => {
console.log(draft_order.id)
})

This API Route requires the following request body parameters:

  • email: a string indicating the email of the customer to associate this draft order with.
  • region_id: a string indicating the ID of the region this draft order belongs to.
  • shipping_methods: an array of objects, each object being a shipping method to use for the draft order. The object is required to have the option_id property which is the ID of the shipping option to use. You can optionally specify a price property to set a custom amount for the price.

You can also pass other optional parameters. For example, the items array can be passed to add line items to the draft order. There are two types of items that you can add:

  1. Defined product variants. This is done by passing the variant_id property, with its value being the ID of the product variant to add.
  2. Custom products that aren’t defined in your store. This is done by passing the unit_price property with its value being the price of the custom product, and the title property with its value being the title of the custom product.

You must also pass for each item its quantity.

To learn about other optional request body parameters, such as passing shipping or billing addresses, check out the API reference.

The request returns the created draft order as an object.


Retrieve Draft Order

You can retrieve a draft order by sending a request to the Get Draft Order API Route:

medusa.admin.draftOrders.retrieve(draftOrderId)
.then(({ draft_order }) => {
console.log(draft_order.id)
})

This API Route requires the draft order’s ID as a path parameter.

The request returns the draft order as an object.


Update Draft Order

You can update a draft order by sending a request to the Update Draft Order API Route:

medusa.admin.draftOrders.update(draftOrderId, {
email: "user@example.com",
})
.then(({ draft_order }) => {
console.log(draft_order.id)
})

This API Route requires the draft order’s ID as a path parameter.

In the request body, you can pass any of the draft order’s fields to update. In the example above, you update the email field. Check out the API reference to learn about other accepted request body parameters.

The request returns the updated draft order as an object.


Manage Line Items

Add Line Items

You can add line items to a draft order by sending a request to the Create Line Items API Route:

medusa.admin.draftOrders.addLineItem(draftOrderId, {
quantity: 1,
})
.then(({ draft_order }) => {
console.log(draft_order.cart.items)
})

This API Route requires the ID of the draft order as a path parameter.

In the request body, it’s required to pass the quantity parameter, which is a number indicating the quantity of the item in the draft order. You can also pass other optional parameters based on the type of line item you’re adding.

There are two types of items that you can add:

  1. Defined product variants. This is done by passing the variant_id parameter, with its value being the ID of the product variant to add.
  2. Custom products that aren’t defined in your store. This is done by passing the unit_price parameter with its value being the price of the custom product, and the title property with its value being the title of the custom product.

Check the API reference for a full list of request body parameters.

The request returns the updated draft order as an object. You can access the draft order’s items by accessing the carts property in the draft item, then the items property. The items property is an array of line item objects.

Update Line Item

You can update a line item by sending a request to the Update Line Item API Route:

medusa.admin.draftOrders.updateLineItem(draftOrderId, itemId, {
quantity: 1,
})
.then(({ draft_order }) => {
console.log(draft_order.cart.items)
})

This API Route requires the draft order and line item’s IDs as path parameters.

In the request body, you can pass any of the line item’s fields as parameters to update them. In the example above, you pass the quantity parameter to update the quantity of the item. Check out the API reference for a full list of request body parameters.

The request returns the updated draft order as an object. You can access the draft order’s items by accessing the carts property in the draft item, then the items property. The items property is an array of line item objects.

Delete Line Item

You can delete a line item by sending a request to the Delete Line Item API Route:

medusa.admin.draftOrders.removeLineItem(draftOrderId, itemId)
.then(({ draft_order }) => {
console.log(draft_order.cart.items)
})

This API Route requires the draft order and line item’s IDs as path parameters.

The request returns the updated draft order as an object. You can access the draft order’s items by accessing the carts property in the draft item, then the items property. The items property is an array of line item objects.


Register Draft Order Payment

Registering the draft order’s payment leads to authorizing and capturing the payment using the system payment method. This would complete the draft order and create an order from it.

You can register the draft order payment by sending a request to the Register Draft Order Payment API Route:

medusa.admin.draftOrders.markPaid(draftOrderId)
.then(({ order }) => {
console.log(order.id)
})

This API Route requires the draft order’s ID as a path parameter.

The request returns the order created from the draft order as an object.


Delete a Draft Order

You can delete a draft order by sending a request to the Delete Draft Order API Route:

medusa.admin.draftOrders.delete(draftOrderId)
.then(({ id, object, deleted }) => {
console.log(id)
})

This API Route requires the draft order’s ID as a path parameter.

The request returns the following fields:

  • id: The ID of the deleted draft order.
  • object: The type of object that was deleted. In this case, the value will be draft-order.
  • deleted: A boolean value indicating whether the draft order was deleted.

See Also

Was this section helpful?