This is a pure V module that can be used to communicate with Shopware 6.
Requires at least Shopware version 6.4.
Shopware Admin API credentials can be generated in the shopware backend (Settings->System->Integrations).
Shopware 6 Admin API Endpoint Reference
Shopware 6 Developer Documentation
I recommend to configure the api credentials via .env - take a look at my dotenv module.
- built-in oauth token renewal
- useful helper functions for file upload and search
- can handle big imports that may take several hours
- parallel processing
- errors during compile time
Run "v init" to auto-generate your v.mod file.
v init
Then edit the dependencies in your v.mod file to look like this:
dependencies: ['thomaspeissl.shopwareac']
And install with:
v install
To update your dependencies later just run "v install" again.
v install thomaspeissl.shopwareac
git clone https://github.com/thomaspeissl/v-shopware-api-client.git ~/.vmodules/thomaspeissl/shopwareac
Fill in your api credentials in the code placeholders an then run.
cd examples
v run simple.v
v run search.v
This example gets products from the admin api and prints out their product ids.
module main
import thomaspeissl.shopwareac
import json
struct ShopResponse {
data []ShopResponseData
}
struct ShopResponseData {
id string
}
fn main() {
mut sw_api := shopwareac.Login{ // mut is needed for the automated oauth2 token renewal
api_url: 'http://localhost:8000/api/'
client_id: 'XXXXXXXXXXXXXXXXXXXXXXXXXX' // get this from Shopware 6 backend Settings->System->Integrations
client_secret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}
response := sw_api.get('product?limit=1')
response_data := json.decode(ShopResponse, response) or { ShopResponse{} }
for product in response_data.data {
println(product.id)
}
}
- date_time
- decode
- encode
- strip
- Attributes
- Category
- ConfiguratorSetting
- CustomField
- Id
- Login
- add_media_to_product
- auth
- delete
- find_category_by_customfield
- find_media_by_name
- find_product_by_customfield
- find_product_by_productnumber
- find_property_by_name
- find_subcategory_by_name
- get
- get_default_category
- get_default_cms_page
- get_default_media_folder
- get_default_payment_method
- get_default_sales_channel
- get_default_tax
- get_last_sync
- get_raw
- patch
- post
- resend_sync
- search
- sync
- sync_delete
- sync_upsert
- update_media_from_url
- upload
- upload_file
- Manufacturer
- Media
- Option_
- Price
- Product
- ProductMedia
- PropertyGroup
- ShopResponseData
- Tax
- Unit
- Visibility
fn date_time() string
current time formatted for Shopware date time custom fields eg. "2022-01-16T12:00:00+00:00"
fn decode(data string) map[string]json2.Any
fn encode(s string) string
Percent-encoding reserved characters eg. for filter parameters
fn strip(s string) string
strip not allowed chars
struct Attributes {
pub:
media_id string @[json: mediaId]
cover_id string @[json: coverId]
child_count int @[json: childCount]
stock int
custom_fields map[string]string @[json: customFields]
active bool
product_number string @[json: productNumber]
custom_search_keywords []string @[json: customSearchKeywords]
payment_method_id string @[json: paymentMethodId]
name string
parent_id string @[json: parentId]
cms_page_id string @[json: cmsPageId]
}
struct Category {
pub mut:
id string
name string @[omitempty]
parent_id string @[json: parentId; omitempty]
cms_page_id string @[json: cmsPageId]
}
struct ConfiguratorSetting {
pub mut:
id string @[omitempty]
option_id string @[json: 'optionId'; omitempty]
}
struct CustomField {
pub mut:
custom_import_field1 string @[omitempty]
custom_import_field2 string @[omitempty]
custom_import_field3 string @[omitempty]
custom_import_field4 string @[omitempty]
custom_import_field5 string @[omitempty]
custom_import_field6 string @[omitempty]
custom_import_field7 string @[omitempty]
custom_import_field8 string @[omitempty]
custom_import_field9 string @[omitempty]
}
struct Id {
pub mut:
id string @[omitempty]
}
struct Login {
mut:
token AuthToken
pub:
client_id string
client_secret string
pub mut:
api_url string
}
fn (mut l Login) add_media_to_product(media_id string, product_id string, set_as_cover bool, position int)
add_media_to_product position should begin with 0
fn (mut l Login) auth() bool
auth get's called automatic and renews the oauth token if needed
fn (mut l Login) delete(endpoint string, id string)
fn (mut l Login) find_category_by_customfield(field string, value string) !ShopResponseData
fn (mut l Login) find_media_by_name(name string) !ShopResponseData
fn (mut l Login) find_product_by_customfield(field string, value string) !ShopResponseData
fn (mut l Login) find_product_by_productnumber(productnumber string) !ShopResponseData
fn (mut l Login) find_property_by_name(name string, group string) !ShopResponseData
fn (mut l Login) find_subcategory_by_name(name string, parent string) !ShopResponseData
fn (mut l Login) get(endpoint string) string
fn (mut l Login) get_default_category() string
fn (mut l Login) get_default_cms_page() string
fn (mut l Login) get_default_media_folder() string
fn (mut l Login) get_default_payment_method() string
fn (mut l Login) get_default_sales_channel() string
fn (mut l Login) get_default_tax() string
fn (mut l Login) get_last_sync() string
get_last_sync returns the last sync payload
fn (mut l Login) get_raw(endpoint string) http.Response
fn (mut l Login) patch(endpoint string, data string)
fn (mut l Login) post(endpoint string, data string) string
post returns the id of the created content on success
fn (mut l Login) resend_sync()
resend_sync sends the last sync operation (sync saves data into a file) again to the shop api - useful for debugging or temporary errors
fn (mut l Login) search(entity string, data string) string
fn (mut l Login) sync(data string) !string
sync API is an add-on to the Admin API that allows you to perform multiple write operations (creating/updating and deleting) simultaneously
fn (mut l Login) sync_delete(entity string, data []string)
sync_delete is a shorthand function for sync with data chunking for large arrays
fn (mut l Login) sync_upsert(entity string, data []string)
sync_upsert is a shorthand function for sync with data chunking for large arrays
fn (mut l Login) update_media_from_url(media_id string, url string)
Attach resource data to the media object from the given url
fn (mut l Login) upload(file_url string, name string, media_folder_id string) !string
upload returns the mediaId of the uploaded file on success
fn (mut l Login) upload_file(media_id string, name string, _ext string, data string) !
upload_file via binary blob
struct Manufacturer {
pub mut:
id string
name string @[omitempty]
link string @[omitempty]
description string @[omitempty]
media Media @[omitempty]
}
struct Media {
pub:
id string @[omitempty]
media_folder_id string @[json: 'mediaFolderId'; omitempty]
}
struct Option_ { // Option is a reserved word
pub mut:
id string @[omitempty]
name string @[omitempty]
group_id string @[json: 'groupId'; omitempty]
}
struct Price {
pub mut:
net f64
gross f64
currency_id string = 'b7d2554b0ce847cd82f3ac9bd1c0dfca' @[json: 'currencyId']
linked bool = true
}
struct Product {
pub mut:
id string
name string @[omitempty]
stock ?int @[omitempty]
product_number string @[json: 'productNumber'; omitempty]
description string @[omitempty]
manufacturer Id @[omitempty]
categories []Id @[omitempty]
visibilities []Visibility @[omitempty]
tax_id string @[json: 'taxId'; omitempty]
keywords string @[omitempty]
custom_search_keywords []string @[json: 'customSearchKeywords'; omitempty]
options []Option_ @[omitempty]
weight int @[omitempty]
price []Price @[omitempty]
cover_id string @[json: 'coverId'; omitempty]
unit_id string @[json: 'unitId'; omitempty]
media []ProductMedia @[omitempty]
custom_fields CustomField @[json: 'customFields'; omitempty]
ean string @[omitempty]
meta_title string @[json: 'metaTitle'; omitempty]
meta_description string @[json: 'metaDescription'; omitempty]
parent_id string @[json: 'parentId'; omitempty]
reference_unit f64 @[json: 'referenceUnit'; omitempty]
purchase_unit f64 @[json: 'purchaseUnit'; omitempty]
max_purchase int @[json: 'maxPurchase'; omitempty]
configurator_settings []ConfiguratorSetting @[json: 'configuratorSettings'; omitempty]
}
struct ProductMedia {
pub mut:
id string
position int
media Media
}
struct PropertyGroup {
pub mut:
id string @[omitempty]
name string @[omitempty]
}
struct ShopResponseData {
pub:
id string
attributes Attributes
}
struct Tax {
pub mut:
id string @[omitempty]
name string @[omitempty]
tax_rate ?f64 @[json: 'taxRate'; omitempty]
}
struct Unit {
pub mut:
id string
name string @[omitempty]
short_code string @[json: 'shortCode'; omitempty]
}
struct Visibility {
pub mut:
id string @[omitempty]
sales_channel_id string @[json: 'salesChannelId'; omitempty]
visibility int = 30 @[omitempty]
}
v doc -f md .