Skip to content

Instantly share code, notes, and snippets.

@adevinwild
Created June 7, 2025 12:31
Show Gist options
  • Save adevinwild/153a61fd04841b043081425489b866ef to your computer and use it in GitHub Desktop.
Save adevinwild/153a61fd04841b043081425489b866ef to your computer and use it in GitHub Desktop.
Learn how to create an order programmatically using Medusa.js V2
// src/api/admin/custom/orders/route.ts
import { createOrderWorkflow } from '@medusajs/core-flows'; // ℹ️ This is the worflow exposed and used inside Medusa
import { type MedusaRequest, type MedusaResponse } from '@medusajs/framework/http';
export async function POST(req: MedusaRequest, res: MedusaResponse) {
// ℹ️ You can use the body of the request to have an Admin API route to handle this
// on top of using modules to fetch dynamically the data (region, items etc...)
// For the demo, we're going to keep it simple with some hardcoded values
const { result } = await createOrderWorkflow(req.scope)
.run({
input: {
region_id: "reg_01JTMYCB1XBGA5D9Y59B1AGFFX",
items: [
{
variant_id: "variant_01JTMYCBC7H7WE474YGFN07XWX",
// ℹ️ I've used a specific variant_id of my database
// but you can have custom line items here with custom prices.
quantity: 1,
title: "SHIRT-S-BLACK",
unit_price: 10
}
],
sales_channel_id: "sc_01JTMYC95DA8F1TKYD3ZG3TD7P",
status: "pending",
shipping_address: {
first_name: "John",
last_name: "Doe",
address_1: "123 Main St",
city: "Los Angeles",
country_code: "us",
postal_code: "90001"
}
}
})
res.json({
order: result
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment