How to Manage Regions using Admin APIs
In this document, you’ll learn how to manage regions using the Admin APIs.
Overview
Using the region admin REST APIs, you can manage regions in your store, including creating, updating, and deleting regions.
Scenario
You want to add or use the following admin functionalities:
- List regions
- Create a region
- Update a region
- Add shipping options to a region
- Delete a Region
You can use Medusa’s Admin APIs to achieve more functionalities as well. Check out the API reference to learn more.
Prerequisites
Medusa Components
It is assumed that you already have a Medusa server installed and set up. If not, you can follow the quickstart guide to get started.
JS Client
This guide includes code snippets to send requests to your Medusa server 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 installed and have created an instance of the client.
Medusa React
This guide also includes code snippets to send requests to your Medusa server using Medusa React, among other methods.
If you follow the Medusa React code blocks, it's assumed you already have Medusa React installed and have used MedusaProvider higher in your component tree.
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.
List Regions
You can retrieve regions available on your server using the List Regions endpoint:
- Medusa JS Client
- Medusa React
- Fetch API
- cURL
medusa.admin.regions.list()
.then(({ regions, limit, offset, count }) => {
console.log(regions.length)
// display regions
})
import { Region } from "@medusajs/medusa"
import { useAdminRegions } from "medusa-react"
const Regions = () => {
const { regions, isLoading } = useAdminRegions()
return (
<div>
{isLoading && <span>Loading...</span>}
{regions && !regions.length && <span>No Regions</span>}
{regions && regions.length > 0 && (
<ul>
{regions.map((region: Region) => (
<li key={region.id}>{region.name}</li>
))}
</ul>
)}
</div>
)
}
export default Regions
fetch(`<SERVER_URL>/admin/regions`, {
credentials: "include",
})
.then((response) => response.json())
.then(({ regions, limit, offset, count }) => {
console.log(regions.length)
// display regions
})
curl -L -X GET '<SERVER_URL>/admin/regions' \
-H 'Authorization: Bearer <API_TOKEN>'
This request returns an array of regions, as well as pagination fields.
You can also pass filters and other selection query parameters to the request. Check out the API reference for more details on available query parameters.
Create a Region
You can create a region by sending a request to the Create a Region endpoint:
- Medusa JS Client
- Medusa React
- Fetch API
- cURL
medusa.admin.regions.create({
name: "Europe",
currency_code: "eur",
tax_rate: 0,
payment_providers: [
"manual",
],
fulfillment_providers: [
"manual",
],
countries: [
"DK",
],
})
.then(({ region }) => {
console.log(region.id)
})
import { useAdminCreateRegion } from "medusa-react"
const CreateRegion = () => {
const createRegion = useAdminCreateRegion()
// ...
const handleCreate = () => {
createRegion.mutate({
name: "Europe",
currency_code: "eur",
tax_rate: 0,
payment_providers: [
"manual",
],
fulfillment_providers: [
"manual",
],
countries: [
"DK",
],
})
}
// ...
}
export default CreateRegion
fetch(`<SERVER_URL>/admin/regions`, {
credentials: "include",
method: "POST",
body: JSON.stringify({
name: "Europe",
currency_code: "eur",
tax_rate: 0,
payment_providers: [
"manual",
],
fulfillment_providers: [
"manual",
],
countries: [
"DK",
],
}),
})
.then((response) => response.json())
.then(({ region }) => {
console.log(region.id)
})
curl -L -X POST '<SERVER_URL>/admin/regions' \
-H 'Authorization: Bearer <API_TOKEN>' \
-H 'Content-Type: application/json' \
--data-raw '{
"name": "Europe",
"currency_code": "eur",
"tax_rate": 0,
"payment_providers": [
"manual"
],
"fulfillment_providers": [
"manual"
],
"countries": [
"DK"
]
}'
This request requires the following body parameters:
name
: The name of the region.currency_code
: The 3 character ISO currency code.tax_rate
: The tax rate in the Region.payment_providers
: An array of payment provider IDs. The array must contain at least one item.fulfillment_providers
: An array of fulfillment provider IDs. The array must contain at least one item.countries
: An array of the 2 character ISO code of the countries included in the region.
This request also accepts optional parameters, which you can view in the API reference.
The request returns the created region in the response.
Update a Region
You can update any of the region’s fields and configurations. The REST APIs offer different APIs for updating specific configurations, such as the Add Country endpoint.
Alternatively, you can update the details of a region using the Update a Region endpoint:
- Medusa JS Client
- Medusa React
- Fetch API
- cURL
medusa.admin.regions.update(regionId, {
countries: [
"DK",
"DE",
],
})
.then(({ region }) => {
console.log(region.id)
})
import { useAdminUpdateRegion } from "medusa-react"
const UpdateRegion = () => {
const updateRegion = useAdminUpdateRegion(regionId)
// ...
const handleUpdate = () => {
updateRegion.mutate({
countries: [
"DK",
"DE",
],
})
}
// ...
}
export default UpdateRegion
fetch(`<SERVER_URL>/admin/regions/${regionId}`, {
credentials: "include",
method: "POST",
body: JSON.stringify({
countries: [
"DK",
"DE",
],
}),
})
.then((response) => response.json())
.then(({ region }) => {
console.log(region.id)
})
curl -L -X POST '<SERVER_URL>/admin/regions/<REGION_ID>' \
-H 'Authorization: Bearer <API_TOKEN>' \
-H 'Content-Type: application/json' \
--data-raw '{
"countries": [
"DK",
"DE"
]
}'
This request accepts in its body parameters any of the region’s fields that you want to update. In the example above, you update the list of countries that are included in that region.
You can see the list of accepted fields in the API reference.
This request returns the full object of the updated region.
In the example above, the list of countries replace any countries that were previously in the region. So, if you’re adding a country, make sure to include previously added countries as well.
Add a Shipping Option to a Region
You can add a shipping option to a region by sending a request to the Create Shipping Option endpoint:
- Medusa JS Client
- Medusa React
- Fetch API
- cURL
medusa.admin.shippingOptions.create({
name: "PostFake",
region_id: regionId,
provider_id: "manual",
data: {
},
price_type: "flat_rate",
amount: 1000,
})
.then(({ shipping_option }) => {
console.log(shipping_option.id)
})
import { useAdminCreateShippingOption } from "medusa-react"
const Region = () => {
const createShippingOption = useAdminCreateShippingOption()
// ...
const handleCreate = () => {
createShippingOption.mutate({
name: "PostFake",
region_id: regionId,
provider_id: "manual",
data: {
},
price_type: "flat_rate",
amount: 1000,
})
}
// ...
}
export default Region
fetch(`<SERVER_URL>/admin/shipping-options`, {
credentials: "include",
method: "POST",
body: JSON.stringify({
name: "PostFake",
region_id: regionId,
provider_id: "manual",
price_type: "flat_rate",
data: {
},
amount: 1000,
}),
})
.then((response) => response.json())
.then(({ shipping_option }) => {
console.log(shipping_option.id)
})
curl -L -X POST '<SERVER_URL>/admin/shipping-options' \
-H 'Authorization: Bearer <API_TOKEN>' \
-H 'Content-Type: application/json' \
--data-raw '{
"name": "PostFake",
"region_id": "<REGION_ID>",
"provider_id": "manual",
"price_type": "flat_rate",
"data": {},
"amount": 1000
}'
This request requires the following body parameters:
name
: The name of the shipping option.region_id
: The ID of the region.provider_id
: The ID of the fulfillment provider. The fulfillment provider must be enabled in the region.data
: An object of data needed for the fulfillment provider to handle shipping with this shipping option. If none is required, you can pass an empty object.price_type
: The price type of the shipping option. Can beflat_rate
for fixed price, orcalculated
for prices that are calculated using custom logic.- If
price_type
isflat_rate
, theamount
field is then required. It is the price of the shipping option. Ifprice_type
iscalculated
,amount
is not required.
The is_return
body parameter can also be passed if the shipping option is a return shipping option. Its boolean value indicates whether the shipping option is a return option or not.
This request accepts other optional body parameters, which you can learn more about in the API reference.
This request returns the created shipping option.
You can also manage shipping options such as list, update, and delete. You can learn more in the API reference.
Delete a Region
You can delete a region by sending a request to the Delete a Region endpoint:
- Medusa JS Client
- Medusa React
- Fetch API
- cURL
medusa.admin.regions.delete(regionId)
.then(({ id, object, deleted }) => {
console.log(id)
})
import { useAdminDeleteRegion } from "medusa-react"
const Region = () => {
const deleteRegion = useAdminDeleteRegion(regionId)
// ...
const handleDelete = () => {
deleteRegion.mutate()
}
// ...
}
export default Region
fetch(`<SERVER_URL>/admin/regions/${regionId}`, {
credentials: "include",
method: "DELETE",
})
.then((response) => response.json())
.then(({ id, object, deleted }) => {
console.log(id)
})
curl -L -X DELETE '<SERVER_URL>/admin/regions/<REGION_ID>' \
-H 'Authorization: Bearer <API_TOKEN>'
This request requires the region ID as a path parameter. It deletes the region and returns the following fields:
id
: The ID of the deleted region.object
: The type of object that was deleted. In this case, the value will beregion
.deleted
: A boolean value indicating whether the region was deleted.