Skip to content

Commit 3ac9425

Browse files
authored
docs: add section on using JS SDK for downloading files (#16140)
1 parent 7d7edad commit 3ac9425

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

www/apps/book/public/llms-full.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56589,6 +56589,40 @@ The method returns a Promise that, when resolved, has the data returned by the r
5658956589

5659056590
***
5659156591

56592+
## Download Binary or File Responses
56593+
56594+
By default, the `fetch` method sets the `accept` header of the request to `application/json`, so it parses the response as JSON and returns a JavaScript object.
56595+
56596+
The `fetch` method decides whether to parse the response as JSON based on the request's `accept` header, not the response's content type. So, if your custom route returns a binary or file response, such as a PDF or an image, you must override the `accept` header to a value other than `application/json`. The method then returns the raw [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object without parsing it.
56597+
56598+
For example, to download a PDF file returned by a custom route:
56599+
56600+
```tsx
56601+
const response: Response = await sdk.client.fetch(
56602+
`/admin/orders/${orderId}/invoice`,
56603+
{
56604+
method: "GET",
56605+
headers: {
56606+
accept: "application/pdf",
56607+
},
56608+
}
56609+
)
56610+
56611+
const blob = await response.blob()
56612+
const url = window.URL.createObjectURL(blob)
56613+
const link = document.createElement("a")
56614+
link.href = url
56615+
link.download = `invoice-${orderId}.pdf`
56616+
link.click()
56617+
window.URL.revokeObjectURL(url)
56618+
```
56619+
56620+
In this example, you set the `accept` header to `application/pdf`, so the `fetch` method returns the raw `Response` object. You then use the [Response.blob](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) method to read the response as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), which you can download in the browser.
56621+
56622+
Since the method returns the raw `Response` object in this case, type the returned value as `Response`. You're then responsible for reading the response's body, using methods like `blob`, [arrayBuffer](https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer), or [text](https://developer.mozilla.org/en-US/docs/Web/API/Response/text).
56623+
56624+
***
56625+
5659256626
## Migrating from Fetch API to JS SDK
5659356627

5659456628
Prefer `sdk.client.fetch` over the raw [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) when sending requests to your Medusa backend.

www/apps/resources/app/js-sdk/page.mdx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,44 @@ The method returns a Promise that, when resolved, has the data returned by the r
398398

399399
---
400400

401+
## Download Binary or File Responses
402+
403+
By default, the `fetch` method sets the `accept` header of the request to `application/json`, so it parses the response as JSON and returns a JavaScript object.
404+
405+
The `fetch` method decides whether to parse the response as JSON based on the request's `accept` header, not the response's content type. So, if your custom route returns a binary or file response, such as a PDF or an image, you must override the `accept` header to a value other than `application/json`. The method then returns the raw [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object without parsing it.
406+
407+
For example, to download a PDF file returned by a custom route:
408+
409+
```tsx
410+
const response: Response = await sdk.client.fetch(
411+
`/admin/orders/${orderId}/invoice`,
412+
{
413+
method: "GET",
414+
headers: {
415+
accept: "application/pdf",
416+
},
417+
}
418+
)
419+
420+
const blob = await response.blob()
421+
const url = window.URL.createObjectURL(blob)
422+
const link = document.createElement("a")
423+
link.href = url
424+
link.download = `invoice-${orderId}.pdf`
425+
link.click()
426+
window.URL.revokeObjectURL(url)
427+
```
428+
429+
In this example, you set the `accept` header to `application/pdf`, so the `fetch` method returns the raw `Response` object. You then use the [Response.blob](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) method to read the response as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), which you can download in the browser.
430+
431+
<Note>
432+
433+
Since the method returns the raw `Response` object in this case, type the returned value as `Response`. You're then responsible for reading the response's body, using methods like `blob`, [arrayBuffer](https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer), or [text](https://developer.mozilla.org/en-US/docs/Web/API/Response/text).
434+
435+
</Note>
436+
437+
---
438+
401439
## Migrating from Fetch API to JS SDK
402440

403441
Prefer `sdk.client.fetch` over the raw [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) when sending requests to your Medusa backend.

www/apps/resources/generated/edit-dates.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@ export const generatedEditDates = {
21252125
"app/commerce-modules/store/links-to-other-modules/page.mdx": "2025-12-16T06:36:58.256Z",
21262126
"app/examples/page.mdx": "2026-03-27T12:55:32.030Z",
21272127
"app/medusa-cli/commands/build/page.mdx": "2026-06-24T11:05:27.287Z",
2128-
"app/js-sdk/page.mdx": "2025-12-16T07:15:37.754Z",
2128+
"app/js-sdk/page.mdx": "2026-07-20T15:06:29.274Z",
21292129
"references/js_sdk/admin/Admin/properties/js_sdk.admin.Admin.apiKey/page.mdx": "2026-04-30T16:36:41.141Z",
21302130
"references/js_sdk/admin/Admin/properties/js_sdk.admin.Admin.campaign/page.mdx": "2026-01-06T15:55:12.396Z",
21312131
"references/js_sdk/admin/Admin/properties/js_sdk.admin.Admin.claim/page.mdx": "2026-04-30T16:36:40.978Z",

0 commit comments

Comments
 (0)