1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Guide: Integrate Third-Party Brand System`,
5 }
6
7 # {metadata.title}
8
9 In the previous chapters, you've created a [Brand Module](../../custom-features/module/page.mdx) that adds brands to your application. In this chapter, you'll integrate a dummy Content-Management System (CMS) in a new module. The module's service will provide methods to retrieve and manage brands in the CMS. You'll later use this service to sync data from and to the CMS.
10
11 <Note>
12
13 Learn more about modules in [this chapter](../../../fundamentals/modules/page.mdx).
14
15 </Note>
16
17 ## 1. Create Module Directory
18
19 You'll integrate the third-party system in a new CMS Module. So, create the directory `src/modules/cms` that will hold the module's resources.
20
21 ![Directory structure after adding the directory for the CMS Module](https://res.cloudinary.com/dza7lstvk/image/upload/v1733492447/Medusa%20Book/cms-dir-overview-1_gasguk.jpg)
22
23 ---
24
25 ## 2. Create Module Service
26
27 Next, you'll create the module's service. It will provide methods to connect and perform actions with the third-party system.
28
29 Create the CMS Module's service at `src/modules/cms/service.ts` with the following content:
30
31 ![Directory structure after adding the CMS Module's service](https://res.cloudinary.com/dza7lstvk/image/upload/v1733492583/Medusa%20Book/cms-dir-overview-2_zwcwh3.jpg)
32
33 export const serviceHighlights = [
34 ["3", "ModuleOptions", "The options that the CMS Module receives."],
35 ["7", "InjectedDependencies", "The dependencies injected into the service from the module's container."],
36 ["16", "logger", "Dependencies injected from the module's container"],
37 ["16", "options", "Options passed to the module in the configurations."]
38 ]
39
40 ```ts title="src/modules/cms/service.ts" highlights={serviceHighlights}
41 import { Logger, ConfigModule } from "@medusajs/framework/types"
42
43 export type ModuleOptions = {
44 apiKey: string
45 }
46
47 type InjectedDependencies = {
48 logger: Logger
49 configModule: ConfigModule
50 }
51
52 class CmsModuleService {
53 private options_: ModuleOptions
54 private logger_: Logger
55
56 constructor({ logger }: InjectedDependencies, options: ModuleOptions) {
57 this.logger_ = logger
58 this.options_ = options
59
60 // TODO initialize SDK
61 }
62 }
63
64 export default CmsModuleService
65 ```
66
67 You create a `CmsModuleService` that will hold the methods to connect to the third-party CMS. A service's constructor accepts two parameters:
68
69 1. The module's container. Since a module is [isolated](../../../fundamentals/modules/isolation/page.mdx), it has a [local container](../../../fundamentals/modules/container/page.mdx) different than the Medusa container you use in other customizations. This container holds Framework tools like the [Logger utility](../../../debugging-and-testing/logging/page.mdx) and resources within the module.
70 2. Options passed to the module when it's later added in Medusa's configurations. These options are useful to pass secret keys or configurations that ensure your module is re-usable across applications. For the CMS Module, you accept the API key to connect to the dummy CMS as an option.
71
72 When integrating a third-party system that has a Node.js SDK or client, you can initialize that client in the constructor to be used in the service's methods.
73
74 ### Integration Methods
75
76 Next, you'll add methods that simulate sending requests to a third-party CMS. You'll use these methods later to sync brands from and to the CMS.
77
78 Add the following methods in the `CmsModuleService`:
79
80 export const methodsHighlights = [
81 ["6", "sendRequest", "Since the third-party system isn't real, this method only logs a message."],
82 ["12", "createBrand", "A method that creates a brand in the third-party system."],
83 ["16", "deleteBrand", "A method that deletes a brand in the third-party system."],
84 ["20", "retrieveBrands", "A method that retrieves a brand from a third-party system."]
85 ]
86
87 ```ts title="src/modules/cms/service.ts" highlights={methodsHighlights}
88 export class CmsModuleService {
89 // ...
90
91 // a dummy method to simulate sending a request,
92 // in a realistic scenario, you'd use an SDK, fetch, or axios clients
93 private async sendRequest(url: string, method: string, data?: any) {
94 this.logger_.info(`Sending a ${method} request to ${url}.`)
95 this.logger_.info(`Request Data: ${JSON.stringify(data, null, 2)}`)
96 this.logger_.info(`API Key: ${JSON.stringify(this.options_.apiKey, null, 2)}`)
97 }
98
99 async createBrand(brand: Record<string, unknown>) {
100 await this.sendRequest("/brands", "POST", brand)
101 }
102
103 async deleteBrand(id: string) {
104 await this.sendRequest(`/brands/${id}`, "DELETE")
105 }
106
107 async retrieveBrands(): Promise<Record<string, unknown>[]> {
108 await this.sendRequest("/brands", "GET")
109
110 return []
111 }
112 }
113 ```
114
115 The `sendRequest` method sends requests to the third-party CMS. Since this guide isn't using a real CMS, it only simulates the sending by logging messages in the terminal.
116
117 You also add three methods that use the `sendRequest` method:
118
119 - `createBrand` that creates a brand in the third-party system.
120 - `deleteBrand` that deletes the brand in the third-party system.
121 - `retrieveBrands` to retrieve a brand from the third-party system.
122
123 ---
124
125 ## 3. Export Module Definition
126
127 After creating the module's service, you'll export the module definition indicating the module's name and service.
128
129 Create the file `src/modules/cms/index.ts` with the following content:
130
131 ![Directory structure of the Medusa application after adding the module definition file](https://res.cloudinary.com/dza7lstvk/image/upload/v1733492991/Medusa%20Book/cms-dir-overview-3_b0byks.jpg)
132
133 ```ts title="src/modules/cms/index.ts"
134 import { Module } from "@medusajs/framework/utils"
135 import CmsModuleService from "./service"
136
137 export const CMS_MODULE = "cms"
138
139 export default Module(CMS_MODULE, {
140 service: CmsModuleService,
141 })
142 ```
143
144 You use `Module` from the Modules SDK to export the module's definition, indicating that the module's name is `cms` and its service is `CmsModuleService`.
145
146 ---
147
148 ## 4. Add Module to Medusa's Configurations
149
150 Finally, add the module to the Medusa configurations at `medusa-config.ts`:
151
152 ```ts title="medusa-config.ts"
153 module.exports = defineConfig({
154 // ...
155 modules: [
156 // ...
157 {
158 resolve: "./src/modules/cms",
159 options: {
160 apiKey: process.env.CMS_API_KEY,
161 },
162 },
163 ],
164 })
165 ```
166
167 The object passed in `modules` accept an `options` property, whose value is an object of options to pass to the module. These are the options you receive in the `CmsModuleService`'s constructor.
168
169 You can add the `CMS_API_KEY` environment variable to `.env`:
170
171 ```bash
172 CMS_API_KEY=123
173 ```
174
175 ---
176
177 ## Next Steps: Sync Brand From Medusa to CMS
178
179 You can now use the CMS Module's service to perform actions on the third-party CMS.
180
181 In the next chapter, you'll learn how to emit an event when a brand is created, then handle that event to sync the brand from Medusa to the third-party service.