Skip to content

About

A collection of administrative scripts for managing bulk operations in Medusa v2. This repository contains tools we use to efficiently manage our Medusa store, shared with the community.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Medusa Bulk Management Tools

A collection of administrative scripts for managing bulk operations in Medusa v2. This repository contains tools we use to efficiently manage our Medusa store, shared with the community.

What's Included

Currently includes scripts for:

  • Tax Region Management - Bulk create tax regions for multiple countries
  • VAT Rate Configuration - Set default VAT rates across all regions
  • Service Zone Management - Create and update shipping service zones with flexible country selection

More bulk operation scripts will be added as we build them for our store.

Prerequisites

  • Node.js (v18 or higher)
  • Medusa v2 server
  • Admin access to your Medusa instance

Installation

  1. Install dependencies:
npm install
  1. Create a .env file in the project root:
cp .env.example .env
  1. Configure your environment variables (see Authentication)

Authentication

Medusa v2 uses secret API keys for admin operations.

  1. Generate a Secret API Key:

    • Log into your Medusa Admin dashboard
    • Go to Settings → Developer → Secret API Keys
    • Create a new API key (you can only have one active key per user)
    • Copy the key immediately (it's only shown once)
  2. Add to .env file:

MEDUSA_API_URL=https://your-medusa-instance.com
MEDUSA_API_KEY=sk_your_secret_key_here

Important: Secret API keys use Authorization: Basic header format in Medusa v2, which these scripts handle automatically.

Available Scripts

Tax Management

Create EU Tax Regions

Creates tax regions for all EU countries plus Switzerland, Norway, Ukraine, Vatican City, and Monaco.

node create-eu-tax-regions.js

What it does:

  • Creates 33 tax regions with country codes
  • Sets the default tax provider (tp_system)
  • Skips regions that already exist

Countries included:

  • EU: AT, BE, BG, HR, CY, CZ, DK, EE, FI, FR, DE, GR, HU, IE, IT, LV, LT, LU, MT, NL, PL, PT, RO, SK, SI, ES, SE
  • Non-EU: CH, GB, MC, NO, UA, VA

Set VAT Rates

Sets the default VAT rate for all EU tax regions.

node set-eu-tax.js

What it does:

  • Fetches all tax regions (with pagination)
  • Sets 5% VAT as the default tax rate for each EU country
  • Skips regions that already have a default tax rate

Configuration: To change the VAT rate, edit the TAX_RATE constant in set-eu-tax.js:

const TAX_RATE = 5  // Change this to your desired rate

Shipping Management

Create Service Zone (Interactive)

Creates or updates a shipping service zone for a specific warehouse (stock location) with an interactive terminal interface. Service zones define which geographical areas can be served from a particular warehouse.

node create-service-zone.js

Interactive Steps:

The script will guide you through:

  1. Select Stock Location - Choose from your existing warehouses
  2. Fulfillment Set - Create a new one or enter an existing ID
  3. Service Zone Name - Enter a name for your zone
  4. Country Selection Mode - Choose how to select countries:
    • Include specific countries
    • Include all countries
    • Exclude specific countries
  5. Select Countries - Pick from:
    • EU Countries (predefined list)
    • All available countries from your store
    • Manual entry (comma-separated codes)
  6. Confirm - Review summary and proceed

Features:

  • ✅ Interactive prompts with arrow key navigation
  • ✅ Checkbox selection for multiple countries
  • ✅ Auto-creates fulfillment sets if needed
  • ✅ Updates existing zones or creates new ones
  • ✅ Validates all inputs
  • ✅ Shows summary before applying changes

Example Flow:

📦 Step 1: Select Stock Location (Warehouse)
? What would you like to do? Use existing stock location
? Select a stock location: European Warehouse (Wieliczka, PL)
✅ Selected: European Warehouse

📋 Step 2: Get Fulfillment Set
? Create a new fulfillment set for European Warehouse? Yes
? Enter fulfillment set name: Shipping
? Select fulfillment set type: Shipping (default)
✅ Created fulfillment set: fset_01XXXXX

🗺️  Step 3: Service Zone Configuration
? Enter service zone name: EU Zone

🌍 Step 4: Country Selection
? How do you want to select countries? Include specific countries

📍 Step 5: Select Countries
? Which countries would you like to work with? EU Countries (predefined list)
? Select countries to include: (Use arrow keys and space to select)
  ◉ Austria (AT)
  ◉ Belgium (BE)
  ◉ Bulgaria (BG)
  ...
✅ Including 27 countries

📝 Summary:
   Warehouse: European Warehouse
   Fulfillment Set: fset_01XXXXX
   Zone Name: EU Zone
   Countries: 27 countries
   Codes: at, be, bg, hr, cy, cz, dk, ee, fi, fr...

? Proceed with creating/updating this service zone? Yes
✅ Successfully created service zone!

Shared Resources

Countries List

The countries.js file contains a centralized list of European countries used across all scripts:

import { EU_COUNTRIES, EU_COUNTRY_CODES } from './countries.js'

// EU_COUNTRIES: Array of objects with code and name
// EU_COUNTRY_CODES: Array of just country codes

This ensures consistency across all scripts and makes it easy to update country lists in one place.

Environment Variables

Variable Required Description
MEDUSA_API_URL Yes Base URL of your Medusa instance
MEDUSA_API_KEY Yes Secret API key for authentication

Example .env File

# Medusa Instance
MEDUSA_API_URL=https://your-medusa-instance.com

# Secret API Key
MEDUSA_API_KEY=sk_your_secret_api_key_here

Usage Examples

Setting up EU Tax Regions from Scratch

  1. Create tax regions:
node create-eu-tax-regions.js
  1. Set VAT rates:
node set-eu-tax.js

Troubleshooting

401 Unauthorized Error

Problem: Scripts fail with "Request failed with status code 401"

Solution:

  • Verify your API key is correct in .env
  • Ensure the API key hasn't been revoked
  • Generate a new API key from Medusa Admin (Settings → Developer → Secret API Keys)
  • Remember: You can only have one active secret key per user

400 Bad Request Error

Problem: "Provider is required when creating a non-province tax region"

Solution: This is handled automatically by the scripts. The provider_id: "tp_system" is set for all tax regions.

Default Tax Rate Already Exists

Problem: Cannot set default tax rate because one already exists

Solution: Each tax region can only have one default tax rate. If you need to update it:

  1. Delete the existing default tax rate via Medusa Admin
  2. Run the script again

Security Notes

  • Never commit .env file - It contains sensitive credentials
  • API keys are sensitive - Treat them like passwords
  • Rotate keys regularly - Generate new keys periodically

Medusa v2 Specifics

These scripts are designed for Medusa v2 and use:

  • Authorization: Basic {api_key} header format for authentication
  • /admin/tax-regions endpoint for tax region management
  • provider_id: "tp_system" as the default tax provider

If you're using Medusa v1, these scripts will not work.

Dependencies

  • @medusajs/medusa-js (v6.1.10) - Medusa JavaScript SDK
  • dotenv (v17.2.3) - Environment variable management

Contributing

We welcome contributions! If you have bulk operation scripts that would be useful to the Medusa community, feel free to:

  • Open an issue to discuss your idea
  • Submit a pull request with your script
  • Share feedback on existing scripts

About

These scripts are actively used to manage our Medusa store. We're sharing them with the community to help others streamline their Medusa operations.

License

ISC

About

A collection of administrative scripts for managing bulk operations in Medusa v2. This repository contains tools we use to efficiently manage our Medusa store, shared with the community.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages