Skip to main content
Skip to main content

Stock Locations

Queries and Mutations listed here are used to send requests to the Admin Stock Location API Routes. To use these hooks, make sure to install the @medusajs/stock-location module in your Medusa backend.

All hooks listed require authentication.

A stock location, provided by the Stock Location module, indicates a physical address that stock-kept items, such as physical products, can be stored in. An admin can create and manage available stock locations.

Related Guide: How to manage stock locations.

Mutations

useAdminCreateStockLocation

This hook creates a stock location.

Example

import React from "react"
import { useAdminCreateStockLocation } from "medusa-react"

const CreateStockLocation = () => {
const createStockLocation = useAdminCreateStockLocation()
// ...

const handleCreate = (name: string) => {
createStockLocation.mutate({
name,
}, {
onSuccess: ({ stock_location }) => {
console.log(stock_location.id)
}
})
}

// ...
}

export default CreateStockLocation

Mutation Function Parameters

AdminPostStockLocationsReqAdminPostStockLocationsReqRequired
The details of the stock location to create.

Mutation Function Returned Data

AdminStockLocationsResAdminStockLocationsResRequired
The stock location's details.

useAdminUpdateStockLocation

This hook updates a stock location's details.

Example

import React from "react"
import { useAdminUpdateStockLocation } from "medusa-react"

type Props = {
stockLocationId: string
}

const StockLocation = ({ stockLocationId }: Props) => {
const updateLocation = useAdminUpdateStockLocation(
stockLocationId
)
// ...

const handleUpdate = (
name: string
) => {
updateLocation.mutate({
name
}, {
onSuccess: ({ stock_location }) => {
console.log(stock_location.name)
}
})
}
}

export default StockLocation

Hook Parameters

idstringRequired
The stock location's ID.

Mutation Function Parameters

AdminPostStockLocationsLocationReqAdminPostStockLocationsLocationReqRequired
The details to update of the stock location.

Mutation Function Returned Data

AdminStockLocationsResAdminStockLocationsResRequired
The stock location's details.

useAdminDeleteStockLocation

This hook deletes a stock location.

Example

import React from "react"
import { useAdminDeleteStockLocation } from "medusa-react"

type Props = {
stockLocationId: string
}

const StockLocation = ({ stockLocationId }: Props) => {
const deleteLocation = useAdminDeleteStockLocation(
stockLocationId
)
// ...

const handleDelete = () => {
deleteLocation.mutate(void 0, {
onSuccess: ({ id, object, deleted }) => {
console.log(id)
}
})
}
}

export default StockLocation

Hook Parameters

idstringRequired
The stock location's ID.

Mutation Function Returned Data

DeleteResponseDeleteResponseRequired
The response returned for a DELETE request.

Queries

useAdminStockLocations

This hook retrieves a list of stock locations. The stock locations can be filtered by fields such as name or created_at passed in the query parameter. The stock locations can also be sorted or paginated.

Example

To list stock locations:

import React from "react"
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

To specify relations that should be retrieved within the stock locations:

import React from "react"
import { useAdminStockLocations } from "medusa-react"

function StockLocations() {
const {
stock_locations,
isLoading
} = useAdminStockLocations({
expand: "address"
})

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

By default, only the first 20 records are retrieved. You can control pagination by specifying the limit and offset properties:

import React from "react"
import { useAdminStockLocations } from "medusa-react"

function StockLocations() {
const {
stock_locations,
limit,
offset,
isLoading
} = useAdminStockLocations({
expand: "address",
limit: 10,
offset: 0
})

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

Hook Parameters

Filters and pagination configurations to apply on the retrieved stock locations.

Query Returned Data

limitnumberRequired
The limit applied on the retrieved items.
offsetnumberRequired
The number of items skipped before retrieving the list of items.
countnumberRequired
The total count of items.
stock_locationsStockLocationExpandedDTO[]Required
The list of stock locations.

useAdminStockLocation

This hook retrieves a stock location's details.

Example

import React from "react"
import { useAdminStockLocation } from "medusa-react"

type Props = {
stockLocationId: string
}

const StockLocation = ({ stockLocationId }: Props) => {
const {
stock_location,
isLoading
} = useAdminStockLocation(stockLocationId)

return (
<div>
{isLoading && <span>Loading...</span>}
{stock_location && (
<span>{stock_location.name}</span>
)}
</div>
)
}

export default StockLocation

Hook Parameters

idstringRequired
The stock location's ID.

Query Returned Data

stock_locationStockLocationExpandedDTORequired
Stock location details.
Was this section helpful?