1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Currency Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Currency 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](!user-guide!/settings/store) to learn how to manage your store's currencies using the dashboard.
18
19 </Note>
20
21 Medusa has currency related features available out-of-the-box through the Currency 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 Currency Module.
22
23 <Note>
24
25 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
26
27 </Note>
28
29 ## Currency Features
30
31 - [Currency Management and Retrieval](/references/currency/listAndCountCurrencies): This module adds all common currencies to your application and allows you to retrieve them.
32 - [Support Currencies in Modules](./links-to-other-modules/page.mdx): Other Commerce Modules use currency codes in their data models or operations. Use the Currency Module to retrieve a currency code and its details.
33
34 ---
35
36 ## How to Use the Currency Module
37
38 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.
39
40 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.
41
42 For example:
43
44 export const highlights = [
45 ["13", "Modules.CURRENCY", "Resolve the module in a step."]
46 ]
47
48 ```ts title="src/workflows/retrieve-price-with-currency.ts" highlights={highlights}
49 import {
50 createWorkflow,
51 WorkflowResponse,
52 createStep,
53 StepResponse,
54 transform,
55 } from "@medusajs/framework/workflows-sdk"
56 import { Modules } from "@medusajs/framework/utils"
57
58 const retrieveCurrencyStep = createStep(
59 "retrieve-currency",
60 async ({}, { container }) => {
61 const currencyModuleService = container.resolve(Modules.CURRENCY)
62
63 const currency = await currencyModuleService
64 .retrieveCurrency("usd")
65
66 return new StepResponse({ currency })
67 }
68 )
69
70 type Input = {
71 price: number
72 }
73
74 export const retrievePriceWithCurrency = createWorkflow(
75 "create-currency",
76 (input: Input) => {
77 const { currency } = retrieveCurrencyStep()
78
79 const formattedPrice = transform({
80 input,
81 currency,
82 }, (data) => {
83 return `${data.currency.symbol}${data.input.price}`
84 })
85
86 return new WorkflowResponse({
87 formattedPrice,
88 })
89 }
90 )
91 ```
92
93 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
94
95 <CodeTabs group="resource-types">
96 <CodeTab label="API Route" value="api-route">
97
98 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"], ["13"], ["14"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
99 import type {
100 MedusaRequest,
101 MedusaResponse,
102 } from "@medusajs/framework/http"
103 import { retrievePriceWithCurrency } from "../../workflows/retrieve-price-with-currency"
104
105 export async function GET(
106 req: MedusaRequest,
107 res: MedusaResponse
108 ) {
109 const { result } = await retrievePriceWithCurrency(req.scope)
110 .run({
111 price: 10,
112 })
113
114 res.send(result)
115 }
116 ```
117
118 </CodeTab>
119 <CodeTab label="Subscriber" value="subscriber">
120
121 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"], ["13"], ["14"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
122 import {
123 type SubscriberConfig,
124 type SubscriberArgs,
125 } from "@medusajs/framework"
126 import { retrievePriceWithCurrency } from "../workflows/retrieve-price-with-currency"
127
128 export default async function handleUserCreated({
129 event: { data },
130 container,
131 }: SubscriberArgs<{ id: string }>) {
132 const { result } = await retrievePriceWithCurrency(container)
133 .run({
134 price: 10,
135 })
136
137 console.log(result)
138 }
139
140 export const config: SubscriberConfig = {
141 event: "user.created",
142 }
143 ```
144
145 </CodeTab>
146 <CodeTab label="Scheduled Job" value="scheduled-job">
147
148 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"], ["9"], ["10"]]}
149 import { MedusaContainer } from "@medusajs/framework/types"
150 import { retrievePriceWithCurrency } from "../workflows/retrieve-price-with-currency"
151
152 export default async function myCustomJob(
153 container: MedusaContainer
154 ) {
155 const { result } = await retrievePriceWithCurrency(container)
156 .run({
157 price: 10,
158 })
159
160 console.log(result)
161 }
162
163 export const config = {
164 name: "run-once-a-day",
165 schedule: `0 0 * * *`,
166 }
167 ```
168
169 </CodeTab>
170 </CodeTabs>
171
172 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
173
174 ---
175
176 <CommerceModuleSections />