1 import { Prerequisites, CardList, Table } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Create a Plugin`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn how to create a Medusa plugin and publish it.
10
11 A [plugin](../page.mdx) is a package of reusable Medusa customizations that you can install in any Medusa application. By creating and publishing a plugin, you can reuse your Medusa customizations across multiple projects or share them with the community.
12
13 <Note>
14
15 Plugins are available starting from [Medusa v2.3.0](https://github.com/medusajs/medusa/releases/tag/v2.3.0).
16
17 </Note>
18
19 ## 1. Create a Plugin Project
20
21 Plugins are created in a separate Medusa project. This makes the development and publishing of the plugin easier. Later, you'll install that plugin in your Medusa application to test it out and use it.
22
23 Medusa's `create-medusa-app` CLI tool provides the option to create a plugin project. Run the following command to create a new plugin project:
24
25 ```bash npx2yarnExec
26 npx create-medusa-app my-plugin --plugin
27 ```
28
29 This will create a new Medusa plugin project in the `my-plugin` directory.
30
31 ### Plugin Directory Structure
32
33 After the installation is done, the plugin structure will look like this:
34
35 ![Directory structure of a plugin project](https://res.cloudinary.com/dza7lstvk/image/upload/v1737019441/Medusa%20Book/project-dir_q4xtri.jpg)
36
37 - `src/`: Contains the Medusa customizations.
38 - `src/admin`: Contains [admin extensions](../../admin/page.mdx).
39 - `src/api`: Contains [API routes](../../api-routes/page.mdx) and [middlewares](../../api-routes/middlewares/page.mdx). You can add store, admin, or any custom API routes.
40 - `src/jobs`: Contains [scheduled jobs](../../scheduled-jobs/page.mdx).
41 - `src/links`: Contains [module links](../../module-links/page.mdx).
42 - `src/modules`: Contains [modules](../../modules/page.mdx).
43 - `src/provider`: Contains [module providers](#create-module-providers).
44 - `src/subscribers`: Contains [subscribers](../../events-and-subscribers/page.mdx).
45 - `src/workflows`: Contains [workflows](../../workflows/page.mdx). You can also add [hooks](../../workflows/add-workflow-hook/page.mdx) under `src/workflows/hooks`.
46 - `package.json`: Contains the plugin's package information, including general information and dependencies.
47 - `tsconfig.json`: Contains the TypeScript configuration for the plugin.
48
49 ---
50
51 ## 2. Prepare Plugin
52
53 ### Package Name
54
55 Before developing, testing, and publishing your plugin, make sure its name in `package.json` is correct. This is the name you'll use to install the plugin in your Medusa application.
56
57 For example:
58
59 ```json title="package.json"
60 {
61 "name": "@myorg/plugin-name",
62 // ...
63 }
64 ```
65
66 ### Package Keywords
67
68 Medusa scrapes NPM for a list of plugins that integrate third-party services, to later showcase them on the Medusa website. If you want your plugin to appear in that listing, make sure to add the `medusa-v2` and `medusa-plugin-integration` keywords to the `keywords` field in `package.json`.
69
70 <Note title="Important">
71
72 Only plugins that integrate third-party services are listed in the Medusa integrations page. If your plugin doesn't integrate a third-party service, you can skip this step.
73
74 </Note>
75
76 ```json title="package.json"
77 {
78 "keywords": [
79 "medusa-plugin-integration",
80 "medusa-v2"
81 ],
82 // ...
83 }
84 ```
85
86 In addition, make sure to use one of the following keywords based on your integration type:
87
88 <Table>
89 <Table.Header>
90 <Table.Row>
91 <Table.HeaderCell>
92 Keyword
93 </Table.HeaderCell>
94 <Table.HeaderCell>
95 Description
96 </Table.HeaderCell>
97 <Table.HeaderCell>
98 Example
99 </Table.HeaderCell>
100 </Table.Row>
101 </Table.Header>
102 <Table.Body>
103 <Table.Row>
104 <Table.Cell>
105 `medusa-plugin-analytics`
106 </Table.Cell>
107 <Table.Cell>
108 Analytics service integration
109 </Table.Cell>
110 <Table.Cell>
111 Google Analytics
112 </Table.Cell>
113 </Table.Row>
114 <Table.Row>
115 <Table.Cell>
116 `medusa-plugin-auth`
117 </Table.Cell>
118 <Table.Cell>
119 Authentication service integration
120 </Table.Cell>
121 <Table.Cell>
122 Auth0
123 </Table.Cell>
124 </Table.Row>
125 <Table.Row>
126 <Table.Cell>
127 `medusa-plugin-cms`
128 </Table.Cell>
129 <Table.Cell>
130 CMS service integration
131 </Table.Cell>
132 <Table.Cell>
133 Contentful
134 </Table.Cell>
135 </Table.Row>
136 <Table.Row>
137 <Table.Cell>
138 `medusa-plugin-notification`
139 </Table.Cell>
140 <Table.Cell>
141 Notification service integration
142 </Table.Cell>
143 <Table.Cell>
144 Twilio SMS
145 </Table.Cell>
146 </Table.Row>
147 <Table.Row>
148 <Table.Cell>
149 `medusa-plugin-payment`
150 </Table.Cell>
151 <Table.Cell>
152 Payment service integration
153 </Table.Cell>
154 <Table.Cell>
155 PayPal
156 </Table.Cell>
157 </Table.Row>
158 <Table.Row>
159 <Table.Cell>
160 `medusa-plugin-search`
161 </Table.Cell>
162 <Table.Cell>
163 Search service integration
164 </Table.Cell>
165 <Table.Cell>
166 MeiliSearch
167 </Table.Cell>
168 </Table.Row>
169 <Table.Row>
170 <Table.Cell>
171 `medusa-plugin-shipping`
172 </Table.Cell>
173 <Table.Cell>
174 Shipping service integration
175 </Table.Cell>
176 <Table.Cell>
177 DHL
178 </Table.Cell>
179 </Table.Row>
180 <Table.Row>
181 <Table.Cell>
182 `medusa-plugin-other`
183 </Table.Cell>
184 <Table.Cell>
185 Other third-party integrations
186 </Table.Cell>
187 <Table.Cell>
188 Sentry
189 </Table.Cell>
190 </Table.Row>
191 </Table.Body>
192 </Table>
193
194 ### Package Dependencies
195
196 <Note>
197
198 Your plugin project will already have the dependencies mentioned in this section. Unless you made changes to the dependencies, you can skip this section.
199
200 </Note>
201
202 In the `package.json` file you must have the Medusa dependencies as `devDependencies` and `peerDependencies`. In addition, you must have `@swc/core` as a `devDependency`, as it's used by the plugin CLI tools.
203
204 For example, assuming `2.5.0` is the latest Medusa version:
205
206 ```json title="package.json"
207 {
208 "devDependencies": {
209 "@medusajs/admin-sdk": "2.5.0",
210 "@medusajs/cli": "2.5.0",
211 "@medusajs/framework": "2.5.0",
212 "@medusajs/medusa": "2.5.0",
213 "@medusajs/test-utils": "2.5.0",
214 "@medusajs/ui": "4.0.4",
215 "@medusajs/icons": "2.5.0",
216 "@swc/core": "1.5.7",
217 },
218 "peerDependencies": {
219 "@medusajs/admin-sdk": "2.5.0",
220 "@medusajs/cli": "2.5.0",
221 "@medusajs/framework": "2.5.0",
222 "@medusajs/test-utils": "2.5.0",
223 "@medusajs/medusa": "2.5.0",
224 "@medusajs/ui": "4.0.3",
225 "@medusajs/icons": "2.5.0",
226 }
227 }
228 ```
229
230 ### Package Exports
231
232 <Note>
233
234 Your plugin project will already have the exports mentioned in this section. Unless you made changes to the exports or you created your plugin before [Medusa v2.7.0](https://github.com/medusajs/medusa/releases/tag/v2.7.0), you can skip this section.
235
236 </Note>
237
238 In the `package.json` file, make sure your plugin has the following exports:
239
240 ```json title="package.json"
241 {
242 "exports": {
243 "./package.json": "./package.json",
244 "./workflows": "./.medusa/server/src/workflows/index.js",
245 "./.medusa/server/src/modules/*": "./.medusa/server/src/modules/*/index.js",
246 "./providers/*": "./.medusa/server/src/providers/*/index.js",
247 "./admin": {
248 "import": "./.medusa/server/src/admin/index.mjs",
249 "require": "./.medusa/server/src/admin/index.js",
250 "default": "./.medusa/server/src/admin/index.js"
251 },
252 "./*": "./.medusa/server/src/*.js"
253 }
254 }
255 ```
256
257 <Note title="Tip">
258
259 Aside from the `./package.json`, `./providers`, and `./admin`, these exports are only a recommendation. You can cherry-pick the files and directories you want to export.
260
261 </Note>
262
263 The plugin exports the following files and directories:
264
265 - `./package.json`: The `package.json` file. Medusa needs to access the `package.json` when registering the plugin.
266 - `./workflows`: The workflows exported in `./src/workflows/index.ts`.
267 - `./.medusa/server/src/modules/*`: The definition file of modules. This is useful if you create links to the plugin's modules in the Medusa application.
268 - `./providers/*`: The definition file of module providers. This is useful if your plugin includes a module provider, allowing you to register the plugin's providers in Medusa applications. Learn more in the [Create Module Providers](#create-module-providers) section.
269 - `./admin`: The admin extensions exported in `./src/admin/index.ts`.
270 - `./*`: Any other files in the plugin's `src` directory.
271
272 ---
273
274 ## 3. Publish Plugin Locally for Development and Testing
275
276 Medusa's CLI tool provides commands to simplify developing and testing your plugin in a local Medusa application. You start by publishing your plugin in the local package registry, then install it in your Medusa application. You can then watch for changes in the plugin as you develop it.
277
278 ### Publish and Install Local Package
279
280 <Prerequisites
281 items={[
282 {
283 text: "Medusa application installed.",
284 link: "/learn/installation",
285 }
286 ]}
287 />
288
289 The first time you create your plugin, you need to publish the package into a local package registry, then install it in your Medusa application. This is a one-time only process.
290
291 To publish the plugin to the local registry, run the following command in your plugin project:
292
293 ```bash npx2yarn title="Plugin project"
294 npx medusa plugin:publish
295 ```
296
297 This command uses [Yalc](https://github.com/wclr/yalc) under the hood to publish the plugin to a local package registry. The plugin is published locally under the name you specified in `package.json`.
298
299 Next, navigate to your Medusa application:
300
301 ```bash title="Medusa application"
302 cd ~/path/to/medusa-app
303 ```
304
305 Make sure to replace `~/path/to/medusa-app` with the path to your Medusa application.
306
307 Then, if your project was created before v2.3.1 of Medusa, make sure to install `yalc` as a development dependency:
308
309 ```bash npm2yarn badgeLabel="Medusa Application" badgeColor="green"
310 npm install --save-dev yalc
311 ```
312
313 After that, run the following Medusa CLI command to install the plugin:
314
315 ```bash npx2yarn title="Medusa application"
316 npx medusa plugin:add @myorg/plugin-name
317 ```
318
319 Make sure to replace `@myorg/plugin-name` with the name of your plugin as specified in `package.json`. Your plugin will be installed from the local package registry into your Medusa application.
320
321 ### Register Plugin in Medusa Application
322
323 After installing the plugin, you need to register it in your Medusa application in the configurations defined in `medusa-config.ts`.
324
325 Add the plugin to the `plugins` array in the `medusa-config.ts` file:
326
327 export const pluginHighlights = [
328 ["5", `"@myorg/plugin-name"`, "Replace with your plugin name."],
329 ]
330
331 ```ts title="medusa-config.ts" highlights={pluginHighlights}
332 module.exports = defineConfig({
333 // ...
334 plugins: [
335 {
336 resolve: "@myorg/plugin-name",
337 options: {},
338 },
339 ],
340 })
341 ```
342
343 The `plugins` configuration is an array of objects where each object has a `resolve` key whose value is the name of the plugin package.
344
345 #### Pass Module Options through Plugin
346
347 Each plugin configuration also accepts an `options` property, whose value is an object of options to pass to the plugin's modules.
348
349 For example:
350
351 export const pluginOptionsHighlight = [
352 ["6", "options", "Options to pass to the plugin's modules."]
353 ]
354
355 ```ts title="medusa-config.ts" highlights={pluginOptionsHighlight}
356 module.exports = defineConfig({
357 // ...
358 plugins: [
359 {
360 resolve: "@myorg/plugin-name",
361 options: {
362 apiKey: true,
363 },
364 },
365 ],
366 })
367 ```
368
369 The `options` property in the plugin configuration is passed to all modules in the plugin. Learn more about module options in [this chapter](../../modules/options/page.mdx).
370
371 ### Watch Plugin Changes During Development
372
373 While developing your plugin, you can watch for changes in the plugin and automatically update the plugin in the Medusa application using it. This is the only command you'll continuously need during your plugin development.
374
375 To do that, run the following command in your plugin project:
376
377 ```bash npx2yarn title="Plugin project"
378 npx medusa plugin:develop
379 ```
380
381 This command will:
382
383 - Watch for changes in the plugin. Whenever a file is changed, the plugin is automatically built.
384 - Publish the plugin changes to the local package registry. This will automatically update the plugin in the Medusa application using it. You can also benefit from real-time HMR updates of admin extensions.
385
386 ### Start Medusa Application
387
388 You can start your Medusa application's development server to test out your plugin:
389
390 ```bash npm2yarn badgeLabel="Medusa Application" badgeColor="green"
391 npm run dev
392 ```
393
394 While your Medusa application is running and the plugin is being watched, you can test your plugin while developing it in the Medusa application.
395
396 ---
397
398 ## 4. Create Customizations in the Plugin
399
400 You can now build your plugin's customizations. The following guide explains how to build different customizations in your plugin.
401
402 <CardList
403 items={[
404 {
405 title: "Create a module",
406 href: "/learn/fundamentals/modules",
407 },
408 {
409 title: "Create a module link",
410 href: "/learn/fundamentals/module-links",
411 },
412 {
413 title: "Create a workflow",
414 href: "/learn/fundamentals/workflows",
415 },
416 {
417 title: "Add a workflow hook",
418 href: "/learn/fundamentals/workflows/add-workflow-hook",
419 },
420 {
421 title: "Create an API route",
422 href: "/learn/fundamentals/api-routes",
423 },
424 {
425 title: "Add a subscriber",
426 href: "/learn/fundamentals/events-and-subscribers",
427 },
428 {
429 title: "Add a scheduled job",
430 href: "/learn/fundamentals/scheduled-jobs",
431 },
432 {
433 title: "Add an admin widget",
434 href: "/learn/fundamentals/admin/widgets",
435 },
436 {
437 title: "Add an admin UI route",
438 href: "/learn/fundamentals/admin/ui-routes",
439 }
440 ]}
441 className="mb-1.5"
442 />
443
444 While building those customizations, you can test them in your Medusa application by [watching the plugin changes](#watch-plugin-changes-during-development) and [starting the Medusa application](#start-medusa-application).
445
446 ### Generating Migrations for Modules
447
448 During your development, you may need to generate migrations for modules in your plugin. To do that, first, add the following environment variables in your plugin project:
449
450 ```plain title="Plugin project"
451 DB_USERNAME=postgres
452 DB_PASSWORD=123...
453 DB_HOST=localhost
454 DB_PORT=5432
455 DB_NAME=db_name
456 ```
457
458 You can add these environment variables in a `.env` file in your plugin project. The variables are:
459
460 - `DB_USERNAME`: The username of the PostgreSQL user to connect to the database.
461 - `DB_PASSWORD`: The password of the PostgreSQL user to connect to the database.
462 - `DB_HOST`: The host of the PostgreSQL database. Typically, it's `localhost` if you're running the database locally.
463 - `DB_PORT`: The port of the PostgreSQL database. Typically, it's `5432` if you're running the database locally.
464 - `DB_NAME`: The name of the PostgreSQL database to connect to.
465
466 Then, run the following command in your plugin project to generate migrations for the modules in your plugin:
467
468 ```bash npx2yarn title="Plugin project"
469 npx medusa plugin:db:generate
470 ```
471
472 This command generates migrations for all modules in the plugin.
473
474 Finally, run these migrations on the Medusa application that the plugin is installed in using the `db:migrate` command:
475
476 ```bash npx2yarn title="Medusa application"
477 npx medusa db:migrate
478 ```
479
480 The migrations in your application, including your plugin, will run and update the database.
481
482 ### Importing Module Resources
483
484 In the [Prepare Plugin](#2-prepare-plugin) section, you learned about [exported resources](#package-exports) in your plugin.
485
486 These exports allow you to import your plugin resources in your Medusa application, including workflows, links and modules.
487
488 For example, to import the plugin's workflow in your Medusa application:
489
490 <Note title="Tip">
491
492 `@myorg/plugin-name` is the plugin package's name.
493
494 </Note>
495
496 ```ts
497 import { Workflow1, Workflow2 } from "@myorg/plugin-name/workflows"
498 import BlogModule from "@myorg/plugin-name/modules/blog"
499 // import other files created in plugin like ./src/types/blog.ts
500 import BlogType from "@myorg/plugin-name/types/blog"
501 ```
502
503 ### Create Module Providers
504
505 The [exported resources](#package-exports) also allow you to import module providers in your plugin and register them in the Medusa application's configuration. A module provider is a module that provides the underlying logic or integration related to a commerce or Infrastructure Module.
506
507 For example, assuming your plugin has a [Notification Module Provider](!resources!/infrastructure-modules/notification) called `my-notification`, you can register it in your Medusa application's configuration like this:
508
509 <Note title="Tip">
510
511 `@myorg/plugin-name` is the plugin package's name.
512
513 </Note>
514
515 ```ts highlights={[["9"]]} title="medusa-config.ts"
516 module.exports = defineConfig({
517 // ...
518 modules: [
519 {
520 resolve: "@medusajs/medusa/notification",
521 options: {
522 providers: [
523 {
524 resolve: "@myorg/plugin-name/providers/my-notification",
525 id: "my-notification",
526 options: {
527 channels: ["email"],
528 // provider options...
529 },
530 },
531 ],
532 },
533 },
534 ],
535 })
536 ```
537
538 You pass to `resolve` the path to the provider relative to the plugin package. So, in this example, the `my-notification` provider is located in `./src/providers/my-notification/index.ts` of the plugin.
539
540 To learn how to create module providers, refer to the following guides:
541
542 <CardList
543 items={[
544 {
545 title: "File Module Provider",
546 href: "!resources!/references/file-provider-module",
547 },
548 {
549 title: "Notification Module Provider",
550 href: "!resources!/references/notification-provider-module",
551 },
552 {
553 title: "Auth Module Provider",
554 href: "!resources!/references/auth/provider",
555 },
556 {
557 title: "Payment Module Provider",
558 href: "!resources!/references/payment/provider",
559 },
560 {
561 title: "Fulfillment Module Provider",
562 href: "!resources!/references/fulfillment/provider",
563 },
564 {
565 title: "Tax Module Provider",
566 href: "!resources!/references/tax/provider",
567 },
568 ]}
569 className="mb-1.5"
570 />
571
572 ---
573
574 ## 5. Publish Plugin to NPM
575
576 <Note title="Want your plugin to be listed in the Integrations page?">
577
578 Make sure to add the keywords mentioned in the [Package Keywords](#package-keywords) section in your plugin's `package.json` file.
579
580 </Note>
581
582 Medusa's CLI tool provides a command that bundles your plugin to be published to npm. Once you're ready to publish your plugin publicly, run the following command in your plugin project:
583
584 ```bash npx2yarn
585 npx medusa plugin:build
586 ```
587
588 The command will compile an output in the `.medusa/server` directory.
589
590 You can now publish the plugin to npm using the [NPM CLI tool](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm). Run the following command to publish the plugin to npm:
591
592 ```bash
593 npm publish
594 ```
595
596 If you haven't logged in before with your NPM account, you'll be asked to log in first. Then, your package is published publicly to be used in any Medusa application.
597
598 ### Install Public Plugin in Medusa Application
599
600 You install a plugin that's published publicly using your package manager. For example:
601
602 ```bash npm2yarn
603 npm install @myorg/plugin-name
604 ```
605
606 Where `@myorg/plugin-name` is the name of your plugin as published on NPM.
607
608 Then, register the plugin in your Medusa application's configurations as explained in [this section](#register-plugin-in-medusa-application).
609
610 ---
611
612 ## Update a Published Plugin
613
614 <Note title="Tip">
615
616 To update the Medusa dependencies in a plugin, refer to [this documentation](../../../update/page.mdx#update-plugin-project).
617
618 </Note>
619
620 If you've published a plugin and you've made changes to it, you'll have to publish the update to NPM again.
621
622 First, run the following command to change the version of the plugin:
623
624 ```bash
625 npm version <type>
626 ```
627
628 Where `<type>` indicates the type of version update you’re publishing. For example, it can be `major` or `minor`. Refer to the [npm version documentation](https://docs.npmjs.com/cli/v10/commands/npm-version) for more information.
629
630 Then, re-run the same commands for publishing a plugin:
631
632 ```bash npx2yarn
633 npx medusa plugin:build
634 ```
635
636 ```bash npm2yarn
637 npm publish
638 ```
639
640 This will publish an updated version of your plugin under a new version.