InvenTree: Bulk create stock locations using Python API scripting

This script bulk creates stock locations A1..19, B1..19 and C1..19. You should customize it according

It requires a config.yaml with your specific InvenTree instance data.

inventree_create_drawers.py
#!/usr/bin/env python3
"""Create Schublade sublocations under an Apothekerschrank location in InvenTree.

This script connects to an InvenTree instance via the REST API and creates
a set of drawer ("Schublade") sublocations beneath a parent location
called "Apothekerschrank". For each of the three rows A, B, and C, it
creates drawers numbered 1 through 19, yielding 57 sublocations total.
Each sublocation is assigned the "Schublade" stock location type.

The script is idempotent: if a sublocation already exists under the
parent, it is skipped rather than duplicated.

Configuration is read from ``config.yaml`` in the same directory:

.. code-block:: yaml

    inventree:
      server: https://inventree.example.com
      token: your-api-token-here

Requirements:
    - Python 3.8+
    - requests
    - PyYAML

Usage::

    python3 create_locations.py
"""

import sys
import requests
import yaml
from pathlib import Path

# Path to the YAML configuration file, expected next to this script.
CONFIG_PATH = Path(__file__).parent / "config.yaml"


def load_config():
    """Load InvenTree connection settings from ``config.yaml``.

    Returns a dict with ``server`` and ``token`` keys.
    """
    with open(CONFIG_PATH, "r") as f:
        return yaml.safe_load(f)["inventree"]


class InvenTreeAPI:
    """Minimal REST API client for InvenTree.

    Wraps :mod:`requests` with token-based authentication and convenience
    methods for GET and POST calls against the InvenTree API.
    """

    def __init__(self, server, token):
        """Initialize the API client.

        :param server: Base URL of the InvenTree instance, e.g.
            ``https://inventree.example.com``.
        :param token: API authentication token (generate one in the
            InvenTree web UI under *Settings > API Keys*).
        """
        self.server = server.rstrip("/")
        self.token = token
        self.session = requests.Session()
        self.session.headers.update({
            "Authorization": f"Token {token}",
            "Content-Type": "application/json",
        })

    def get(self, path, params=None):
        """Perform a GET request and return the parsed JSON response."""
        r = self.session.get(f"{self.server}{path}", params=params)
        r.raise_for_status()
        return r.json()

    def post(self, path, data):
        """Perform a POST request with a JSON body and return the parsed response."""
        r = self.session.post(f"{self.server}{path}", json=data)
        if r.status_code == 400:
            print(f"  ERROR 400: {r.json()}")
        r.raise_for_status()
        return r.json()


def find_location_type(api, name):
    """Find a stock location type by name.

    Iterates through all location types via paginated API calls.
    Returns the primary key (``pk``) if found, otherwise ``None``.
    """
    offset = 0
    while True:
        data = api.get("/api/stock/location-type/", params={"limit": 100, "offset": offset})
        results = data if isinstance(data, list) else data.get("results", [])
        for t in results:
            if t["name"] == name:
                return t["pk"]
        if isinstance(data, list) or not data.get("next"):
            break
        offset += 100
    return None


def find_location(api, name):
    """Find a stock location by name across all locations.

    Iterates through all stock locations via paginated API calls.
    Returns the primary key (``pk``) if found, otherwise ``None``.
    """
    offset = 0
    while True:
        data = api.get("/api/stock/location/", params={"limit": 100, "offset": offset})
        for loc in data["results"]:
            if loc["name"] == name:
                return loc["pk"]
        if not data["next"]:
            break
        offset += 100
    return None


def find_child_location(api, parent_pk, name):
    """Find a child stock location by name under a given parent.

    Queries the API for all locations whose ``parent`` matches
    ``parent_pk`` and returns the ``pk`` of the first match, or ``None``.
    """
    results = api.get("/api/stock/location/", params={
        "parent": parent_pk, "limit": 1000
    })
    loc_list = results if isinstance(results, list) else results.get("results", [])
    for loc in loc_list:
        if loc["name"] == name:
            return loc["pk"]
    return None


def main():
    """Main entry point: find parent location, ensure location type, create sublocations."""
    config = load_config()
    api = InvenTreeAPI(config["server"], config["token"])

    # --- Find or create the "Schublade" stock location type ---
    # InvenTree supports user-defined location types that can be assigned
    # to individual stock locations. We reuse an existing type if present.
    schublade_type_pk = find_location_type(api, "Schublade")
    if schublade_type_pk:
        print(f"Location type 'Schublade' found (pk={schublade_type_pk})")
    else:
        result = api.post("/api/stock/location-type/", {
            "name": "Schublade",
            "description": "Schublade im Apothekerschrank",
            "icon": "ti:box:outline",
        })
        schublade_type_pk = result["pk"]
        print(f"Location type 'Schublade' created (pk={schublade_type_pk})")

    # --- Find the "Apothekerschrank" parent location ---
    # This location must already exist in InvenTree. The script will exit
    # with an error if it cannot be found.
    apotheker_pk = find_location(api, "Apothekerschrank")
    if not apotheker_pk:
        print("ERROR: Location 'Apothekerschrank' not found")
        sys.exit(1)
    print(f"Location 'Apothekerschrank' found (pk={apotheker_pk})")

    # --- Create sublocations: Schublade A1..A19, B1..B19, C1..C19 ---
    # Each sublocation is created as a child of the Apothekerschrank
    # location and assigned the "Schublade" location type. Locations
    # that already exist are skipped to keep the script idempotent.
    created = 0
    skipped = 0
    for letter in ["A", "B", "C"]:
        for number in range(1, 20):
            name = f"Schublade {letter}{number}"
            existing = find_child_location(api, apotheker_pk, name)
            if existing:
                print(f"  Skipped (exists): {name} (pk={existing})")
                skipped += 1
                continue
            result = api.post("/api/stock/location/", {
                "name": name,
                "parent": apotheker_pk,
                "location_type": schublade_type_pk,
            })
            print(f"  Created: {name} (pk={result['pk']})")
            created += 1

    print(f"\nDone: {created} created, {skipped} skipped")


if __name__ == "__main__":
    main()

Check out similar posts by category: InvenTree