1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Fulfillment Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Fulfillment Module and how to use it in your application.
14
15 <Note title="Looking for no-code docs?">
16
17 Refer to the Medusa Admin User Guide to learn how to use the dashboard to:
18
19 - [Manage order fulfillments](!user-guide!/orders/fulfillments).
20 - [Manage shipping options and profiles](!user-guide!/settings/locations-and-shipping).
21
22 </Note>
23
24 Medusa has fulfillment related features available out-of-the-box through the Fulfillment Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Fulfillment Module.
25
26 <Note>
27
28 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
29
30 </Note>
31
32 ## Fulfillment Features
33
34 - [Fulfillment Management](./item-fulfillment/page.mdx): Create fulfillments and keep track of their status, items, and more.
35 - [Integrate Third-Party Fulfillment Providers](./fulfillment-provider/page.mdx): Create third-party fulfillment providers to provide customers with shipping options and fulfill their orders.
36 - [Restrict By Location and Rules](./shipping-option/page.mdx): Shipping options can be restricted to specific geographical locations. You can also specify custom rules to restrict shipping options.
37 - [Support Different Fulfillment Forms](./concepts/page.mdx): Support various fulfillment forms, such as shipping or pick up.
38 - [Tiered Pricing and Price Rules](../pricing/price-rules/page.mdx): Set prices for shipping options with tiers and rules, allowing you to create complex pricing strategies.
39
40 ---
41
42 ## How to Use the Fulfillment Module
43
44 In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
45
46 You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package.
47
48 For example:
49
50 export const highlights = [
51 ["12", "Modules.FULFILLMENT", "Resolve the module in a step."]
52 ]
53
54 ```ts title="src/workflows/create-fulfillment.ts" highlights={highlights}
55 import {
56 createWorkflow,
57 WorkflowResponse,
58 createStep,
59 StepResponse,
60 } from "@medusajs/framework/workflows-sdk"
61 import { Modules } from "@medusajs/framework/utils"
62
63 const createFulfillmentStep = createStep(
64 "create-fulfillment",
65 async ({}, { container }) => {
66 const fulfillmentModuleService = container.resolve(Modules.FULFILLMENT)
67
68 const fulfillment = await fulfillmentModuleService.createFulfillment({
69 location_id: "loc_123",
70 provider_id: "webshipper",
71 delivery_address: {
72 country_code: "us",
73 city: "Strongsville",
74 address_1: "18290 Royalton Rd",
75 },
76 items: [
77 {
78 title: "Shirt",
79 sku: "SHIRT",
80 quantity: 1,
81 barcode: "123456",
82 },
83 ],
84 labels: [],
85 order: {},
86 })
87
88 return new StepResponse({ fulfillment }, fulfillment.id)
89 },
90 async (fulfillmentId, { container }) => {
91 if (!fulfillmentId) {
92 return
93 }
94 const fulfillmentModuleService = container.resolve(Modules.FULFILLMENT)
95
96 await fulfillmentModuleService.deleteFulfillment(fulfillmentId)
97 }
98 )
99
100 export const createFulfillmentWorkflow = createWorkflow(
101 "create-fulfillment",
102 () => {
103 const { fulfillment } = createFulfillmentStep()
104
105 return new WorkflowResponse({
106 fulfillment,
107 })
108 }
109 )
110 ```
111
112 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
113
114 <CodeTabs group="resource-types">
115 <CodeTab label="API Route" value="api-route">
116
117 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
118 import type {
119 MedusaRequest,
120 MedusaResponse,
121 } from "@medusajs/framework/http"
122 import { createFulfillmentWorkflow } from "../../workflows/create-fuilfillment"
123
124 export async function GET(
125 req: MedusaRequest,
126 res: MedusaResponse
127 ) {
128 const { result } = await createFulfillmentWorkflow(req.scope)
129 .run()
130
131 res.send(result)
132 }
133 ```
134
135 </CodeTab>
136 <CodeTab label="Subscriber" value="subscriber">
137
138 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
139 import {
140 type SubscriberConfig,
141 type SubscriberArgs,
142 } from "@medusajs/framework"
143 import { createFulfillmentWorkflow } from "../workflows/create-fuilfillment"
144
145 export default async function handleUserCreated({
146 event: { data },
147 container,
148 }: SubscriberArgs<{ id: string }>) {
149 const { result } = await createFulfillmentWorkflow(container)
150 .run()
151
152 console.log(result)
153 }
154
155 export const config: SubscriberConfig = {
156 event: "user.created",
157 }
158 ```
159
160 </CodeTab>
161 <CodeTab label="Scheduled Job" value="scheduled-job">
162
163 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
164 import { MedusaContainer } from "@medusajs/framework/types"
165 import { createFulfillmentWorkflow } from "../workflows/create-fuilfillment"
166
167 export default async function myCustomJob(
168 container: MedusaContainer
169 ) {
170 const { result } = await createFulfillmentWorkflow(container)
171 .run()
172
173 console.log(result)
174 }
175
176 export const config = {
177 name: "run-once-a-day",
178 schedule: `0 0 * * *`,
179 }
180 ```
181
182 </CodeTab>
183 </CodeTabs>
184
185 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
186
187 ---
188
189 ## Configure Fulfillment Module
190
191 The Fulfillment Module accepts options for further configurations. Refer to [this documentation](./module-options/page.mdx) for details on the module's options.
192
193 ---
194
195 <CommerceModuleSections />