1 ---
2 tags:
3 - customer
4 - storefront
5 products:
6 - customer
7 ---
8
9 import { CodeTabs, CodeTab } from "docs-ui"
10
11 export const metadata = {
12 title: `Manage Customer Addresses in Storefront`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to manage a customer's addresses in a storefront. This is useful in the customer's profile page, or when the customer adds an address during checkout and you want to save it for future orders.
18
19 ## List Customer Addresses
20
21 To retrieve the list of customer addresses, send a request to the [List Customer Addresses API route](!api!/store/customers/get-address):
22
23 <Note title="Tip">
24
25 - Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx).
26 - Since only authenticated customers can view their addresses, this example assumes that the JS SDK is already configured for authentication and the customer's authentication token was set as explained in the [Login in Storefront](../login/page.mdx) and [Third-Party Login](../third-party-login/page.mdx) guides.
27
28 </Note>
29
30 <CodeTabs group="store-request">
31 <CodeTab label="React" value="react">
32
33 export const highlights = [
34 ["21", "offset", "Calculate the number of addresses to skip based on the current page and limit."],
35 ["23", "listAddress", "Send a request to retrieve the addresses."],
36 ["27", "count", "The total number of addresses in the Medusa application."],
37 ["39", "setHasMorePages", "Set whether there are more pages based on the total count."],
38 ["53", "", "Using only two address fields for simplicity."],
39 ["60", "button", "Show a button to load more addresses if there are more pages."]
40 ]
41
42 ```tsx highlights={highlights} collapsibleLines="50-77" expandButtonLabel="Show render"
43 "use client" // include with Next.js 13+
44
45 import { HttpTypes } from "@medusajs/types"
46 import { useEffect, useState } from "react"
47 import { sdk } from "@/lib/sdk"
48
49 export default function Addresses() {
50 const [addresses, setAddresses] = useState<
51 HttpTypes.StoreCustomerAddress[]
52 >([])
53 const [loading, setLoading] = useState(true)
54 const limit = 20
55 const [currentPage, setCurrentPage] = useState(1)
56 const [hasMorePages, setHasMorePages] = useState(false)
57
58 useEffect(() => {
59 if (!loading) {
60 return
61 }
62
63 const offset = (currentPage - 1) * limit
64
65 sdk.store.customer.listAddress({
66 limit,
67 offset,
68 })
69 .then(({ addresses: addressesData, count }) => {
70 setAddresses((prev) => {
71 if (prev.length > offset) {
72 // addresses already added because
73 // the same request has already been sent
74 return prev
75 }
76 return [
77 ...prev,
78 ...addressesData,
79 ]
80 })
81 setHasMorePages(count > limit * currentPage)
82 })
83 .finally(() => setLoading(false))
84 }, [loading])
85
86 return (
87 <div>
88 {loading && <span>Loading...</span>}
89 {!loading && !addresses.length && (
90 <span>You have no addresses</span>
91 )}
92 <ul>
93 {addresses.map((address) => (
94 <li key={address.id}>
95 {address.address_name && `${address.address_name} - `}
96 City: {address.city} -
97 Country: {address.country_code}
98 </li>
99 ))}
100 </ul>
101 {!loading && hasMorePages && (
102 <button
103 onClick={() => {
104 setCurrentPage((prev) => prev + 1)
105 setLoading(true)
106 }}
107 disabled={loading}
108 >
109 Load More
110 </button>
111 )}
112 </div>
113 )
114 }
115 ```
116
117 </CodeTab>
118 <CodeTab label="JS SDK" value="js-sdk">
119
120 export const fetchHighlights = [
121 ["2", "limit", "The number of addresses to retrieve"],
122 ["3", "offset", "The number of addresses to skip before those retrieved."],
123 ]
124
125 ```ts highlights={fetchHighlights}
126 sdk.store.customer.listAddress({
127 limit,
128 offset,
129 })
130 .then(({ addresses, count }) => {
131 // use addresses...
132 console.log(addresses, count)
133 })
134 ```
135
136 </CodeTab>
137 </CodeTabs>
138
139 The [List Customer Addresses API route](!api!/store/customers/list-customers-addresses) accepts pagination parameters to paginate the address.
140
141 The request returns in the response the `addresses` field, which is an array of addresses. You can check its structure in the [customer schema](!api!/store/customers/schema).
142
143 The request also returns the `count` field, which is the total number of addresses in the Medusa application. You can use it to check if there are more addresses to retrieve.
144
145 ---
146
147 ## Add Customer Address
148
149 To add a new address for the customer, send a request to the [Add Customer Address API route](!api!/store/customers/create-address):
150
151 <Note title="Tip">
152
153 - This example uses the `useRegion` hook defined in the [Region Context guide](../../regions/context/page.mdx).
154 - This example uses the `useCustomer` hook defined in the [Customer Context guide](../context/page.mdx).
155 - Since only authenticated customers can add addresses, this example assumes that the JS SDK is already configured for authentication and the customer's authentication token was set as explained in the [Login in Storefront](../login/page.mdx) and [Third-Party Login](../third-party-login/page.mdx) guides.
156
157 </Note>
158
159 <CodeTabs group="store-request">
160 <CodeTab label="React" value="react">
161
162 export const addHighlights = [
163 ["4", "useRegion", "Use the hook defined in the Region Context guide."],
164 ["5", "useCustomer", "Use the hook defined in the Customer Context guide."],
165 ]
166
167 ```tsx highlights={addHighlights} collapsibleLines="46-117" expandButtonLabel="Show form"
168 "use client" // include with Next.js 13+
169
170 import { useState } from "react"
171 import { useRegion } from "@/providers/region"
172 import { useCustomer } from "@/providers/customer"
173 import { sdk } from "@/lib/sdk"
174
175 export default function AddAddress() {
176 const { region } = useRegion()
177 const { setCustomer } = useCustomer()
178
179 const [loading, setLoading] = useState(false)
180 const [addressName, setAddressName] = useState("")
181 const [firstName, setFirstName] = useState("")
182 const [lastName, setLastName] = useState("")
183 const [address1, setAddress1] = useState("")
184 const [company, setCompany] = useState("")
185 const [postalCode, setPostalCode] = useState("")
186 const [city, setCity] = useState("")
187 const [countryCode, setCountryCode] = useState("")
188 const [province, setProvince] = useState("")
189 const [phoneNumber, setPhoneNumber] = useState("")
190
191 const handleAdd = (
192 e: React.MouseEvent<HTMLButtonElement, MouseEvent>
193 ) => {
194 e.preventDefault()
195 setLoading(false)
196
197 sdk.store.customer.createAddress({
198 address_name: addressName,
199 first_name: firstName,
200 last_name: lastName,
201 address_1: address1,
202 company,
203 postal_code: postalCode,
204 city,
205 country_code: countryCode,
206 province,
207 phone: phoneNumber,
208 })
209 .then(({ customer }) => {
210 setCustomer(customer)
211 })
212 .finally(() => setLoading(false))
213 }
214
215 return (
216 <form>
217 <input
218 type="text"
219 placeholder="Address Name"
220 value={addressName}
221 onChange={(e) => setAddressName(e.target.value)}
222 />
223 <input
224 type="text"
225 placeholder="First Name"
226 value={firstName}
227 onChange={(e) => setFirstName(e.target.value)}
228 />
229 <input
230 type="text"
231 placeholder="Last Name"
232 value={lastName}
233 onChange={(e) => setLastName(e.target.value)}
234 />
235 <input
236 type="text"
237 placeholder="Address Line"
238 value={address1}
239 onChange={(e) => setAddress1(e.target.value)}
240 />
241 <input
242 type="text"
243 placeholder="Company"
244 value={company}
245 onChange={(e) => setCompany(e.target.value)}
246 />
247 <input
248 type="text"
249 placeholder="Postal Code"
250 value={postalCode}
251 onChange={(e) => setPostalCode(e.target.value)}
252 />
253 <input
254 type="text"
255 placeholder="City"
256 value={city}
257 onChange={(e) => setCity(e.target.value)}
258 />
259 <select
260 value={countryCode}
261 onChange={(e) => setCountryCode(e.target.value)}
262 >
263 {region?.countries?.map((country) => (
264 <option
265 key={country.iso_2}
266 value={country.iso_2}
267 >
268 {country.display_name}
269 </option>
270 ))}
271 </select>
272 <input
273 type="text"
274 placeholder="Province"
275 value={province}
276 onChange={(e) => setProvince(e.target.value)}
277 />
278 <input
279 type="tel"
280 placeholder="Phone Number"
281 value={phoneNumber}
282 onChange={(e) => setPhoneNumber(e.target.value)}
283 />
284 <button
285 disabled={loading}
286 onClick={handleAdd}
287 >
288 Add
289 </button>
290 </form>
291 )
292 }
293 ```
294
295 </CodeTab>
296 <CodeTab label="JS SDK" value="js-sdk">
297
298 ```ts
299 sdk.store.customer.createAddress({
300 address_name: addressName,
301 first_name: firstName,
302 last_name: lastName,
303 address_1: address1,
304 company,
305 postal_code: postalCode,
306 city,
307 country_code: countryCode,
308 province,
309 phone: phoneNumber,
310 })
311 .then(({ customer }) => {
312 // use customer
313 console.log(customer)
314 })
315 ```
316
317 </CodeTab>
318 </CodeTabs>
319
320 In this example, you send a request to the [Add Customer Address API route](!api!/store/customers/create-address) to add a new address for the customer.
321
322 The response of the request has a `customer` field, which is a [customer object](!api!/store/customers/schema). You can access the new address in the `addresses` property of the customer object.
323
324 ---
325
326 ## Edit an Address
327
328 To edit an address, send a request to the [Update Customer Address API route](!api!/store/customers/update-address):
329
330 <Note title="Tip">
331
332 - This example uses the `useRegion` hook defined in the [Region Context guide](../../regions/context/page.mdx).
333 - This example uses the `useCustomer` hook defined in the [Customer Context guide](../context/page.mdx).
334 - Since only authenticated customers can edit their addresses, this example assumes that the JS SDK is already configured for authentication and the customer's authentication token was set as explained in the [Login in Storefront](../login/page.mdx) and [Third-Party Login](../third-party-login/page.mdx) guides.
335
336 </Note>
337
338 <CodeTabs group="store-request">
339 <CodeTab label="React" value="react">
340
341 export const editHighlights = [
342 ["4", "useRegion", "Use the hook defined in the Region Context guide."],
343 ["5", "useCustomer", "Use the hook defined in the Customer Context guide."],
344 ["18", "address", "Retrieve the address from the customer's `addresses` property."],
345 ]
346
347 ```tsx highlights={editHighlights} collapsibleLines="76-147" expandButtonLabel="Show form"
348 "use client" // include with Next.js 13+
349
350 import { useState } from "react"
351 import { useRegion } from "@/providers/region"
352 import { useCustomer } from "@/providers/customer"
353 import { sdk } from "@/lib/sdk"
354
355 type Props = {
356 id: string
357 }
358
359 export default function EditAddress(
360 { id }: Props
361 ) {
362 const { customer, setCustomer } = useCustomer()
363 const { region } = useRegion()
364
365 const address = customer?.addresses.find(
366 (address) => address.id === id
367 )
368 const [loading, setLoading] = useState(false)
369 const [addressName, setAddressName] = useState(
370 address?.address_name || ""
371 )
372 const [firstName, setFirstName] = useState(
373 address?.first_name || ""
374 )
375 const [lastName, setLastName] = useState(
376 address?.last_name || ""
377 )
378 const [address1, setAddress1] = useState(
379 address?.address_1 || ""
380 )
381 const [company, setCompany] = useState(
382 address?.company || ""
383 )
384 const [postalCode, setPostalCode] = useState(
385 address?.postal_code || ""
386 )
387 const [city, setCity] = useState(
388 address?.city || ""
389 )
390 const [countryCode, setCountryCode] = useState(
391 address?.country_code || ""
392 )
393 const [province, setProvince] = useState(
394 address?.province || ""
395 )
396 const [phoneNumber, setPhoneNumber] = useState(
397 address?.phone || ""
398 )
399
400 const handleEdit = (
401 e: React.MouseEvent<HTMLButtonElement, MouseEvent>
402 ) => {
403 e.preventDefault()
404 if (!customer || !address) {
405 return
406 }
407 setLoading(true)
408
409 sdk.store.customer.updateAddress(address.id, {
410 address_name: addressName,
411 first_name: firstName,
412 last_name: lastName,
413 address_1: address1,
414 company,
415 postal_code: postalCode,
416 city,
417 country_code: countryCode,
418 province,
419 phone: phoneNumber,
420 })
421 .then(({ customer }) => {
422 setCustomer(customer)
423 })
424 .finally(() => setLoading(false))
425 }
426
427 return (
428 <form>
429 <input
430 type="text"
431 placeholder="Address Name"
432 value={addressName}
433 onChange={(e) => setAddressName(e.target.value)}
434 />
435 <input
436 type="text"
437 placeholder="First Name"
438 value={firstName}
439 onChange={(e) => setFirstName(e.target.value)}
440 />
441 <input
442 type="text"
443 placeholder="Last Name"
444 value={lastName}
445 onChange={(e) => setLastName(e.target.value)}
446 />
447 <input
448 type="text"
449 placeholder="Address Line"
450 value={address1}
451 onChange={(e) => setAddress1(e.target.value)}
452 />
453 <input
454 type="text"
455 placeholder="Company"
456 value={company}
457 onChange={(e) => setCompany(e.target.value)}
458 />
459 <input
460 type="text"
461 placeholder="Postal Code"
462 value={postalCode}
463 onChange={(e) => setPostalCode(e.target.value)}
464 />
465 <input
466 type="text"
467 placeholder="City"
468 value={city}
469 onChange={(e) => setCity(e.target.value)}
470 />
471 <select
472 value={countryCode}
473 onChange={(e) => setCountryCode(e.target.value)}
474 >
475 {region?.countries?.map((country) => (
476 <option
477 key={country.iso_2}
478 value={country.iso_2}
479 >
480 {country.display_name}
481 </option>
482 ))}
483 </select>
484 <input
485 type="text"
486 placeholder="Province"
487 value={province}
488 onChange={(e) => setProvince(e.target.value)}
489 />
490 <input
491 type="tel"
492 placeholder="Phone Number"
493 value={phoneNumber}
494 onChange={(e) => setPhoneNumber(e.target.value)}
495 />
496 <button
497 disabled={loading}
498 onClick={handleEdit}
499 >
500 Add
501 </button>
502 </form>
503 )
504 }
505 ```
506
507 </CodeTab>
508 <CodeTab label="JS SDK" value="js-sdk">
509
510 ```ts
511 sdk.store.customer.updateAddress(address.id, {
512 address_name: addressName,
513 first_name: firstName,
514 last_name: lastName,
515 address_1: address1,
516 company,
517 postal_code: postalCode,
518 city,
519 country_code: countryCode,
520 province,
521 phone: phoneNumber,
522 })
523 .then(({ customer }) => {
524 // use customer...
525 console.log(customer)
526 })
527 ```
528
529 </CodeTab>
530 </CodeTabs>
531
532 In this example, you send a request to the [Update Customer Address API route](!api!/store/customers/update-address) to edit an address.
533
534 The response of the request has a `customer` field, which is a [customer object](!api!/store/customers/schema). You can access the updated address in the `addresses` property of the customer object.
535
536 ---
537
538 ## Delete Customer Address
539
540 To delete a customer's address, send a request to the [Delete Customer Address API route](!api!/store/customers/remove-address):
541
542 <Note title="Tip">
543
544 Since only authenticated customers can delete their addresses, this example assumes that the JS SDK is already configured for authentication and the customer's authentication token was set as explained in the [Login in Storefront](../login/page.mdx) and [Third-Party Login](../third-party-login/page.mdx) guides.
545
546 </Note>
547
548 export const deleteHighlights = [
549 ["1", "addrId", "The ID of the address to delete."],
550 ["2", "parent", "The updated customer object."]
551 ]
552
553 ```ts highlights={deleteHighlights}
554 sdk.store.customer.deleteAddress(addrId)
555 .then(({ parent: customer }) => {
556 // use customer...
557 console.log(customer)
558 })
559 ```
560
561 In this example, you send a request to the [Delete Customer Address API route](!api!/store/customers/remove-address) to delete an address.
562
563 The response of the request has a `parent` field, which is a [customer object](!api!/store/customers/schema). You can access the updated customer in the `parent` property of the response.