# Api Category import

**URL:** <https://forum.shopware.com/t/api-category-import/103763>\
**Category:** Shopware 6 (German)\
**Created:** [18. April 2024 um 18:04 UTC](https://forum.shopware.com/t/api-category-import/103763 "2024-04-18T18:04:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [18. April 2024 um 18:04 UTC](https://forum.shopware.com/t/api-category-import/103763/1 "2024-04-18T18:04:21Z")

</div>

Beim Importieren von einem Kategoriebaum in ein blankes System nehmen ich die Shopware API für Kategorien und die XML meines Datenimports wo die Kategorien mit „\>“ separiert sind. Das hin und her abgleichen ist ziemlich aufwändig. Gibt es hier keine leichte Lösung?

```auto
SubCat > SubsubCat

```

Idee: kann man die Kategorien nicht importieren , so wie der Breadcrumb ausgegeben wird? Und der Import legt es an if not exists?

```auto
"breadcrumb": [
"Home",
"Subcat", 
"Subsubcat"
],

```

---

<div class="post-metadata">

**Author:** ![Murmeltier](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/murmeltier/32/16126_2.png) [@Murmeltier](https://forum.shopware.com/u/Murmeltier)\
**Post date:** [18. April 2024 um 18:21 UTC](https://forum.shopware.com/t/api-category-import/103763/2 "2024-04-18T18:21:44Z")

</div>

Sollte eigentlich machbar sein, oder?  
Man könnte ja dafür z.B ein CSV File hernehmen, also so:

```auto
Breadcrumb Level 1, Breadcrumb Level 2, Breadcrumb Level 3, ...
Home, Subcat, Subsubcat, ...
Home, Subcat, AnotherSubsubcat, ...

```

Den Rest könnte man dann evtl mit einem Python-Skript erledigen, das die CSV-Datei liest und Kategorien in Shopware 6 über die API erstellt.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [18. April 2024 um 18:27 UTC](https://forum.shopware.com/t/api-category-import/103763/3 "2024-04-18T18:27:04Z")

</div>

Na die liegen sozusagen schon so vor wie in deinem Beispiel beschrieben, jedoch weiss ich nicht wie ich das in die API übergeben kann.  
Ich muss dann immer checken: existiert die Subcat auf diesem Level mit der und der parentId schon? Nein, dann importieren sie. Wäre irgendwie cool wenn man das einfach per Array übergeben könnte.

---

<div class="post-metadata">

**Author:** ![Murmeltier](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/murmeltier/32/16126_2.png) [@Murmeltier](https://forum.shopware.com/u/Murmeltier)\
**Post date:** [18. April 2024 um 18:30 UTC](https://forum.shopware.com/t/api-category-import/103763/4 "2024-04-18T18:30:40Z")

</div>

Ginge das Ganze evtl. sogar über die Konsole?

```auto
bin/console dal:import:category categories.csv

```

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [18. April 2024 um 22:21 UTC](https://forum.shopware.com/t/api-category-import/103763/5 "2024-04-18T22:21:52Z")

</div>

> [@brettvormkopp](#):
>
> Wäre irgendwie cool wenn man das einfach per Array übergeben könnte.

Da unendlich viele gleich benannte Kategorien erstellt werden können, ist das nicht möglich. Die UUID ist dafür zwingend. Die kann man aber selbst erstellen und muss das nicht Shopware überlassen. So kann man mit zwei API Requests 1. Aktuelle Kategorien abfragen und 2. per Upsert alle Kategorien einfügen das Vorhaben umsetzen.

---

<div class="post-metadata">

**Author:** ![area-net-gmbh](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/area-net-gmbh/32/21995_2.png) [@area-net-gmbh](https://forum.shopware.com/u/area-net-gmbh)\
**Post date:** [19. April 2024 um 06:08 UTC](https://forum.shopware.com/t/api-category-import/103763/6 "2024-04-19T06:08:01Z")

</div>

Oder direkt per DB. Je nachdem von wieviel Kategorien wir sprechen, dürfte das schneller sei - da ich es da mit nur einem Request (bzw. zwei mit Category-Translation) abhandeln kann.

Muss sich @brettvormkopp eine Logik überlegen, wie er es mit der Sortierung (after\_category\_id) hinbekommt - aber das muss er über die API ja auch.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [19. April 2024 um 08:01 UTC](https://forum.shopware.com/t/api-category-import/103763/7 "2024-04-19T08:01:55Z")

</div>

Ja leider hat die gelieferte XML keine IDs … aber es gibt eindeutige Namen im Level 2, sozusagen nach Home… und dann Level 3 und 4 gibt es jeweils in dem einen, und dem anderen Strang. der Strang ist sozusagen immer unique als „category.name“. Also meine Abrage wäre dann immer vorwärts und rückwärst die API runterreiten 😃

Winter \> VWreifen \> 14Zoll  
Sommer \> VWreifen \> 14Zoll

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [20. April 2024 um 10:33 UTC](https://forum.shopware.com/t/api-category-import/103763/8 "2024-04-20T10:33:15Z")

</div>

Ok, ich habe es jetzt geschafft die Shopware Menus und die XML zu mappen.  
Wie bekomme ich jetzt eine Kategorie erstellt über die API?

Hier ist ein Beispiel, aber die Rückgabe des POST ist ein JSON mit allen Kategorien und die neue Fehlt.

> **[Product Management | Admin API](https://shopware.stoplight.io/docs/admin-api/e51cf55ab14a4-product-management)**
>
> This section explains the handling of the product data structure. Powered by Stoplight.

Habe dann versucht mehr Daten(siehe Beispeil) zu übertragen, aber auch das geht nicht. Hat jemand eine Idee?

```auto
{
    "displayNestedProducts": true,
    "type": "page",
    "productAssignmentType": "product",
    "name": "peter",
    "id": "018d131737217262b7f66a47ecbfa555",
    "parent_id": "018d131737217262b7f66a47ecbfa580",
    "version_id": "0x0fa91ce3e96a4bc2be4bd9ce752c3425",
    "parent_version_id": "0fa91ce3e96a4bc2be4bd9ce752c3425",
    "after_category_version_id": "0fa91ce3e96a4bc2be4bd9ce752c3425",
    "level":2,
    "active": true,
    "createdAt": "2024-04-19 19:05:31.033",
    "parent": {},
  "children": [
    {}
  ]
}

```

Danke und Gruss

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [20. April 2024 um 12:06 UTC](https://forum.shopware.com/t/api-category-import/103763/9 "2024-04-20T12:06:42Z")

</div>

parentId und afterCategoryId sind die zwei UUIDs, die du benötigst.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [20. April 2024 um 12:21 UTC](https://forum.shopware.com/t/api-category-import/103763/10 "2024-04-20T12:21:54Z")

</div>

@Max_Shop vielen Dank. leider geht es auch nicht mit diesen, muss die id umgewandelt werden? Mit 0x…?  
Kannst du bitte ein minimal json schreiben, wie ich das reinbekomme?

„peter“ soll subcat von „Home“(018d131737217262b7f66a47ecbfa580) sein

```auto
{
    "name": "peter",
    "id": "018d131737217262b7f66a47ecbfa555",
    "parent_id": "018d131737217262b7f66a47ecbfa580",
    "after_category_version_id":"0fa91ce3e96a4bc2be4bd9ce752c3425"
}

```

Vielen lieben Dank.

API: api/category  
METHOD: POST  
Bearer Token vorhanden

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [20. April 2024 um 18:59 UTC](https://forum.shopware.com/t/api-category-import/103763/11 "2024-04-20T18:59:49Z")

</div>

Schau dir mal an, was alles mit required gekennzeichnet ist:

> **[Category | Admin API](https://shopware.stoplight.io/docs/admin-api/branches/main/9b609052379da-category)**
>
> Added since version: 6.0.0.0 Powered by Stoplight.

Was aber viel einfacher ist, gehe ins Backend, lege eine neue Kategorie an und schau dir dann das XHR an, welches das Backend an die API geschickt hat. Dann hast du dein JSON per Copy & Past.

Ich glaube, das hast du ja schon in einem vorherigen Beitrag geposrted.

Also einfach zusätzlich die required übernehmen.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [21. April 2024 um 10:07 UTC](https://forum.shopware.com/t/api-category-import/103763/12 "2024-04-21T10:07:48Z")

</div>

Ich habe langsam den Verdacht, dass man mit der API gar keine Kategorien anlegen kann, es gibt ja auch nirgendwo eine beschreibung dazu. Kann ein Shopware Mitarbeiter dazu was sagen? @Moritz_Naczenski

Hier ist mein Code:

POST:

```auto
domain..../api/category

```

HEADER:

```auto
'Accept: application/json',
    'Content-Type: application/json',
    'Authorization: Bearer eyJ0eXAiOiJKV1Qi......'

```

BODY:

```auto
{
    "total": 1,
    "data": [{
    "extensions": {
        "foreignKeys": {
            "extensions": [],
            "apiAlias": "category_foreign_keys_extension"
        },
        "search": {
            "extensions": [],
            "_uniqueIdentifier": null,
            "translated": [],
            "autoIncrement": 2
        }
    },
    "_uniqueIdentifier": "018ef1165ded7c02a347fab7948ece2e",
    "versionId": "0fa91ce3e96a4bc2be4bd9ce752c3425",
    "translated": {
        "breadcrumb": [
            "Home",
            "Peter"
        ],
        "name": "Peter",
        "customFields": {},
        "slotConfig": [],
        "linkType": null,
        "internalLink": null,
        "externalLink": null,
        "linkNewTab": null,
        "description": null,
        "metaTitle": null,
        "metaDescription": null,
        "keywords": null
    },
    "createdAt": "2024-04-18T12:02:47.987+00:00",
    "updatedAt": "2024-04-18T12:07:48.883+00:00",
    "afterCategoryId": null,
    "parentId": "018d131737217262b7f66a47ecbfa580",
    "autoIncrement": 2,
    "mediaId": null,
    "name": "Peter",
    "breadcrumb": [
        "Home",
        "Peter"
    ],
    "path": "|018d131737217262b7f66a47ecbfa580|",
    "level": 2,
    "active": true,
    "childCount": 1,
    "visibleChildCount": 0,
    "displayNestedProducts": true,
    "parent": null,
    "children": null,
    "translations": null,
    "media": null,
    "products": null,
    "nestedProducts": null,
    "tags": null,
    "cmsPageId": "018d1317375b708fa5ef34fbe5d5d445",
    "cmsPageIdSwitched": true,
    "cmsPage": null,
    "productStreamId": null,
    "productStream": null,
    "slotConfig": [],
    "navigationSalesChannels": null,
    "footerSalesChannels": null,
    "serviceSalesChannels": null,
    "linkType": null,
    "linkNewTab": null,
    "internalLink": null,
    "externalLink": null,
    "visible": true,
    "type": "page",
    "productAssignmentType": "product",
    "description": null,
    "metaTitle": null,
    "metaDescription": null,
    "keywords": null,
    "mainCategories": null,
    "seoUrls": null,
    "customEntityTypeId": null,
    "customFields": null,
    "id": "018ef1165ded7c02a347fab7948ece2e",
    "apiAlias": "category"
}],
    "aggregations": []
}

```

RESULT:  
Alle existierende Kategorien, aber nicht die eben hinzugefügte.

Bei dem von dir geposteten Link zu Category | Admin API sind auch Produkte aufgezählt, ich habe zum jetzigen Zeitpunkt keine. Ich will nur die Kategorien importieren.

Shopware 6.6.1.1  
PHP 8.3  
MySQL 10.6

> **[Domains, Webspace, Domain Webhosting, Server-Hosting Provider ALL-INKL](https://all-inkl.com)**
>
> Führender Webhost & Provider für Domains, Homepage-Webspace (Domain-Webhosting) & Server (Server-Hosting, Managed Server). ALL-INKL - für Retail & Reseller!

Danke und Gruss

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [21. April 2024 um 10:14 UTC](https://forum.shopware.com/t/api-category-import/103763/13 "2024-04-21T10:14:43Z")

</div>

OH MANN, bin ich blöd. ich hatte kein https sondern http…  
Jetzt hat es geklappt.  
Es reicht:

```auto
{
  "displayNestedProducts": true,
  "type": "page",
  "productAssignmentType": "product",
  "name": "Hallo"
}

```

Antwort vom Server ist dann „1“

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [21. April 2024 um 13:06 UTC](https://forum.shopware.com/t/api-category-import/103763/14 "2024-04-21T13:06:13Z")

</div>

ich möchte noch hinzufügen, dass man nur GET und POST nutzen sollte, bei PATCH löscht sich die deutsche Sprache und die Kategorie geht futsch. Durch die verwendung der UUID kann man es fast vergessen das wieder herzustellen. Ein falscher Klick in phpmyadmin auf den Eintrag und statt ESCAPE irgendwo hingeklickt und der Wert ist weg.

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [21. April 2024 um 13:08 UTC](https://forum.shopware.com/t/api-category-import/103763/15 "2024-04-21T13:08:37Z")

</div>

Du kannst im Header die Sprache definieren. Ohne Angabe der Sprache wird immer die Standardsprache verwendet.

Noch zu deinem JSON vorab:

parentId und afterCategoryId angeben, dann kannst du neue Kategorien exakt einfügen, wo du es möchtest.

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [21. April 2024 um 13:10 UTC](https://forum.shopware.com/t/api-category-import/103763/16 "2024-04-21T13:10:45Z")

</div>

> [@brettvormkopp](#):
>
> Ich habe langsam den Verdacht, dass man mit der API gar keine Kategorien anlegen kann

Das komplette/admin Backend basiert auf der API. Was du dort machen kannst, kannst du definitiv auch selbst über die API machen, ggf. mit mehreren GET und dann POST Requests.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [21. April 2024 um 13:20 UTC](https://forum.shopware.com/t/api-category-import/103763/17 "2024-04-21T13:20:58Z")

</div>

Das ist gut zu wissen dass das Backend es auch so macht.

Bei dem Beispiel wird jedoch parent\_id nicht übernommen. Ich wollte hier als Subcat von „Home“ haben, aber es landet als „Hauptkategorie“ in der Datenbank

```auto
{
  "displayNestedProducts": true,
  "type": "page",
  "productAssignmentType": "product",
  "name": "Peter",
  "parent_id": "018d131737217262b7f66a47ecbfa580",
  "after_category_version_id": "0fa91ce3e96a4bc2be4bd9ce752c3425"
}

```

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/3/0/302535354a7f4af83892d7952e001fa7ee53e088.png)

EDIT: kann es was mit dem Wert „path“ aus der Tabelle „category“ zu tun haben? Weil der ist auch leer.

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [21. April 2024 um 13:34 UTC](https://forum.shopware.com/t/api-category-import/103763/18 "2024-04-21T13:34:38Z")

</div>

Ich habe auch Unsinn geschrieben, entschuldige bitte.

Es sollte auch afterCategoryId und parentId verwendet werden und nicht after\_category\_version\_id und parent\_id

Die Schreibweisen mit \_ werden in der Datenbank verwendet, bei JSON macht man daraus camelCase.

path is read-only und wird automatisch von der API erstellt.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [21. April 2024 um 13:37 UTC](https://forum.shopware.com/t/api-category-import/103763/19 "2024-04-21T13:37:48Z")

</div>

Vielen vielen Dank, after\_category\_version\_id ist korrekt, nur parentID muss camelCase 🙂

So funktioniert es jetzt:

```auto
{
  "displayNestedProducts": true,
  "type": "page",
  "productAssignmentType": "product",
  "name": "Hallo",
  "parentId": "018d131737217262b7f66a47ecbfa580",
  "after_category_version_id": "0fa91ce3e96a4bc2be4bd9ce752c3425"
}

```

Vielen Lieben Dank @Max_Shop

---

<div class="post-metadata">

**Author:** ![EikeBrandtWarneke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@EikeBrandtWarneke](https://forum.shopware.com/u/EikeBrandtWarneke)\
**Post date:** [21. April 2024 um 13:38 UTC](https://forum.shopware.com/t/api-category-import/103763/20 "2024-04-21T13:38:42Z")

</div>

Wie immer gilt: schau dir mal den request in der Administration an, wenn du eine neue Kategorie anlegst. Das sollte helfen, um die Daten korrekt zu formatieren.

Viele Grüße

[Nächste Seite](https://forum.shopware.com/t/api-category-import/103763.md?page=2)
