* docs(refactoring): configured eslint and typescript (#3511) * docs: configured eslint and typescript * fixed yarn.lock * docs(refactoring): migrate components directory to typescript (#3517) * docs: migrate components directory to typescript * removed vscode settings * fix following merge * docs: refactored QueryNote component (#3576) * docs: refactored first batch of theme components (#3579) * docs: refactored second batch of theme components (#3580) * added missing badge styles * fix after merge * docs(refactoring): migrated remaining component to TypeScript (#3770) * docs(refactoring): configured eslint and typescript (#3511) * docs: configured eslint and typescript * fixed yarn.lock * docs(refactoring): migrate components directory to typescript (#3517) * docs: migrate components directory to typescript * removed vscode settings * fix following merge * docs: refactored QueryNote component (#3576) * docs: refactored first batch of theme components (#3579) * docs: refactored second batch of theme components (#3580) * added missing badge styles * docs: refactoring second batch of theme components * fix after merge * refactored icons and other components * docs: refactored all components * docs(refactoring): set up and configured Tailwind Css (#3841) * docs: added tailwind config * docs: added more tailwind configurations * add includes option * added more tailwind configurations * fix to configurations * docs(refactoring): use tailwind css (#4134) * docs: added tailwind config * docs: added more tailwind configurations * add includes option * added more tailwind configurations * fix to configurations * docs(refactoring): refactored all styles to use tailwind css (#4132) * refactored Badge component to use tailwind css * refactored Bordered component to use tailwind css * updated to latest docusaurus * refactored BorderedIcon component to use tailwind css * refactored Feedback component to use tailwind css * refactored icons and footersociallinks to tailwind css * start refactoring of large card * refactored large card styling * refactored until admonitions * refactored until codeblock * refactored until Tabs * refactored Tabs (without testing * finished refactoring styles to tailwind css * upgraded to version 2.4.1 * general fixes * adjusted eslint configurations * fixed ignore files * fixes to large card * fix search styling * fix npx command * updated tabs to use isCodeTabs prop * fixed os tabs * removed os-tabs class in favor of general styling * improvements to buttons * fix for searchbar * fixed redocly download button * chore: added eslint code action (#4135) * small change in commerce modules page
621 lines
16 KiB
Plaintext
621 lines
16 KiB
Plaintext
---
|
||
description: 'Learn how to manage stock locations using the admin APIs. This includes how to list, create, update, and delete stock locations.'
|
||
addHowToData: true
|
||
---
|
||
|
||
import Tabs from '@theme/Tabs';
|
||
import TabItem from '@theme/TabItem';
|
||
|
||
# How to Manage Stock Locations
|
||
|
||
In this document, you’ll learn how to manage stock locations using the admin APIs.
|
||
|
||
## Overview
|
||
|
||
Using the stock locations admin REST APIs, you can manage stock locations in your store, including creating, updating, and deleting locations.
|
||
|
||
### Scenario
|
||
|
||
You want to add or use the following admin functionalities:
|
||
|
||
- List stock locations.
|
||
- Create a stock location.
|
||
- Retrieve a stock location.
|
||
- Associate a stock location with a sales channel, and remove the association.
|
||
- Update a stock location.
|
||
- Delete a stock location.
|
||
|
||
---
|
||
|
||
## Prerequisites
|
||
|
||
### Medusa Components
|
||
|
||
It is assumed that you already have a Medusa backend installed and set up. If not, you can follow the [quickstart guide](../../../development/backend/install.mdx) to get started.
|
||
|
||
### Required Module
|
||
|
||
This guide assumes you have a stock location module installed. You can use Medusa’s [Stock Location module](../install-modules.md#stock-location-module) or [create your own module](../backend/create-stock-location-service.md).
|
||
|
||
### JS Client
|
||
|
||
This guide includes code snippets to send requests to your Medusa backend using Medusa’s JS Client, among other methods.
|
||
|
||
If you follow the JS Client code blocks, it’s assumed you already have [Medusa’s JS Client](../../../js-client/overview.md) installed and have [created an instance of the client](../../../js-client/overview.md#configuration).
|
||
|
||
### Medusa React
|
||
|
||
This guide also includes code snippets to send requests to your Medusa backend using Medusa React, among other methods.
|
||
|
||
If you follow the Medusa React code blocks, it's assumed you already have [Medusa React installed](../../../medusa-react/overview.md) and have [used MedusaProvider higher in your component tree](../../../medusa-react/overview.md#usage).
|
||
|
||
### Authenticated Admin User
|
||
|
||
You must be an authenticated admin user before following along with the steps in the tutorial.
|
||
|
||
You can learn more about [authenticating as an admin user in the API reference](/api/admin/#section/Authentication).
|
||
|
||
---
|
||
|
||
## List Stock Locations
|
||
|
||
You can list stock locations by using the [List Stock Locations endpoint](/api/admin#tag/Stock-Locations/operation/GetStockLocations):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.stockLocations.list()
|
||
.then(({ stock_locations, limit, offset, count }) => {
|
||
console.log(stock_locations.length)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminStockLocations } from "medusa-react"
|
||
|
||
function StockLocations() {
|
||
const {
|
||
stock_locations,
|
||
isLoading } = useAdminStockLocations()
|
||
|
||
return (
|
||
<div>
|
||
{isLoading && <span>Loading...</span>}
|
||
{stock_locations && !stock_locations.length && (
|
||
<span>No Locations</span>
|
||
)}
|
||
{stock_locations && stock_locations.length > 0 && (
|
||
<ul>
|
||
{stock_locations.map(
|
||
(location) => (
|
||
<li key={location.id}>{location.name}</li>
|
||
)
|
||
)}
|
||
</ul>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
export default StockLocations
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/stock-locations`, {
|
||
credentials: "include",
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ stock_locations, limit, offset, count }) => {
|
||
console.log(stock_locations.length)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X GET '<BACKEND_URL>/admin/stock-locations' \
|
||
-H 'Authorization: Bearer <API_TOKEN>'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint does not require any path or query parameters. You can, however, pass it query parameters to search or filter the list of stock locations. For example, you can pass the `q` query parameter to search through the locations by name. You can learn about available query parameters in the [API reference](/api/admin#tag/Stock-Locations/operation/GetStockLocations).
|
||
|
||
The request returns an array of stock location objects along with [pagination parameters](/api/admin#section/Pagination).
|
||
|
||
---
|
||
|
||
## Create a Stock Location
|
||
|
||
You can create a stock location using the [Create a Stock Location endpoint](/api/admin#tag/Stock-Locations/operation/PostStockLocations):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.stockLocations.create({
|
||
name: "Main Warehouse",
|
||
})
|
||
.then(({ stock_location }) => {
|
||
console.log(stock_location.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminCreateStockLocation } from "medusa-react"
|
||
|
||
const CreateStockLocation = () => {
|
||
const createStockLocation = useAdminCreateStockLocation()
|
||
// ...
|
||
|
||
const handleCreate = () => {
|
||
createStockLocation.mutate({
|
||
name: "Main Warehouse",
|
||
})
|
||
}
|
||
|
||
// ...
|
||
}
|
||
|
||
export default CreateStockLocation
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/stock-locations`, {
|
||
credentials: "include",
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
name: "Main Warehouse",
|
||
}),
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ stock_location }) => {
|
||
console.log(stock_location.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X POST '<BACKEND_URL>/admin/stock-locations' \
|
||
-H 'Authorization: Bearer <API_TOKEN>' \
|
||
-H 'Content-Type: application/json' \
|
||
--data-raw '{
|
||
"name": "Main Warehouse"
|
||
}'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires in its body parameters the `name` field, which is the name of the stock location. You can also pass in the request body parameters other fields related to the address or metadata. You can learn more in the [API reference](/api/admin#tag/Stock-Locations/operation/PostStockLocations).
|
||
|
||
This request returns the created stock location as an object.
|
||
|
||
---
|
||
|
||
## Retrieve a Stock Location
|
||
|
||
You can retrieve a stock location by sending a request to the [Get Stock Location endpoint](/api/admin#tag/Stock-Locations/operation/GetStockLocationsStockLocation):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.stockLocations.retrieve(stockLocationId)
|
||
.then(({ stock_location }) => {
|
||
console.log(stock_location.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminStockLocation } from "medusa-react"
|
||
|
||
function StockLocation() {
|
||
const {
|
||
stock_location,
|
||
isLoading } = useAdminStockLocation(stockLocationId)
|
||
|
||
return (
|
||
<div>
|
||
{isLoading && <span>Loading...</span>}
|
||
{stock_location && (
|
||
<span>{stock_location.name}</span>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
export default StockLocation
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/stock-locations/${stockLocationId}`,
|
||
{
|
||
credentials: "include",
|
||
}
|
||
)
|
||
.then((response) => response.json())
|
||
.then(({ stock_location }) => {
|
||
console.log(stock_location.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X GET '<BACKEND_URL>/admin/stock-locations/<LOC_ID>' \
|
||
-H 'Authorization: Bearer <API_TOKEN>'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the stock location ID to be passed as a path parameter. It also accepts query parameters related to expanding and selecting fields. You can learn more in the [API reference](/api/admin#tag/Stock-Locations/operation/GetStockLocationsStockLocation).
|
||
|
||
It returns the stock location as an object.
|
||
|
||
---
|
||
|
||
## Associate a Stock Location with a Sales Channel
|
||
|
||
You can associate a stock location with a sales channel by sending a request to the [Associate Stock Channel endpoint](/api/admin#tag/Sales-Channels/operation/PostSalesChannelsSalesChannelStockLocation):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.salesChannels.addLocation(salesChannelId, {
|
||
location_id,
|
||
})
|
||
.then(({ sales_channel }) => {
|
||
console.log(sales_channel.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminAddLocationToSalesChannel } from "medusa-react"
|
||
|
||
function StockLocation() {
|
||
const addLocation = useAdminAddLocationToSalesChannel()
|
||
// ...
|
||
|
||
const handleAdd = () => {
|
||
addLocation.mutate({
|
||
sales_channel_id,
|
||
location_id,
|
||
})
|
||
}
|
||
}
|
||
|
||
export default StockLocation
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
<!-- eslint-disable max-len -->
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/sales-channels/${salesChannelId}/stock-locations`, {
|
||
credentials: "include",
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
location_id,
|
||
}),
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ sales_channel }) => {
|
||
console.log(sales_channel.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X POST '<BACKEND_URL>/admin/sales-channels/<CHANNEL_ID>/stock-locations' \
|
||
-H 'Authorization: Bearer <API_TOKEN>' \
|
||
-H 'Content-Type: application/json' \
|
||
--data-raw '{
|
||
"location_id": "<LOC_ID>"
|
||
}'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the ID of the sales channel as a path parameter. In its body parameters, it requires the ID of the stock location you’re associating the sales channel with.
|
||
|
||
This request returns the sales channel object.
|
||
|
||
:::note
|
||
|
||
You can associate a location with more than one sales channel, and you can associate a sales channel with more than one location.
|
||
|
||
:::
|
||
|
||
---
|
||
|
||
## Remove Association Between Stock Location and Sales Channel
|
||
|
||
You can remove the association between a stock location and a sales channel by sending a request to the [Remove Stock Location Association endpoint](/api/admin#tag/Sales-Channels/operation/DeleteSalesChannelsSalesChannelStockLocation):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.salesChannels.removeLocation(salesChannelId, {
|
||
location_id,
|
||
})
|
||
.then(({ id, object, deleted }) => {
|
||
console.log(id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import {
|
||
useAdminRemoveLocationFromSalesChannel,
|
||
} from "medusa-react"
|
||
|
||
function StockLocation() {
|
||
const removeLocation =
|
||
useAdminRemoveLocationFromSalesChannel()
|
||
// ...
|
||
|
||
const handleRemove = () => {
|
||
removeLocation.mutate({
|
||
sales_channel_id,
|
||
location_id,
|
||
})
|
||
}
|
||
}
|
||
|
||
export default StockLocation
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
<!-- eslint-disable max-len -->
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/sales-channels/${salesChannelId}/stock-locations`, {
|
||
credentials: "include",
|
||
method: "DELETE",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
location_id,
|
||
}),
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ id, object, deleted }) => {
|
||
console.log(id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X DELETE '<BACKEND_URL>/admin/sales-channels/<CHANNEL_ID>/stock-locations' \
|
||
-H 'Authorization: Bearer <API_TOKEN>' \
|
||
-H 'Content-Type: application/json' \
|
||
--data-raw '{
|
||
"location_id": "<LOC_ID>"
|
||
}'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the ID of the sales channel as a path parameter. In its body parameters, it requires the ID of the stock location you’re removing the association of the sales channel with.
|
||
|
||
The request returns the following fields:
|
||
|
||
- `id`: The ID of the location.
|
||
- `object`: The type of object that was removed. In this case, the value will be `stock-location`.
|
||
- `deleted`: A boolean value indicating whether the association with the stock location was removed.
|
||
|
||
:::note
|
||
|
||
This request does not delete the stock location. It only removes the association between it and the specified sales channel.
|
||
|
||
:::
|
||
|
||
---
|
||
|
||
## Update a Stock Location
|
||
|
||
You can update a stock location by sending a request to the [Update Stock Location endpoint](/api/admin#tag/Stock-Locations/operation/PostStockLocationsStockLocation):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.stockLocations.update(stockLocationId, {
|
||
name: "Warehouse",
|
||
})
|
||
.then(({ stock_location }) => {
|
||
console.log(stock_location.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminUpdateStockLocation } from "medusa-react"
|
||
|
||
function StockLocation() {
|
||
const updateLocation = useAdminUpdateStockLocation(
|
||
stockLocationId
|
||
)
|
||
// ...
|
||
|
||
const handleRemove = () => {
|
||
updateLocation.mutate({
|
||
name: "Warehouse",
|
||
})
|
||
}
|
||
}
|
||
|
||
export default StockLocation
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/stock-locations/${stockLocationId}`,
|
||
{
|
||
credentials: "include",
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
name: "Warehouse",
|
||
}),
|
||
}
|
||
)
|
||
.then((response) => response.json())
|
||
.then(({ stock_location }) => {
|
||
console.log(stock_location.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X POST '<BACKEND_URL>/admin/stock-locations/<LOC_ID>' \
|
||
-H 'Authorization: Bearer <API_TOKEN>' \
|
||
-H 'Content-Type: application/json' \
|
||
--data-raw '{
|
||
"name": "Main Warehouse"
|
||
}'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the ID of a stock location as a path parameter. In its body parameters, you can pass any of the location’s attributes to update, such as the name or address. You can learn more in the [API reference](/api/admin#tag/Stock-Locations/operation/PostStockLocationsStockLocation).
|
||
|
||
This request returns the updated stock location as an object.
|
||
|
||
---
|
||
|
||
## Delete a Stock Location
|
||
|
||
You can delete a stock location by sending a request to the [Delete Stock Location endpoint](/api/admin#tag/Stock-Locations/operation/DeleteStockLocationsStockLocation):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.stockLocations.delete(stockLocationId)
|
||
.then(({ id, object, deleted }) => {
|
||
console.log(id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminDeleteStockLocation } from "medusa-react"
|
||
|
||
function StockLocation() {
|
||
const deleteLocation = useAdminDeleteStockLocation(
|
||
stockLocationId
|
||
)
|
||
// ...
|
||
|
||
const handleDelete = () => {
|
||
deleteLocation.mutate()
|
||
}
|
||
}
|
||
|
||
export default StockLocation
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/stock-locations/${stockLocationId}`,
|
||
{
|
||
credentials: "include",
|
||
method: "DELETE",
|
||
}
|
||
)
|
||
.then((response) => response.json())
|
||
.then(({ id, object, deleted }) => {
|
||
console.log(id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X DELETE '<BACKEND_URL>/admin/stock-locations/<LOC_ID>' \
|
||
-H 'Authorization: Bearer <API_TOKEN>'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the ID of the stock location as a path parameter.
|
||
|
||
It returns the following fields in the response:
|
||
|
||
- `id`: The ID of the location.
|
||
- `object`: The type of object that was deleted. In this case, the value will be `stock_location`.
|
||
- `deleted`: A boolean value indicating whether the stock location was deleted successfully.
|
||
|
||
---
|
||
|
||
## See Also
|
||
|
||
- [How to manage inventory items](./manage-inventory-items.mdx)
|
||
- [How to manage item allocations in orders](./manage-item-allocations-in-orders.mdx)
|